Skip to content
Open
6 changes: 6 additions & 0 deletions UPGRADING.INTERNALS
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ PHP 8.6 INTERNALS UPGRADE NOTES
- ext/mbstring:
. Added GB18030-2022 to default encoding list for zh-CN.

- ext/standard:
. _php_error_log() now has a formal return type of zend_result.
. _php_error_log() now accepts zend_string* values instead of char*.
. _php_error_log_ex() has been removed.
. php_mail()'s extra_cmd parameter is now a zend_string*.

========================
4. OpCode changes
========================
Expand Down
2 changes: 1 addition & 1 deletion ext/mbstring/mbstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -4703,7 +4703,7 @@ PHP_FUNCTION(mb_send_mail)
extra_cmd = php_escape_shell_cmd(extra_cmd);
}

RETVAL_BOOL(php_mail(to_r, ZSTR_VAL(subject), message, ZSTR_VAL(str_headers), extra_cmd ? ZSTR_VAL(extra_cmd) : NULL));
RETVAL_BOOL(php_mail(to_r, ZSTR_VAL(subject), message, ZSTR_VAL(str_headers), extra_cmd));

if (extra_cmd) {
zend_string_release_ex(extra_cmd, 0);
Expand Down
42 changes: 13 additions & 29 deletions ext/standard/basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -1330,42 +1330,30 @@ error options:
/* {{{ Send an error message somewhere */
PHP_FUNCTION(error_log)
{
char *message, *opt = NULL, *headers = NULL;
size_t message_len, opt_len = 0, headers_len = 0;
zend_string *message, *opt = NULL, *headers = NULL;
zend_long erropt = 0;

ZEND_PARSE_PARAMETERS_START(1, 4)
Z_PARAM_STRING(message, message_len)
Z_PARAM_STR(message)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(erropt)
Z_PARAM_PATH_OR_NULL(opt, opt_len)
Z_PARAM_STRING_OR_NULL(headers, headers_len)
Z_PARAM_PATH_STR_OR_NULL(opt)
Z_PARAM_STR_OR_NULL(headers)
ZEND_PARSE_PARAMETERS_END();

if (_php_error_log_ex((int) erropt, message, message_len, opt, headers) == FAILURE) {
RETURN_FALSE;
}

RETURN_TRUE;
}
/* }}} */

/* For BC (not binary-safe!) */
PHPAPI int _php_error_log(int opt_err, const char *message, const char *opt, const char *headers) /* {{{ */
{
return _php_error_log_ex(opt_err, message, (opt_err == 3) ? strlen(message) : 0, opt, headers);
RETURN_BOOL(_php_error_log((int) erropt, message, opt, headers) == SUCCESS);
}
/* }}} */

PHPAPI int _php_error_log_ex(int opt_err, const char *message, size_t message_len, const char *opt, const char *headers) /* {{{ */
PHPAPI zend_result _php_error_log(int opt_err, const zend_string *message, const zend_string *opt, const zend_string *headers) /* {{{ */
{
php_stream *stream = NULL;
size_t nbytes;

switch (opt_err)
{
case 1: /*send an email */
if (!php_mail(opt, "PHP error_log message", message, headers, NULL)) {
if (!php_mail(ZSTR_VAL(opt), "PHP error_log message", ZSTR_VAL(message), ZSTR_VAL(headers), NULL)) {
return FAILURE;
}
break;
Expand All @@ -1375,27 +1363,27 @@ PHPAPI int _php_error_log_ex(int opt_err, const char *message, size_t message_le
return FAILURE;

case 3: /*save to a file */
stream = php_stream_open_wrapper(opt, "a", REPORT_ERRORS, NULL);
stream = php_stream_open_wrapper(ZSTR_VAL(opt), "a", REPORT_ERRORS, NULL);
if (!stream) {
return FAILURE;
}
nbytes = php_stream_write(stream, message, message_len);
nbytes = php_stream_write(stream, ZSTR_VAL(message), ZSTR_LEN(message));
php_stream_close(stream);
if (nbytes != message_len) {
if (nbytes != ZSTR_LEN(message)) {
return FAILURE;
}
break;

case 4: /* send to SAPI */
if (sapi_module.log_message) {
sapi_module.log_message(message, -1);
sapi_module.log_message(ZSTR_VAL(message), -1);
} else {
return FAILURE;
}
break;

default:
php_log_err_with_severity(message, LOG_NOTICE);
php_log_err_with_severity(ZSTR_VAL(message), LOG_NOTICE);
break;
}
return SUCCESS;
Expand Down Expand Up @@ -2322,11 +2310,7 @@ PHP_FUNCTION(is_uploaded_file)
RETURN_FALSE;
}

if (zend_hash_exists(SG(rfc1867_uploaded_files), path)) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
RETURN_BOOL(zend_hash_exists(SG(rfc1867_uploaded_files), path));
}
/* }}} */

Expand Down
4 changes: 1 addition & 3 deletions ext/standard/basic_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ PHP_MINIT_FUNCTION(user_filters);
PHP_RSHUTDOWN_FUNCTION(user_filters);
PHP_RSHUTDOWN_FUNCTION(browscap);

/* Left for BC (not binary safe!) */
PHPAPI int _php_error_log(int opt_err, const char *message, const char *opt, const char *headers);
PHPAPI int _php_error_log_ex(int opt_err, const char *message, size_t message_len, const char *opt, const char *headers);
PHPAPI zend_result _php_error_log(int opt_err, const zend_string *message, const zend_string *opt, const zend_string *headers);

typedef struct _php_basic_globals {
HashTable *user_shutdown_function_names;
Expand Down
67 changes: 30 additions & 37 deletions ext/standard/mail.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,20 @@ typedef enum {
CONTAINS_NULL
} php_mail_header_value_error_type;

static php_mail_header_value_error_type php_mail_build_headers_check_field_value(zval *val)
static php_mail_header_value_error_type php_mail_build_headers_check_field_value(const zend_string *value)
{
size_t len = 0;
zend_string *value = Z_STR_P(val);

/* https://tools.ietf.org/html/rfc2822#section-2.2.1 */
/* https://tools.ietf.org/html/rfc2822#section-2.2.3 */
while (len < value->len) {
if (*(value->val+len) == '\r') {
if (*(value->val+len+1) != '\n') {
while (len < ZSTR_LEN(value)) {
if (*(ZSTR_VAL(value)+len) == '\r') {
if (*(ZSTR_VAL(value)+len+1) != '\n') {
return CONTAINS_CR_ONLY;
}

if (value->len - len >= 3
&& (*(value->val+len+2) == ' ' || *(value->val+len+2) == '\t')) {
if (ZSTR_LEN(value) - len >= 3
&& (*(ZSTR_VAL(value)+len+2) == ' ' || *(ZSTR_VAL(value)+len+2) == '\t')) {
len += 3;
continue;
}
Expand All @@ -96,30 +95,29 @@ static php_mail_header_value_error_type php_mail_build_headers_check_field_value
* Therefore, considering such an environment, folding with LF alone
* is allowed.
*/
if (*(value->val+len) == '\n') {
if (value->len - len >= 2
&& (*(value->val+len+1) == ' ' || *(value->val+len+1) == '\t')) {
if (*(ZSTR_VAL(value)+len) == '\n') {
if (ZSTR_LEN(value) - len >= 2
&& (*(ZSTR_VAL(value)+len+1) == ' ' || *(ZSTR_VAL(value)+len+1) == '\t')) {
len += 2;
continue;
}
return CONTAINS_LF_ONLY;
}
if (*(value->val+len) == '\0') {
if (*(ZSTR_VAL(value)+len) == '\0') {
return CONTAINS_NULL;
}
len++;
}
return NO_HEADER_ERROR;
}


static bool php_mail_build_headers_check_field_name(zend_string *key)
static zend_result php_mail_build_headers_check_field_name(const zend_string *key)
{
size_t len = 0;

/* https://tools.ietf.org/html/rfc2822#section-2.2 */
while (len < key->len) {
if (*(key->val+len) < 33 || *(key->val+len) > 126 || *(key->val+len) == ':') {
while (len < ZSTR_LEN(key)) {
if (*(ZSTR_VAL(key)+len) < 33 || *(ZSTR_VAL(key)+len) > 126 || *(ZSTR_VAL(key)+len) == ':') {
return FAILURE;
}
len++;
Expand All @@ -128,9 +126,9 @@ static bool php_mail_build_headers_check_field_name(zend_string *key)
}


static void php_mail_build_headers_elems(smart_str *s, zend_string *key, zval *val);
static void php_mail_build_headers_elems(smart_str *s, const zend_string *key, zval *val);

static void php_mail_build_headers_elem(smart_str *s, zend_string *key, zval *val)
static void php_mail_build_headers_elem(smart_str *s, const zend_string *key, zval *val)
{
switch(Z_TYPE_P(val)) {
case IS_STRING:
Expand All @@ -139,7 +137,8 @@ static void php_mail_build_headers_elem(smart_str *s, zend_string *key, zval *va
return;
}

php_mail_header_value_error_type error_type = php_mail_build_headers_check_field_value(val);
zend_string *str_value = Z_STR_P(val);
php_mail_header_value_error_type error_type = php_mail_build_headers_check_field_value(str_value);
switch (error_type) {
case NO_HEADER_ERROR:
break;
Expand All @@ -162,7 +161,7 @@ static void php_mail_build_headers_elem(smart_str *s, zend_string *key, zval *va
}
smart_str_append(s, key);
smart_str_appendl(s, ": ", 2);
smart_str_appends(s, Z_STRVAL_P(val));
smart_str_append(s, str_value);
smart_str_appendl(s, "\r\n", 2);
break;
case IS_ARRAY:
Expand All @@ -174,7 +173,7 @@ static void php_mail_build_headers_elem(smart_str *s, zend_string *key, zval *va
}


static void php_mail_build_headers_elems(smart_str *s, zend_string *key, zval *val)
static void php_mail_build_headers_elems(smart_str *s, const zend_string *key, zval *val)
{
zend_string *tmp_key;
zval *tmp_val;
Expand Down Expand Up @@ -208,7 +207,7 @@ do { \
} \
} while(0)

PHPAPI zend_string *php_mail_build_headers(HashTable *headers)
PHPAPI zend_string *php_mail_build_headers(const HashTable *headers)
{
zend_ulong idx;
zend_string *key;
Expand Down Expand Up @@ -246,13 +245,7 @@ PHPAPI zend_string *php_mail_build_headers(HashTable *headers)
} else if (zend_string_equals_literal_ci(key, "subject")) {
zend_value_error("The additional headers cannot contain the \"Subject\" header");
} else {
if (Z_TYPE_P(val) == IS_STRING) {
php_mail_build_headers_elem(&s, key, val);
} else if (Z_TYPE_P(val) == IS_ARRAY) {
php_mail_build_headers_elems(&s, key, val);
} else {
zend_type_error("Header \"%s\" must be of type array|string, %s given", ZSTR_VAL(key), zend_zval_value_name(val));
}
php_mail_build_headers_elem(&s, key, val);
}

if (EG(exception)) {
Expand Down Expand Up @@ -350,7 +343,7 @@ PHP_FUNCTION(mail)
extra_cmd = php_escape_shell_cmd(extra_cmd);
}

if (php_mail(to_r, subject_r, message, headers_str && ZSTR_LEN(headers_str) ? ZSTR_VAL(headers_str) : NULL, extra_cmd ? ZSTR_VAL(extra_cmd) : NULL)) {
if (php_mail(to_r, subject_r, message, headers_str && ZSTR_LEN(headers_str) ? ZSTR_VAL(headers_str) : NULL, extra_cmd)) {
RETVAL_TRUE;
} else {
RETVAL_FALSE;
Expand Down Expand Up @@ -392,10 +385,10 @@ static void php_mail_log_to_syslog(char *message) {
}


static void php_mail_log_to_file(char *filename, char *message, size_t message_size) {
static void php_mail_log_to_file(const zend_string *filename, const char *message, size_t message_size) {
/* Write 'message' to the given file. */
uint32_t flags = REPORT_ERRORS | STREAM_DISABLE_OPEN_BASEDIR;
php_stream *stream = php_stream_open_wrapper(filename, "a", flags, NULL);
php_stream *stream = php_stream_open_wrapper(ZSTR_VAL(filename), "a", flags, NULL);
if (stream) {
php_stream_write(stream, message, message_size);
php_stream_close(stream);
Expand Down Expand Up @@ -441,12 +434,12 @@ static int php_mail_detect_multiple_crlf(const char *hdr) {


/* {{{ php_mail */
PHPAPI bool php_mail(const char *to, const char *subject, const char *message, const char *headers, const char *extra_cmd)
PHPAPI bool php_mail(const char *to, const char *subject, const char *message, const char *headers, const zend_string *extra_cmd)
{
FILE *sendmail;
char *sendmail_path = INI_STR("sendmail_path");
char *sendmail_cmd = NULL;
char *mail_log = INI_STR("mail.log");
const zend_string *mail_log = zend_ini_str(ZEND_STRL("mail.log"), false);
const char *hdr = headers;
char *ahdr = NULL;
#if PHP_SIGCHILD
Expand All @@ -459,7 +452,7 @@ PHPAPI bool php_mail(const char *to, const char *subject, const char *message, c
} \
return val; \

if (mail_log && *mail_log) {
if (mail_log && ZSTR_LEN(mail_log)) {
char *logline;

spprintf(&logline, 0, "mail() on [%s:%d]: To: %s -- Headers: %s -- Subject: %s", zend_get_executed_filename(), zend_get_executed_lineno(), to, hdr ? hdr : "", subject);
Expand All @@ -468,7 +461,7 @@ PHPAPI bool php_mail(const char *to, const char *subject, const char *message, c
php_mail_log_crlf_to_spaces(logline);
}

if (!strcmp(mail_log, "syslog")) {
if (zend_string_equals_literal(mail_log, "syslog")) {
php_mail_log_to_syslog(logline);
} else {
/* Add date when logging to file */
Expand All @@ -495,7 +488,7 @@ PHPAPI bool php_mail(const char *to, const char *subject, const char *message, c
MAIL_RET(false);
}

char *line_sep;
const char *line_sep;
zend_string *cr_lf_mode = PG(mail_cr_lf_mode);

if (cr_lf_mode && !zend_string_equals_literal(cr_lf_mode, "crlf")) {
Expand Down Expand Up @@ -558,7 +551,7 @@ PHPAPI bool php_mail(const char *to, const char *subject, const char *message, c
#endif
}
if (extra_cmd != NULL) {
spprintf(&sendmail_cmd, 0, "%s %s", sendmail_path, extra_cmd);
spprintf(&sendmail_cmd, 0, "%s %s", sendmail_path, ZSTR_VAL(extra_cmd));
} else {
sendmail_cmd = sendmail_path;
}
Expand Down
4 changes: 2 additions & 2 deletions ext/standard/php_mail.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

PHP_MINFO_FUNCTION(mail);

PHPAPI zend_string *php_mail_build_headers(HashTable *headers);
PHPAPI extern bool php_mail(const char *to, const char *subject, const char *message, const char *headers, const char *extra_cmd);
PHPAPI zend_string *php_mail_build_headers(const HashTable *headers);
PHPAPI extern bool php_mail(const char *to, const char *subject, const char *message, const char *headers, const zend_string *extra_cmd);

#endif /* PHP_MAIL_H */