Skip to content

Commit 695396f

Browse files
committed
Avoid double formatting in php_error_docref
Export a zend_error_zstr API for that purpose.
1 parent 95c67c6 commit 695396f

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

Zend/zend.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,9 +1259,8 @@ ZEND_API zval *zend_get_configuration_directive(zend_string *name) /* {{{ */
12591259
} \
12601260
} while (0)
12611261

1262-
static ZEND_COLD void zend_error_va_list(
1263-
int orig_type, const char *error_filename, uint32_t error_lineno,
1264-
const char *format, va_list args)
1262+
static ZEND_COLD void zend_error_impl(
1263+
int orig_type, const char *error_filename, uint32_t error_lineno, zend_string *message)
12651264
{
12661265
zval params[4];
12671266
zval retval;
@@ -1272,7 +1271,6 @@ static ZEND_COLD void zend_error_va_list(
12721271
zend_stack delayed_oplines_stack;
12731272
zend_class_entry *orig_fake_scope;
12741273
int type = orig_type & E_ALL;
1275-
zend_string *message = zend_vstrpprintf(0, format, args);
12761274

12771275
/* Report about uncaught exception in case of fatal errors */
12781276
if (EG(exception)) {
@@ -1402,10 +1400,17 @@ static ZEND_COLD void zend_error_va_list(
14021400
EG(exit_status) = 255;
14031401
}
14041402
}
1403+
}
1404+
/* }}} */
14051405

1406+
static ZEND_COLD void zend_error_va_list(
1407+
int orig_type, const char *error_filename, uint32_t error_lineno,
1408+
const char *format, va_list args)
1409+
{
1410+
zend_string *message = zend_vstrpprintf(0, format, args);
1411+
zend_error_impl(orig_type, error_filename, error_lineno, message);
14061412
zend_string_release(message);
14071413
}
1408-
/* }}} */
14091414

14101415
static ZEND_COLD void get_filename_lineno(int type, const char **filename, uint32_t *lineno) {
14111416
/* Obtain relevant filename and lineno */
@@ -1495,7 +1500,6 @@ ZEND_API ZEND_COLD ZEND_NORETURN void zend_error_at_noreturn(
14951500
/* Should never reach this. */
14961501
abort();
14971502
}
1498-
/* }}} */
14991503

15001504
ZEND_API ZEND_COLD ZEND_NORETURN void zend_error_noreturn(int type, const char *format, ...)
15011505
{
@@ -1510,7 +1514,13 @@ ZEND_API ZEND_COLD ZEND_NORETURN void zend_error_noreturn(int type, const char *
15101514
/* Should never reach this. */
15111515
abort();
15121516
}
1513-
/* }}} */
1517+
1518+
ZEND_API ZEND_COLD void zend_error_zstr(int type, zend_string *message) {
1519+
const char *filename;
1520+
uint32_t lineno;
1521+
get_filename_lineno(type, &filename, &lineno);
1522+
zend_error_impl(type, filename, lineno, message);
1523+
}
15141524

15151525
ZEND_API ZEND_COLD void zend_throw_error(zend_class_entry *exception_ce, const char *format, ...) /* {{{ */
15161526
{

Zend/zend.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ ZEND_API ZEND_COLD ZEND_NORETURN void zend_error_noreturn(int type, const char *
300300
/* If filename is NULL the default filename is used. */
301301
ZEND_API ZEND_COLD void zend_error_at(int type, const char *filename, uint32_t lineno, const char *format, ...) ZEND_ATTRIBUTE_FORMAT(printf, 4, 5);
302302
ZEND_API ZEND_COLD ZEND_NORETURN void zend_error_at_noreturn(int type, const char *filename, uint32_t lineno, const char *format, ...) ZEND_ATTRIBUTE_FORMAT(printf, 4, 5);
303+
ZEND_API ZEND_COLD void zend_error_zstr(int type, zend_string *message);
303304

304305
ZEND_API ZEND_COLD void zend_throw_error(zend_class_entry *exception_ce, const char *format, ...) ZEND_ATTRIBUTE_FORMAT(printf, 2, 3);
305306
ZEND_API ZEND_COLD void zend_type_error(const char *format, ...) ZEND_ATTRIBUTE_FORMAT(printf, 1, 2);

main/main.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,7 @@ PHPAPI ZEND_COLD void php_verror(const char *docref, const char *params, int typ
960960
const char *function;
961961
int origin_len;
962962
char *origin;
963-
char *message;
963+
zend_string *message;
964964
int is_function = 0;
965965

966966
/* get error text into buffer and escape for html if necessary */
@@ -1095,15 +1095,15 @@ PHPAPI ZEND_COLD void php_verror(const char *docref, const char *params, int typ
10951095
}
10961096
/* display html formatted or only show the additional links */
10971097
if (PG(html_errors)) {
1098-
spprintf(&message, 0, "%s [<a href='%s%s%s'>%s</a>]: %s", origin, docref_root, docref, docref_target, docref, buffer);
1098+
message = zend_strpprintf(0, "%s [<a href='%s%s%s'>%s</a>]: %s", origin, docref_root, docref, docref_target, docref, buffer);
10991099
} else {
1100-
spprintf(&message, 0, "%s [%s%s%s]: %s", origin, docref_root, docref, docref_target, buffer);
1100+
message = zend_strpprintf(0, "%s [%s%s%s]: %s", origin, docref_root, docref, docref_target, buffer);
11011101
}
11021102
if (target) {
11031103
efree(target);
11041104
}
11051105
} else {
1106-
spprintf(&message, 0, "%s: %s", origin, buffer);
1106+
message = zend_strpprintf(0, "%s: %s", origin, buffer);
11071107
}
11081108
if (replace_origin) {
11091109
zend_string_free(replace_origin);
@@ -1120,8 +1120,8 @@ PHPAPI ZEND_COLD void php_verror(const char *docref, const char *params, int typ
11201120
efree(buffer);
11211121
}
11221122

1123-
php_error(type, "%s", message);
1124-
efree(message);
1123+
zend_error_zstr(type, message);
1124+
zend_string_release(message);
11251125
}
11261126
/* }}} */
11271127

0 commit comments

Comments
 (0)