Skip to content

Commit d7bfdcc

Browse files
committed
zend_string: Support NUL bytes in ZSTR_*_LITERAL() and zend_string_*literal*()
1 parent 948ab10 commit d7bfdcc

File tree

7 files changed

+33
-5
lines changed

7 files changed

+33
-5
lines changed

UPGRADING.INTERNALS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ PHP 8.5 INTERNALS UPGRADE NOTES
8686
. zend_mm_refresh_key_child() must be called on any zend_mm_heap inherited
8787
from the parent process after a fork().
8888
. HASH_KEY_IS_* constants have been moved in the zend_hash_key_type enum.
89+
. ZSTR_INIT_LITERAL(), zend_string_starts_with_literal(), and
90+
zend_string_starts_with_literal_ci now support strings containing NUL
91+
bytes. Passing non-literal char* is no longer supported.
8992

9093
- standard
9194
. ext/standard/php_smart_string.h and ext/standard/php_smart_string_public.h

Zend/zend_string.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ END_EXTERN_C()
123123

124124
#define ZSTR_ALLOCA_FREE(str, use_heap) free_alloca(str, use_heap)
125125

126-
#define ZSTR_INIT_LITERAL(s, persistent) (zend_string_init((s), strlen(s), (persistent)))
126+
#define ZSTR_INIT_LITERAL(s, persistent) (zend_string_init(("" s), sizeof(s) - 1, (persistent)))
127127

128128
/*---*/
129129

@@ -402,7 +402,7 @@ static zend_always_inline bool zend_string_starts_with(const zend_string *str, c
402402
}
403403

404404
#define zend_string_starts_with_literal(str, prefix) \
405-
zend_string_starts_with_cstr(str, prefix, strlen(prefix))
405+
zend_string_starts_with_cstr(str, "" prefix, sizeof(prefix) - 1)
406406

407407
static zend_always_inline bool zend_string_starts_with_cstr_ci(const zend_string *str, const char *prefix, size_t prefix_length)
408408
{
@@ -415,7 +415,7 @@ static zend_always_inline bool zend_string_starts_with_ci(const zend_string *str
415415
}
416416

417417
#define zend_string_starts_with_literal_ci(str, prefix) \
418-
zend_string_starts_with_cstr_ci(str, prefix, strlen(prefix))
418+
zend_string_starts_with_cstr_ci(str, "" prefix, sizeof(prefix) - 1)
419419

420420
/*
421421
* DJBX33A (Daniel J. Bernstein, Times 33 with Addition)

ext/dom/element.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ zend_result dom_element_class_name_write(dom_object *obj, zval *newval)
180180
zval *dom_get_prop_checked_offset(dom_object *obj, uint32_t offset, const char *name)
181181
{
182182
#if ZEND_DEBUG
183-
zend_string *name_zstr = ZSTR_INIT_LITERAL(name, false);
183+
zend_string *name_zstr = zend_string_init(name, strlen(name), false);
184184
const zend_property_info *prop_info = zend_get_property_info(obj->std.ce, name_zstr, 0);
185185
zend_string_release_ex(name_zstr, false);
186186
ZEND_ASSERT(OBJ_PROP_TO_NUM(prop_info->offset) == offset);

ext/zend_test/test.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,13 @@ static ZEND_FUNCTION(zend_test_zend_ini_str)
593593
RETURN_STR(ZT_G(str_test));
594594
}
595595

596+
static ZEND_FUNCTION(zend_test_zstr_init_literal)
597+
{
598+
ZEND_PARSE_PARAMETERS_NONE();
599+
600+
RETURN_STR(ZSTR_INIT_LITERAL("foo\0bar", false));
601+
}
602+
596603
static ZEND_FUNCTION(zend_test_is_string_marked_as_valid_utf8)
597604
{
598605
zend_string *str;

ext/zend_test/test.stub.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ function zend_test_zend_ini_parse_quantity(string $str): int {}
301301
function zend_test_zend_ini_parse_uquantity(string $str): int {}
302302

303303
function zend_test_zend_ini_str(): string {}
304+
function zend_test_zstr_init_literal(): string {}
304305

305306
#ifdef ZEND_CHECK_STACK_LIMIT
306307
function zend_test_zend_call_stack_get(): ?array {}

ext/zend_test/test_arginfo.h

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
zstr_init_literal
3+
--EXTENSIONS--
4+
zend_test
5+
--FILE--
6+
<?php
7+
8+
var_dump(strlen(zend_test_zstr_init_literal()), bin2hex(zend_test_zstr_init_literal()));
9+
10+
?>
11+
--EXPECT--
12+
int(7)
13+
string(14) "666f6f00626172"

0 commit comments

Comments
 (0)