Skip to content

Use strlen(str) instead of sizeof(str)-1 for directly declared strings #8336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Zend/Optimizer/block_pass.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,8 @@ static void zend_optimize_block(zend_basic_block *block, zend_op_array *op_array
zval *arg = &OPLINE_OP1_LITERAL(sv);
char *fname = FUNCTION_CACHE->funcs[Z_LVAL(ZEND_OP1_LITERAL(fcall))].function_name;
size_t flen = FUNCTION_CACHE->funcs[Z_LVAL(ZEND_OP1_LITERAL(fcall))].name_len;
if((flen == sizeof("function_exists")-1 && zend_binary_strcasecmp(fname, flen, "function_exists", sizeof("function_exists")-1) == 0) ||
(flen == sizeof("is_callable")-1 && zend_binary_strcasecmp(fname, flen, "is_callable", sizeof("is_callable")-1) == 0)
if((flen == strlen("function_exists") && zend_binary_strcasecmp(fname, flen, "function_exists", strlen("function_exists")) == 0) ||
(flen == strlen("is_callable") && zend_binary_strcasecmp(fname, flen, "is_callable", strlen("is_callable")) == 0)
) {
zend_function *function;
if((function = zend_hash_find_ptr(EG(function_table), Z_STR_P(arg))) != NULL) {
Expand All @@ -345,7 +345,7 @@ static void zend_optimize_block(zend_basic_block *block, zend_op_array *op_array
LITERAL_BOOL(opline->op1, 1);
opline->op1_type = IS_CONST;
}
} else if(flen == sizeof("constant")-1 && zend_binary_strcasecmp(fname, flen, "constant", sizeof("constant")-1) == 0) {
} else if(flen == strlen("constant") && zend_binary_strcasecmp(fname, flen, "constant", strlen("constant")) == 0) {
zval c;
if (zend_optimizer_get_persistent_constant(Z_STR_P(arg), &c, 1 ELS_CC)) {
literal_dtor(arg);
Expand All @@ -355,7 +355,7 @@ static void zend_optimize_block(zend_basic_block *block, zend_op_array *op_array
/* no copy ctor - get already copied it */
opline->op1_type = IS_CONST;
}
} else if(flen == sizeof("extension_loaded")-1 && zend_binary_strcasecmp(fname, flen, "extension_loaded", sizeof("extension_loaded")-1) == 0) {
} else if(flen == strlen("extension_loaded") && zend_binary_strcasecmp(fname, flen, "extension_loaded", strlen("extension_loaded")) == 0) {
if(zend_hash_exists(&module_registry, Z_STR_P(arg))) {
literal_dtor(arg);
MAKE_NOP(sv);
Expand Down
4 changes: 2 additions & 2 deletions Zend/Optimizer/pass1.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ void zend_optimizer_pass1(zend_op_array *op_array, zend_optimizer_ctx *ctx)
memset(&fake_execute_data, 0, sizeof(zend_execute_data));
fake_execute_data.func = (zend_function*)op_array;
EG(current_execute_data) = &fake_execute_data;
if ((offset = zend_get_constant_str("__COMPILER_HALT_OFFSET__", sizeof("__COMPILER_HALT_OFFSET__") - 1)) != NULL) {
if ((offset = zend_get_constant_str("__COMPILER_HALT_OFFSET__", strlen("__COMPILER_HALT_OFFSET__"))) != NULL) {

literal_dtor(&ZEND_OP2_LITERAL(opline));
replace_by_const_or_qm_assign(op_array, opline, offset);
Expand Down Expand Up @@ -232,7 +232,7 @@ void zend_optimizer_pass1(zend_op_array *op_array, zend_optimizer_ctx *ctx)
}

if (RESULT_UNUSED(opline) &&
!zend_memnstr(Z_STRVAL(ZEND_OP1_LITERAL(send1_opline)), "::", sizeof("::") - 1, Z_STRVAL(ZEND_OP1_LITERAL(send1_opline)) + Z_STRLEN(ZEND_OP1_LITERAL(send1_opline)))) {
!zend_memnstr(Z_STRVAL(ZEND_OP1_LITERAL(send1_opline)), "::", strlen("::"), Z_STRVAL(ZEND_OP1_LITERAL(send1_opline)) + Z_STRLEN(ZEND_OP1_LITERAL(send1_opline)))) {

opline->opcode = ZEND_DECLARE_CONST;
opline->op1_type = IS_CONST;
Expand Down
2 changes: 1 addition & 1 deletion Zend/zend.c
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ void zend_startup(zend_utility_functions *utility_functions) /* {{{ */
zend_interned_strings_init();
zend_startup_builtin_functions();
zend_register_standard_constants();
zend_register_auto_global(zend_string_init_interned("GLOBALS", sizeof("GLOBALS") - 1, 1), 1, php_auto_globals_create_globals);
zend_register_auto_global(zend_string_init_interned("GLOBALS", strlen("GLOBALS"), 1), 1, php_auto_globals_create_globals);

#ifndef ZTS
zend_init_rsrc_plist();
Expand Down
20 changes: 10 additions & 10 deletions Zend/zend_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -3738,7 +3738,7 @@ ZEND_API zend_string *zend_get_callable_name_ex(zval *callable, zend_object *obj
zend_class_entry *ce = Z_OBJCE_P(callable);
return zend_string_concat2(
ZSTR_VAL(ce->name), ZSTR_LEN(ce->name),
"::__invoke", sizeof("::__invoke") - 1);
"::__invoke", strlen("::__invoke"));
}
case IS_REFERENCE:
callable = Z_REFVAL_P(callable);
Expand Down Expand Up @@ -4832,7 +4832,7 @@ static zend_result get_default_via_ast(zval *default_value_zval, const char *def
zend_arena *ast_arena;

zend_string *code = zend_string_concat3(
"<?php ", sizeof("<?php ") - 1, default_value, strlen(default_value), ";", 1);
"<?php ", strlen("<?php "), default_value, strlen(default_value), ";", 1);

ast = zend_compile_string_to_ast(code, &ast_arena, ZSTR_EMPTY_ALLOC());
zend_string_release(code);
Expand Down Expand Up @@ -4886,16 +4886,16 @@ ZEND_API zend_result zend_get_default_from_internal_arg_info(
/* Avoid going through the full AST machinery for some simple and common cases. */
size_t default_value_len = strlen(default_value);
zend_ulong lval;
if (default_value_len == sizeof("null")-1
&& !memcmp(default_value, "null", sizeof("null")-1)) {
if (default_value_len == strlen("null")
&& !memcmp(default_value, "null", strlen("null"))) {
ZVAL_NULL(default_value_zval);
return SUCCESS;
} else if (default_value_len == sizeof("true")-1
&& !memcmp(default_value, "true", sizeof("true")-1)) {
} else if (default_value_len == strlen("true")
&& !memcmp(default_value, "true", strlen("true"))) {
ZVAL_TRUE(default_value_zval);
return SUCCESS;
} else if (default_value_len == sizeof("false")-1
&& !memcmp(default_value, "false", sizeof("false")-1)) {
} else if (default_value_len == strlen("false")
&& !memcmp(default_value, "false", strlen("false"))) {
ZVAL_FALSE(default_value_zval);
return SUCCESS;
} else if (default_value_len >= 2
Expand All @@ -4907,8 +4907,8 @@ ZEND_API zend_result zend_get_default_from_internal_arg_info(
ZVAL_STR(default_value_zval, str);
return SUCCESS;
}
} else if (default_value_len == sizeof("[]")-1
&& !memcmp(default_value, "[]", sizeof("[]")-1)) {
} else if (default_value_len == strlen("[]")
&& !memcmp(default_value, "[]", strlen("[]"))) {
ZVAL_EMPTY_ARRAY(default_value_zval);
return SUCCESS;
} else if (ZEND_HANDLE_NUMERIC_STR(default_value, default_value_len, lval)) {
Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -1631,7 +1631,7 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
break;
}
case ZEND_AST_CONSTANT_CLASS:
smart_str_appendl(str, "__CLASS__", sizeof("__CLASS__")-1);
smart_str_appendl(str, "__CLASS__", strlen("__CLASS__"));
break;
case ZEND_AST_ZNODE:
/* This AST kind is only used for temporary nodes during compilation */
Expand Down
4 changes: 2 additions & 2 deletions Zend/zend_attributes_arginfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static zend_class_entry *register_class_Attribute(void)

zval property_flags_default_value;
ZVAL_UNDEF(&property_flags_default_value);
zend_string *property_flags_name = zend_string_init("flags", sizeof("flags") - 1, 1);
zend_string *property_flags_name = zend_string_init("flags", strlen("flags"), 1);
zend_declare_typed_property(class_entry, property_flags_name, &property_flags_default_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_LONG));
zend_string_release(property_flags_name);

Expand Down Expand Up @@ -123,7 +123,7 @@ static zend_class_entry *register_class_SensitiveParameterValue(void)

zval property_value_default_value;
ZVAL_UNDEF(&property_value_default_value);
zend_string *property_value_name = zend_string_init("value", sizeof("value") - 1, 1);
zend_string *property_value_name = zend_string_init("value", strlen("value"), 1);
zend_declare_typed_property(class_entry, property_value_name, &property_value_default_value, ZEND_ACC_PRIVATE|ZEND_ACC_READONLY, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_ANY));
zend_string_release(property_value_name);

Expand Down
30 changes: 15 additions & 15 deletions Zend/zend_builtin_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ ZEND_FUNCTION(gc_enable)

ZEND_PARSE_PARAMETERS_NONE();

key = zend_string_init("zend.enable_gc", sizeof("zend.enable_gc")-1, 0);
zend_alter_ini_entry_chars(key, "1", sizeof("1")-1, ZEND_INI_USER, ZEND_INI_STAGE_RUNTIME);
key = zend_string_init("zend.enable_gc", strlen("zend.enable_gc"), 0);
zend_alter_ini_entry_chars(key, "1", strlen("1"), ZEND_INI_USER, ZEND_INI_STAGE_RUNTIME);
zend_string_release_ex(key, 0);
}
/* }}} */
Expand All @@ -125,8 +125,8 @@ ZEND_FUNCTION(gc_disable)

ZEND_PARSE_PARAMETERS_NONE();

key = zend_string_init("zend.enable_gc", sizeof("zend.enable_gc")-1, 0);
zend_alter_ini_entry_chars(key, "0", sizeof("0")-1, ZEND_INI_USER, ZEND_INI_STAGE_RUNTIME);
key = zend_string_init("zend.enable_gc", strlen("zend.enable_gc"), 0);
zend_alter_ini_entry_chars(key, "0", strlen("0"), ZEND_INI_USER, ZEND_INI_STAGE_RUNTIME);
zend_string_release_ex(key, 0);
}
/* }}} */
Expand All @@ -142,10 +142,10 @@ ZEND_FUNCTION(gc_status)

array_init_size(return_value, 3);

add_assoc_long_ex(return_value, "runs", sizeof("runs")-1, (long)status.runs);
add_assoc_long_ex(return_value, "collected", sizeof("collected")-1, (long)status.collected);
add_assoc_long_ex(return_value, "threshold", sizeof("threshold")-1, (long)status.threshold);
add_assoc_long_ex(return_value, "roots", sizeof("roots")-1, (long)status.num_roots);
add_assoc_long_ex(return_value, "runs", strlen("runs"), (long)status.runs);
add_assoc_long_ex(return_value, "collected", strlen("collected"), (long)status.collected);
add_assoc_long_ex(return_value, "threshold", strlen("threshold"), (long)status.threshold);
add_assoc_long_ex(return_value, "roots", strlen("roots"), (long)status.num_roots);
}
/* }}} */

Expand Down Expand Up @@ -481,7 +481,7 @@ ZEND_FUNCTION(define)
Z_PARAM_BOOL(non_cs)
ZEND_PARSE_PARAMETERS_END();

if (zend_memnstr(ZSTR_VAL(name), "::", sizeof("::") - 1, ZSTR_VAL(name) + ZSTR_LEN(name))) {
if (zend_memnstr(ZSTR_VAL(name), "::", strlen("::"), ZSTR_VAL(name) + ZSTR_LEN(name))) {
zend_argument_value_error(1, "cannot be a class constant");
RETURN_THROWS();
}
Expand Down Expand Up @@ -1323,8 +1323,8 @@ ZEND_FUNCTION(get_defined_functions)
}
} ZEND_HASH_FOREACH_END();

zend_hash_str_add_new(Z_ARRVAL_P(return_value), "internal", sizeof("internal")-1, &internal);
zend_hash_str_add_new(Z_ARRVAL_P(return_value), "user", sizeof("user")-1, &user);
zend_hash_str_add_new(Z_ARRVAL_P(return_value), "internal", strlen("internal"), &internal);
zend_hash_str_add_new(Z_ARRVAL_P(return_value), "user", strlen("user"), &user);
}
/* }}} */

Expand Down Expand Up @@ -1563,7 +1563,7 @@ static void debug_backtrace_get_args(zend_execute_data *call, zval *arg_array) /
zend_attribute *attribute = zend_get_parameter_attribute_str(
call->func->common.attributes,
"sensitiveparameter",
sizeof("sensitiveparameter") - 1,
strlen("sensitiveparameter"),
i
);

Expand Down Expand Up @@ -1595,7 +1595,7 @@ static void debug_backtrace_get_args(zend_execute_data *call, zval *arg_array) /
zend_attribute *attribute = zend_get_parameter_attribute_str(
call->func->common.attributes,
"sensitiveparameter",
sizeof("sensitiveparameter") - 1,
strlen("sensitiveparameter"),
i
);
bool is_sensitive = attribute != NULL;
Expand Down Expand Up @@ -1634,7 +1634,7 @@ static void debug_backtrace_get_args(zend_execute_data *call, zval *arg_array) /
zend_attribute *attribute = zend_get_parameter_attribute_str(
call->func->common.attributes,
"sensitiveparameter",
sizeof("sensitiveparameter") - 1,
strlen("sensitiveparameter"),
MIN(i, call->func->common.num_args)
);
is_sensitive = attribute != NULL;
Expand Down Expand Up @@ -1953,7 +1953,7 @@ ZEND_FUNCTION(get_extension_funcs)
module = zend_hash_find_ptr(&module_registry, lcname);
zend_string_release_ex(lcname, 0);
} else {
module = zend_hash_str_find_ptr(&module_registry, "core", sizeof("core") - 1);
module = zend_hash_str_find_ptr(&module_registry, "core", strlen("core"));
}

if (!module) {
Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_closures.c
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ static HashTable *zend_closure_get_debug_info(zend_object *object, int *is_temp)
zend_string_release_ex(name, 0);
arg_info++;
}
zend_hash_str_update(debug_info, "parameter", sizeof("parameter")-1, &val);
zend_hash_str_update(debug_info, "parameter", strlen("parameter"), &val);
}

return debug_info;
Expand Down
10 changes: 5 additions & 5 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ uint32_t zend_add_member_modifier(uint32_t flags, uint32_t new_flag) /* {{{ */
ZEND_API zend_string *zend_create_member_string(zend_string *class_name, zend_string *member_name) {
return zend_string_concat3(
ZSTR_VAL(class_name), ZSTR_LEN(class_name),
"::", sizeof("::") - 1,
"::", strlen("::"),
ZSTR_VAL(member_name), ZSTR_LEN(member_name));
}

Expand Down Expand Up @@ -1852,7 +1852,7 @@ zend_ast *zend_negate_num_string(zend_ast *ast) /* {{{ */
zval *zv = zend_ast_get_zval(ast);
if (Z_TYPE_P(zv) == IS_LONG) {
if (Z_LVAL_P(zv) == 0) {
ZVAL_NEW_STR(zv, zend_string_init("-0", sizeof("-0")-1, 0));
ZVAL_NEW_STR(zv, zend_string_init("-0", strlen("-0"), 0));
} else {
ZEND_ASSERT(Z_LVAL_P(zv) > 0);
Z_LVAL_P(zv) *= -1;
Expand Down Expand Up @@ -4033,7 +4033,7 @@ static void zend_compile_assert(znode *result, zend_ast_list *args, zend_string
/* If the original argument was named, add the new argument as named as well,
* as mixing named and positional is not allowed. */
zend_ast *name = zend_ast_create_zval_from_str(
zend_string_init("description", sizeof("description") - 1, 0));
zend_string_init("description", strlen("description"), 0));
arg = zend_ast_create(ZEND_AST_NAMED_ARG, name, arg);
}
zend_ast_list_add((zend_ast *) args, arg);
Expand Down Expand Up @@ -6963,9 +6963,9 @@ static void add_stringable_interface(zend_class_entry *ce) {
erealloc(ce->interface_names, sizeof(zend_class_name) * ce->num_interfaces);
// TODO: Add known interned strings instead?
ce->interface_names[ce->num_interfaces - 1].name =
zend_string_init("Stringable", sizeof("Stringable") - 1, 0);
zend_string_init("Stringable", strlen("Stringable"), 0);
ce->interface_names[ce->num_interfaces - 1].lc_name =
zend_string_init("stringable", sizeof("stringable") - 1, 0);
zend_string_init("stringable", strlen("stringable"), 0);
}

static zend_string *zend_begin_method_decl(zend_op_array *op_array, zend_string *name, bool has_body) /* {{{ */
Expand Down
12 changes: 6 additions & 6 deletions Zend/zend_constants.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ void zend_register_standard_constants(void)
REGISTER_MAIN_BOOL_CONSTANT("FALSE", 0, CONST_PERSISTENT);
REGISTER_MAIN_NULL_CONSTANT("NULL", CONST_PERSISTENT);

true_const = zend_hash_str_find_ptr(EG(zend_constants), "TRUE", sizeof("TRUE")-1);
false_const = zend_hash_str_find_ptr(EG(zend_constants), "FALSE", sizeof("FALSE")-1);
null_const = zend_hash_str_find_ptr(EG(zend_constants), "NULL", sizeof("NULL")-1);
true_const = zend_hash_str_find_ptr(EG(zend_constants), "TRUE", strlen("TRUE"));
false_const = zend_hash_str_find_ptr(EG(zend_constants), "FALSE", strlen("FALSE"));
null_const = zend_hash_str_find_ptr(EG(zend_constants), "NULL", strlen("NULL"));
}


Expand Down Expand Up @@ -213,8 +213,8 @@ static zend_constant *zend_get_halt_offset_constant(const char *name, size_t nam

if (!EG(current_execute_data)) {
return NULL;
} else if (name_len == sizeof("__COMPILER_HALT_OFFSET__")-1 &&
!memcmp(name, "__COMPILER_HALT_OFFSET__", sizeof("__COMPILER_HALT_OFFSET__")-1)) {
} else if (name_len == strlen("__COMPILER_HALT_OFFSET__") &&
!memcmp(name, "__COMPILER_HALT_OFFSET__", strlen("__COMPILER_HALT_OFFSET__"))) {
const char *cfilename;
zend_string *haltname;
size_t clen;
Expand All @@ -223,7 +223,7 @@ static zend_constant *zend_get_halt_offset_constant(const char *name, size_t nam
clen = strlen(cfilename);
/* check for __COMPILER_HALT_OFFSET__ */
haltname = zend_mangle_property_name(haltoff,
sizeof("__COMPILER_HALT_OFFSET__") - 1, cfilename, clen, 0);
strlen("__COMPILER_HALT_OFFSET__"), cfilename, clen, 0);
c = zend_hash_find_ptr(EG(zend_constants), haltname);
zend_string_efree(haltname);
return c;
Expand Down
4 changes: 2 additions & 2 deletions Zend/zend_enum.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,11 @@ void zend_enum_add_interfaces(zend_class_entry *ce)
ce->interface_names = erealloc(ce->interface_names, sizeof(zend_class_name) * ce->num_interfaces);

ce->interface_names[num_interfaces_before].name = zend_string_copy(zend_ce_unit_enum->name);
ce->interface_names[num_interfaces_before].lc_name = zend_string_init("unitenum", sizeof("unitenum") - 1, 0);
ce->interface_names[num_interfaces_before].lc_name = zend_string_init("unitenum", strlen("unitenum"), 0);

if (ce->enum_backing_type != IS_UNDEF) {
ce->interface_names[num_interfaces_before + 1].name = zend_string_copy(zend_ce_backed_enum->name);
ce->interface_names[num_interfaces_before + 1].lc_name = zend_string_init("backedenum", sizeof("backedenum") - 1, 0);
ce->interface_names[num_interfaces_before + 1].lc_name = zend_string_init("backedenum", strlen("backedenum"), 0);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ ZEND_METHOD(Exception, __toString)
str = ZSTR_EMPTY_ALLOC();

exception = ZEND_THIS;
fname = zend_string_init("gettraceasstring", sizeof("gettraceasstring")-1, 0);
fname = zend_string_init("gettraceasstring", strlen("gettraceasstring"), 0);

while (exception && Z_TYPE_P(exception) == IS_OBJECT && instanceof_function(Z_OBJCE_P(exception), zend_ce_throwable)) {
zend_string *prev_str = str;
Expand Down
Loading