Skip to content

Commit 1820c42

Browse files
authored
Prevent unnecessary string duplication in assert() (#11031)
1 parent 5107483 commit 1820c42

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

ext/standard/assert.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ ZEND_DECLARE_MODULE_GLOBALS(assert)
3434

3535
#define ASSERTG(v) ZEND_MODULE_GLOBALS_ACCESSOR(assert, v)
3636

37-
#define SAFE_STRING(s) ((s)?(s):"")
38-
3937
PHPAPI zend_class_entry *assertion_error_ce;
4038

4139
static PHP_INI_MH(OnChangeCallback) /* {{{ */
@@ -151,9 +149,12 @@ PHP_FUNCTION(assert)
151149
zval args[4];
152150
zval retval;
153151
uint32_t lineno = zend_get_executed_lineno();
154-
const char *filename = zend_get_executed_filename();
152+
zend_string *filename = zend_get_executed_filename_ex();
153+
if (UNEXPECTED(!filename)) {
154+
filename = ZSTR_KNOWN(ZEND_STR_UNKNOWN_CAPITALIZED);
155+
}
155156

156-
ZVAL_STRING(&args[0], SAFE_STRING(filename));
157+
ZVAL_STR(&args[0], filename);
157158
ZVAL_LONG(&args[1], lineno);
158159
ZVAL_NULL(&args[2]);
159160

@@ -166,7 +167,6 @@ PHP_FUNCTION(assert)
166167
call_user_function(NULL, NULL, &ASSERTG(callback), &retval, 3, args);
167168
}
168169

169-
zval_ptr_dtor(&args[0]);
170170
zval_ptr_dtor(&retval);
171171
}
172172

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
assert() asserting multiple with callback
3+
--INI--
4+
assert.active = 1
5+
assert.warning = 1
6+
assert.bail = 0
7+
assert.exception=1
8+
--FILE--
9+
<?php
10+
assert_options(ASSERT_CALLBACK, function ($f) {});
11+
try {
12+
assert(false);
13+
} catch (Throwable) {}
14+
try {
15+
assert(false);
16+
} catch (Throwable) {}
17+
try {
18+
assert(false);
19+
} catch (Throwable) {}
20+
try {
21+
assert(false);
22+
} catch (Throwable) {}
23+
try {
24+
assert(false);
25+
} catch (Throwable) {}
26+
try {
27+
assert(false);
28+
} catch (Throwable) {}
29+
try {
30+
assert(false);
31+
} catch (Throwable) {}
32+
?>
33+
DONE
34+
--EXPECT--
35+
DONE

0 commit comments

Comments
 (0)