Skip to content

Commit 105c1e9

Browse files
authored
tree: use zend_str_has_nul_byte() API (#19336)
1 parent 2c4d4a6 commit 105c1e9

File tree

10 files changed

+20
-25
lines changed

10 files changed

+20
-25
lines changed

Zend/zend_API.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2326,7 +2326,7 @@ static zend_always_inline bool zend_parse_arg_string(zval *arg, char **dest, siz
23262326
static zend_always_inline bool zend_parse_arg_path_str(zval *arg, zend_string **dest, bool check_null, uint32_t arg_num)
23272327
{
23282328
if (!zend_parse_arg_str(arg, dest, check_null, arg_num) ||
2329-
(*dest && UNEXPECTED(CHECK_NULL_PATH(ZSTR_VAL(*dest), ZSTR_LEN(*dest))))) {
2329+
(*dest && UNEXPECTED(zend_str_has_nul_byte(*dest)))) {
23302330
return 0;
23312331
}
23322332
return 1;

Zend/zend_execute.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5211,7 +5211,7 @@ static zend_never_inline zend_op_array* ZEND_FASTCALL zend_include_or_eval(zval
52115211
}
52125212
} else if (UNEXPECTED(EG(exception))) {
52135213
break;
5214-
} else if (UNEXPECTED(strlen(ZSTR_VAL(inc_filename)) != ZSTR_LEN(inc_filename))) {
5214+
} else if (UNEXPECTED(zend_str_has_nul_byte(inc_filename))) {
52155215
zend_message_dispatcher(
52165216
(type == ZEND_INCLUDE_ONCE) ?
52175217
ZMSG_FAILED_INCLUDE_FOPEN : ZMSG_FAILED_REQUIRE_FOPEN,
@@ -5245,7 +5245,7 @@ static zend_never_inline zend_op_array* ZEND_FASTCALL zend_include_or_eval(zval
52455245
break;
52465246
case ZEND_INCLUDE:
52475247
case ZEND_REQUIRE:
5248-
if (UNEXPECTED(strlen(ZSTR_VAL(inc_filename)) != ZSTR_LEN(inc_filename))) {
5248+
if (UNEXPECTED(zend_str_has_nul_byte(inc_filename))) {
52495249
zend_message_dispatcher(
52505250
(type == ZEND_INCLUDE) ?
52515251
ZMSG_FAILED_INCLUDE_FOPEN : ZMSG_FAILED_REQUIRE_FOPEN,

ext/mbstring/mbstring.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4506,7 +4506,7 @@ PHP_FUNCTION(mb_send_mail)
45064506
ZEND_PARSE_PARAMETERS_END();
45074507

45084508
if (str_headers) {
4509-
if (strlen(ZSTR_VAL(str_headers)) != ZSTR_LEN(str_headers)) {
4509+
if (UNEXPECTED(zend_str_has_nul_byte(str_headers))) {
45104510
zend_argument_value_error(4, "must not contain any null bytes");
45114511
RETURN_THROWS();
45124512
}

ext/odbc/php_odbc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,7 @@ PHP_FUNCTION(odbc_execute)
10621062
ZSTR_VAL(tmpstr)[0] == '\'' &&
10631063
ZSTR_VAL(tmpstr)[ZSTR_LEN(tmpstr) - 1] == '\'') {
10641064

1065-
if (ZSTR_LEN(tmpstr) != strlen(ZSTR_VAL(tmpstr))) {
1065+
if (UNEXPECTED(zend_str_has_nul_byte(tmpstr))) {
10661066
odbc_release_params(result, params);
10671067
RETURN_FALSE;
10681068
}

ext/standard/exec.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -199,31 +199,26 @@ PHPAPI int php_exec(int type, const char *cmd, zval *array, zval *return_value)
199199

200200
static void php_exec_ex(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{ */
201201
{
202-
char *cmd;
203-
size_t cmd_len;
202+
zend_string *cmd;
204203
zval *ret_code=NULL, *ret_array=NULL;
205204
int ret;
206205

207206
ZEND_PARSE_PARAMETERS_START(1, (mode ? 2 : 3))
208-
Z_PARAM_STRING(cmd, cmd_len)
207+
Z_PARAM_PATH_STR(cmd)
209208
Z_PARAM_OPTIONAL
210209
if (!mode) {
211210
Z_PARAM_ZVAL(ret_array)
212211
}
213212
Z_PARAM_ZVAL(ret_code)
214213
ZEND_PARSE_PARAMETERS_END();
215214

216-
if (!cmd_len) {
215+
if (UNEXPECTED(!ZSTR_LEN(cmd))) {
217216
zend_argument_must_not_be_empty_error(1);
218217
RETURN_THROWS();
219218
}
220-
if (strlen(cmd) != cmd_len) {
221-
zend_argument_value_error(1, "must not contain any null bytes");
222-
RETURN_THROWS();
223-
}
224219

225220
if (!ret_array) {
226-
ret = php_exec(mode, cmd, NULL, return_value);
221+
ret = php_exec(mode, ZSTR_VAL(cmd), NULL, return_value);
227222
} else {
228223
if (Z_TYPE_P(Z_REFVAL_P(ret_array)) == IS_ARRAY) {
229224
ZVAL_DEREF(ret_array);
@@ -235,7 +230,7 @@ static void php_exec_ex(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{ */
235230
}
236231
}
237232

238-
ret = php_exec(2, cmd, ret_array, return_value);
233+
ret = php_exec(2, ZSTR_VAL(cmd), ret_array, return_value);
239234
}
240235
if (ret_code) {
241236
ZEND_TRY_ASSIGN_REF_LONG(ret_code, ret);
@@ -280,7 +275,7 @@ PHPAPI zend_string *php_escape_shell_cmd(const zend_string *unescaped_cmd)
280275
char *p = NULL;
281276
#endif
282277

283-
ZEND_ASSERT(ZSTR_LEN(unescaped_cmd) == strlen(ZSTR_VAL(unescaped_cmd)) && "Must be a binary safe string");
278+
ZEND_ASSERT(!zend_str_has_nul_byte(unescaped_cmd) && "Must be a binary safe string");
284279
size_t l = ZSTR_LEN(unescaped_cmd);
285280
const char *str = ZSTR_VAL(unescaped_cmd);
286281

@@ -387,7 +382,7 @@ PHPAPI zend_string *php_escape_shell_arg(const zend_string *unescaped_arg)
387382
size_t x, y = 0;
388383
zend_string *cmd;
389384

390-
ZEND_ASSERT(ZSTR_LEN(unescaped_arg) == strlen(ZSTR_VAL(unescaped_arg)) && "Must be a binary safe string");
385+
ZEND_ASSERT(!zend_str_has_nul_byte(unescaped_arg) && "Must be a binary safe string");
391386
size_t l = ZSTR_LEN(unescaped_arg);
392387
const char *str = ZSTR_VAL(unescaped_arg);
393388

ext/standard/filestat.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ PHPAPI void php_stat(zend_string *filename, int type, zval *return_value)
749749
php_stream_wrapper *wrapper = NULL;
750750

751751
if (IS_ACCESS_CHECK(type)) {
752-
if (!ZSTR_LEN(filename) || CHECK_NULL_PATH(ZSTR_VAL(filename), ZSTR_LEN(filename))) {
752+
if (!ZSTR_LEN(filename) || zend_str_has_nul_byte(filename)) {
753753
if (ZSTR_LEN(filename) && !IS_EXISTS_CHECK(type)) {
754754
php_error_docref(NULL, E_WARNING, "Filename contains null byte");
755755
}
@@ -821,7 +821,7 @@ PHPAPI void php_stat(zend_string *filename, int type, zval *return_value)
821821
}
822822

823823
if (!wrapper) {
824-
if (!ZSTR_LEN(filename) || CHECK_NULL_PATH(ZSTR_VAL(filename), ZSTR_LEN(filename))) {
824+
if (!ZSTR_LEN(filename) || zend_str_has_nul_byte(filename)) {
825825
if (ZSTR_LEN(filename) && !IS_EXISTS_CHECK(type)) {
826826
php_error_docref(NULL, E_WARNING, "Filename contains null byte");
827827
}

ext/standard/image.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1618,7 +1618,7 @@ static void php_getimagesize_from_any(INTERNAL_FUNCTION_PARAMETERS, int mode) {
16181618
Z_PARAM_ZVAL(info)
16191619
ZEND_PARSE_PARAMETERS_END();
16201620

1621-
if (mode == FROM_PATH && CHECK_NULL_PATH(ZSTR_VAL(input), ZSTR_LEN(input))) {
1621+
if (mode == FROM_PATH && zend_str_has_nul_byte(input)) {
16221622
zend_argument_value_error(1, "must not contain any null bytes");
16231623
RETURN_THROWS();
16241624
}

ext/standard/mail.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ PHP_FUNCTION(mail)
290290
ZEND_PARSE_PARAMETERS_END();
291291

292292
if (headers_str) {
293-
if (strlen(ZSTR_VAL(headers_str)) != ZSTR_LEN(headers_str)) {
293+
if (UNEXPECTED(zend_str_has_nul_byte(headers_str))) {
294294
zend_argument_value_error(4, "must not contain any null bytes");
295295
RETURN_THROWS();
296296
}

ext/xsl/xsltprocessor.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ PHP_METHOD(XSLTProcessor, setParameter)
609609
RETURN_THROWS();
610610
}
611611

612-
if (UNEXPECTED(CHECK_NULL_PATH(ZSTR_VAL(string_key), ZSTR_LEN(string_key)))) {
612+
if (UNEXPECTED(zend_str_has_nul_byte(string_key))) {
613613
zend_argument_value_error(3, "must not contain keys with any null bytes");
614614
RETURN_THROWS();
615615
}
@@ -625,7 +625,7 @@ PHP_METHOD(XSLTProcessor, setParameter)
625625
RETURN_THROWS();
626626
}
627627

628-
if (UNEXPECTED(CHECK_NULL_PATH(ZSTR_VAL(str), ZSTR_LEN(str)))) {
628+
if (UNEXPECTED(zend_str_has_nul_byte(str))) {
629629
zend_string_release(str);
630630
zend_string_release_ex(ht_key, false);
631631
zend_argument_value_error(3, "must not contain values with any null bytes");
@@ -643,7 +643,7 @@ PHP_METHOD(XSLTProcessor, setParameter)
643643
RETURN_THROWS();
644644
}
645645

646-
if (UNEXPECTED(CHECK_NULL_PATH(ZSTR_VAL(name), ZSTR_LEN(name)))) {
646+
if (UNEXPECTED(zend_str_has_nul_byte(name))) {
647647
zend_argument_value_error(2, "must not contain any null bytes");
648648
RETURN_THROWS();
649649
}

main/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ static PHP_INI_MH(OnUpdateMailLog)
707707
static PHP_INI_MH(OnChangeMailForceExtra)
708708
{
709709
/* Check that INI setting does not have any nul bytes */
710-
if (new_value && ZSTR_LEN(new_value) != strlen(ZSTR_VAL(new_value))) {
710+
if (new_value && zend_str_has_nul_byte(new_value)) {
711711
/* TODO Emit warning? */
712712
return FAILURE;
713713
}

0 commit comments

Comments
 (0)