Skip to content

Prevent unnecessary string duplication in assert() #11031

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions ext/standard/assert.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ ZEND_DECLARE_MODULE_GLOBALS(assert)

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

#define SAFE_STRING(s) ((s)?(s):"")

PHPAPI zend_class_entry *assertion_error_ce;

static PHP_INI_MH(OnChangeCallback) /* {{{ */
Expand Down Expand Up @@ -151,9 +149,12 @@ PHP_FUNCTION(assert)
zval args[4];
zval retval;
uint32_t lineno = zend_get_executed_lineno();
const char *filename = zend_get_executed_filename();
zend_string *filename = zend_get_executed_filename_ex();
if (UNEXPECTED(!filename)) {
filename = ZSTR_KNOWN(ZEND_STR_UNKNOWN_CAPITALIZED);
}

ZVAL_STRING(&args[0], SAFE_STRING(filename));
ZVAL_STR(&args[0], filename);
ZVAL_LONG(&args[1], lineno);
ZVAL_NULL(&args[2]);

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

zval_ptr_dtor(&args[0]);
zval_ptr_dtor(&retval);
}

Expand Down
35 changes: 35 additions & 0 deletions ext/standard/tests/assert/assert_closures_multiple.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--TEST--
assert() asserting multiple with callback
--INI--
assert.active = 1
assert.warning = 1
assert.bail = 0
assert.exception=1
--FILE--
<?php
assert_options(ASSERT_CALLBACK, function ($f) {});
try {
assert(false);
} catch (Throwable) {}
try {
assert(false);
} catch (Throwable) {}
try {
assert(false);
} catch (Throwable) {}
try {
assert(false);
} catch (Throwable) {}
try {
assert(false);
} catch (Throwable) {}
try {
assert(false);
} catch (Throwable) {}
try {
assert(false);
} catch (Throwable) {}
?>
DONE
--EXPECT--
DONE