Skip to content

Instantly share code, notes, and snippets.

@nielsdos
Created January 13, 2024 16:57
Show Gist options
  • Save nielsdos/79419e56941ae80062e1b382c7e275f9 to your computer and use it in GitHub Desktop.
Save nielsdos/79419e56941ae80062e1b382c7e275f9 to your computer and use it in GitHub Desktop.
diff --git a/Zend/zend_builtin_functions.c b/Zend/zend_builtin_functions.c
index 9c4b836caf..72cb07decb 100644
--- a/Zend/zend_builtin_functions.c
+++ b/Zend/zend_builtin_functions.c
@@ -1108,10 +1108,9 @@ ZEND_FUNCTION(get_included_files)
ZEND_FUNCTION(trigger_error)
{
zend_long error_type = E_USER_NOTICE;
- char *message;
- size_t message_len;
+ zend_string *message;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &message, &message_len, &error_type) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|l", &message, &error_type) == FAILURE) {
RETURN_THROWS();
}
@@ -1128,7 +1127,7 @@ ZEND_FUNCTION(trigger_error)
break;
}
- zend_error((int)error_type, "%s", message);
+ zend_error_zstr_at(error_type, zend_get_executed_filename_ex(), zend_get_executed_lineno(), message);
// TODO Change to void
RETURN_TRUE;
}
diff --git a/Zend/zend_exceptions.c b/Zend/zend_exceptions.c
index 2b2f6dfb4b..8ad603e51e 100644
--- a/Zend/zend_exceptions.c
+++ b/Zend/zend_exceptions.c
@@ -950,9 +950,10 @@ ZEND_API ZEND_COLD zend_result zend_exception_error(zend_object *ex, int severit
file = zval_get_string(GET_PROPERTY_SILENT(&exception, ZEND_STR_FILE));
line = zval_get_long(GET_PROPERTY_SILENT(&exception, ZEND_STR_LINE));
+ ZVAL_STR(&tmp, str);
zend_error_va(severity | E_DONT_BAIL,
(file && ZSTR_LEN(file) > 0) ? file : NULL, line,
- "Uncaught %s\n thrown", ZSTR_VAL(str));
+ "Uncaught %Z\n thrown", &tmp);
zend_string_release_ex(str, 0);
zend_string_release_ex(file, 0);
diff --git a/main/main.c b/main/main.c
index b2d03b4af8..ee637f2651 100644
--- a/main/main.c
+++ b/main/main.c
@@ -1332,6 +1332,7 @@ static ZEND_COLD void php_error_cb(int orig_type, zend_string *error_filename, c
syslog(LOG_ALERT, "PHP %s: %s (%s)", error_type_str, ZSTR_VAL(message), GetCommandLine());
}
#endif
+ // XXX: Using %Z here doesn't work because the syslog function used underlying this doesn't support \0 in the message
spprintf(&log_buffer, 0, "PHP %s: %s in %s on line %" PRIu32, error_type_str, ZSTR_VAL(message), ZSTR_VAL(error_filename), error_lineno);
php_log_err_with_severity(log_buffer, syslog_type_int);
efree(log_buffer);
@@ -1350,19 +1351,25 @@ static ZEND_COLD void php_error_cb(int orig_type, zend_string *error_filename, c
php_printf("%s<br />\n<b>%s</b>: %s in <b>%s</b> on line <b>%" PRIu32 "</b><br />\n%s", STR_PRINT(prepend_string), error_type_str, ZSTR_VAL(buf), ZSTR_VAL(error_filename), error_lineno, STR_PRINT(append_string));
zend_string_free(buf);
} else {
- php_printf("%s<br />\n<b>%s</b>: %s in <b>%s</b> on line <b>%" PRIu32 "</b><br />\n%s", STR_PRINT(prepend_string), error_type_str, ZSTR_VAL(message), ZSTR_VAL(error_filename), error_lineno, STR_PRINT(append_string));
+ zval tmp;
+ ZVAL_STR(&tmp, message);
+ php_printf("%s<br />\n<b>%s</b>: %Z in <b>%s</b> on line <b>%" PRIu32 "</b><br />\n%s", STR_PRINT(prepend_string), error_type_str, &tmp, ZSTR_VAL(error_filename), error_lineno, STR_PRINT(append_string));
}
} else {
/* Write CLI/CGI errors to stderr if display_errors = "stderr" */
if ((!strcmp(sapi_module.name, "cli") || !strcmp(sapi_module.name, "cgi") || !strcmp(sapi_module.name, "phpdbg")) &&
PG(display_errors) == PHP_DISPLAY_ERRORS_STDERR
) {
- fprintf(stderr, "%s: %s in %s on line %" PRIu32 "\n", error_type_str, ZSTR_VAL(message), ZSTR_VAL(error_filename), error_lineno);
+ fprintf(stderr, "%s: ", error_type_str);
+ fwrite(ZSTR_VAL(message), ZSTR_LEN(message), 1, stderr);
+ fprintf(stderr, " in %s on line %" PRIu32 "\n", ZSTR_VAL(error_filename), error_lineno);
#ifdef PHP_WIN32
fflush(stderr);
#endif
} else {
- php_printf("%s\n%s: %s in %s on line %" PRIu32 "\n%s", STR_PRINT(prepend_string), error_type_str, ZSTR_VAL(message), ZSTR_VAL(error_filename), error_lineno, STR_PRINT(append_string));
+ zval tmp;
+ ZVAL_STR(&tmp, message);
+ php_printf("%s\n%s: %Z in %s on line %" PRIu32 "\n%s", STR_PRINT(prepend_string), error_type_str, &tmp, ZSTR_VAL(error_filename), error_lineno, STR_PRINT(append_string));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment