Skip to content

tree-wide: Do not manually check for NULL before calling free() #19063

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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: 2 additions & 6 deletions Zend/Optimizer/zend_optimizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1208,9 +1208,7 @@ static void zend_redo_pass_two(zend_op_array *op_array)
efree(op_array->literals);
op_array->literals = (zval*)(((char*)op_array->opcodes) + ZEND_MM_ALIGNED_SIZE_EX(sizeof(zend_op) * op_array->last, 16));
} else {
if (op_array->literals) {
efree(op_array->literals);
}
efree(op_array->literals);
op_array->literals = NULL;
}
#endif
Expand Down Expand Up @@ -1318,9 +1316,7 @@ static void zend_redo_pass_two_ex(zend_op_array *op_array, zend_ssa *ssa)
efree(op_array->literals);
op_array->literals = (zval*)(((char*)op_array->opcodes) + ZEND_MM_ALIGNED_SIZE_EX(sizeof(zend_op) * op_array->last, 16));
} else {
if (op_array->literals) {
efree(op_array->literals);
}
efree(op_array->literals);
op_array->literals = NULL;
}
#endif
Expand Down
20 changes: 6 additions & 14 deletions Zend/zend.c
Original file line number Diff line number Diff line change
Expand Up @@ -790,10 +790,8 @@ static void compiler_globals_dtor(zend_compiler_globals *compiler_globals) /* {{
compiler_globals->map_ptr_base = ZEND_MAP_PTR_BIASED_BASE(NULL);
compiler_globals->map_ptr_size = 0;
}
if (compiler_globals->internal_run_time_cache) {
pefree(compiler_globals->internal_run_time_cache, 1);
compiler_globals->internal_run_time_cache = NULL;
}
pefree(compiler_globals->internal_run_time_cache, 1);
compiler_globals->internal_run_time_cache = NULL;
}
/* }}} */

Expand Down Expand Up @@ -1124,14 +1122,10 @@ zend_result zend_post_startup(void) /* {{{ */
compiler_globals->function_table = NULL;
free(compiler_globals->class_table);
compiler_globals->class_table = NULL;
if (compiler_globals->map_ptr_real_base) {
free(compiler_globals->map_ptr_real_base);
}
free(compiler_globals->map_ptr_real_base);
compiler_globals->map_ptr_real_base = NULL;
compiler_globals->map_ptr_base = ZEND_MAP_PTR_BIASED_BASE(NULL);
if (compiler_globals->internal_run_time_cache) {
pefree(compiler_globals->internal_run_time_cache, 1);
}
pefree(compiler_globals->internal_run_time_cache, 1);
compiler_globals->internal_run_time_cache = NULL;
if ((script_encoding_list = (zend_encoding **)compiler_globals->script_encoding_list)) {
compiler_globals_ctor(compiler_globals);
Expand Down Expand Up @@ -1215,10 +1209,8 @@ void zend_shutdown(void) /* {{{ */
CG(script_encoding_list) = NULL;
CG(script_encoding_list_size) = 0;
}
if (CG(internal_run_time_cache)) {
pefree(CG(internal_run_time_cache), 1);
CG(internal_run_time_cache) = NULL;
}
pefree(CG(internal_run_time_cache), 1);
CG(internal_run_time_cache) = NULL;
#endif
zend_map_ptr_static_last = 0;
zend_map_ptr_static_size = 0;
Expand Down
7 changes: 3 additions & 4 deletions Zend/zend_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -1116,8 +1116,9 @@ static zend_result zend_parse_arg(uint32_t arg_num, zval *arg, va_list *va, cons
} else {
zend_argument_type_error(arg_num, "must be of type %s, %s given", expected_type, zend_zval_value_name(arg));
}
} else if (error) {
} else {
efree(error);

}
Comment on lines 1120 to 1122
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
efree(error);
}
efree(error);
}


return FAILURE;
Expand Down Expand Up @@ -4090,9 +4091,7 @@ static zend_always_inline bool zend_is_callable_check_func(zval *callable, zend_
if ((fcc->function_handler->common.fn_flags & ZEND_ACC_PRIVATE)
|| (!zend_check_protected(zend_get_function_root_class(fcc->function_handler), scope))) {
if (error) {
if (*error) {
efree(*error);
}
efree(*error);
zend_spprintf(error, 0, "cannot access %s method %s::%s()", zend_visibility_string(fcc->function_handler->common.fn_flags), ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name));
}
retval = 0;
Expand Down
6 changes: 2 additions & 4 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,8 @@ void zend_oparray_context_begin(zend_oparray_context *prev_context, zend_op_arra

void zend_oparray_context_end(zend_oparray_context *prev_context) /* {{{ */
{
if (CG(context).brk_cont_array) {
efree(CG(context).brk_cont_array);
CG(context).brk_cont_array = NULL;
}
efree(CG(context).brk_cont_array);
CG(context).brk_cont_array = NULL;
if (CG(context).labels) {
zend_hash_destroy(CG(context).labels);
FREE_HASHTABLE(CG(context).labels);
Expand Down
6 changes: 2 additions & 4 deletions Zend/zend_gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -472,10 +472,8 @@ static zend_always_inline void gc_remove_from_roots(gc_root_buffer *root)

static void root_buffer_dtor(zend_gc_globals *gc_globals)
{
if (gc_globals->buf) {
free(gc_globals->buf);
gc_globals->buf = NULL;
}
free(gc_globals->buf);
gc_globals->buf = NULL;
}

static void gc_globals_ctor_ex(zend_gc_globals *gc_globals)
Expand Down
20 changes: 5 additions & 15 deletions Zend/zend_opcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -512,18 +512,12 @@ ZEND_API void destroy_zend_class(zval *zv)
} ZEND_HASH_FOREACH_END();
zend_hash_destroy(&ce->constants_table);
}
if (ce->iterator_funcs_ptr) {
free(ce->iterator_funcs_ptr);
}
if (ce->arrayaccess_funcs_ptr) {
free(ce->arrayaccess_funcs_ptr);
}
free(ce->iterator_funcs_ptr);
free(ce->arrayaccess_funcs_ptr);
if (ce->num_interfaces > 0) {
free(ce->interfaces);
}
if (ce->properties_info_table) {
free(ce->properties_info_table);
}
free(ce->properties_info_table);
if (ce->attributes) {
zend_hash_release(ce->attributes);
}
Expand Down Expand Up @@ -613,12 +607,8 @@ ZEND_API void destroy_op_array(zend_op_array *op_array)
if (op_array->attributes) {
zend_hash_release(op_array->attributes);
}
if (op_array->live_range) {
efree(op_array->live_range);
}
if (op_array->try_catch_array) {
efree(op_array->try_catch_array);
}
efree(op_array->live_range);
efree(op_array->try_catch_array);
if (zend_extension_flags & ZEND_EXTENSIONS_HAVE_OP_ARRAY_DTOR) {
if (op_array->fn_flags & ZEND_ACC_DONE_PASS_TWO) {
zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_dtor_handler, op_array);
Expand Down
4 changes: 1 addition & 3 deletions Zend/zend_ptr_stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ ZEND_API void zend_ptr_stack_n_pop(zend_ptr_stack *stack, int count, ...)

ZEND_API void zend_ptr_stack_destroy(zend_ptr_stack *stack)
{
if (stack->elements) {
pefree(stack->elements, stack->persistent);
}
pefree(stack->elements, stack->persistent);
}


Expand Down
6 changes: 2 additions & 4 deletions Zend/zend_smart_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,8 @@ static zend_always_inline size_t smart_string_alloc(smart_string *str, size_t le
}

static zend_always_inline void smart_string_free_ex(smart_string *str, bool persistent) {
if (str->c) {
pefree(str->c, persistent);
str->c = NULL;
}
pefree(str->c, persistent);
str->c = NULL;
str->a = str->len = 0;
}

Expand Down
12 changes: 4 additions & 8 deletions Zend/zend_stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,8 @@ ZEND_API bool zend_stack_is_empty(const zend_stack *stack)

ZEND_API void zend_stack_destroy(zend_stack *stack)
{
if (stack->elements) {
efree(stack->elements);
stack->elements = NULL;
}
efree(stack->elements);
stack->elements = NULL;
}


Expand Down Expand Up @@ -151,10 +149,8 @@ ZEND_API void zend_stack_clean(zend_stack *stack, void (*func)(void *), bool fre
}
}
if (free_elements) {
if (stack->elements) {
efree(stack->elements);
stack->elements = NULL;
}
efree(stack->elements);
stack->elements = NULL;
stack->top = stack->max = 0;
}
}
6 changes: 2 additions & 4 deletions Zend/zend_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,8 @@ static void zend_file_handle_dtor(zend_file_handle *fh) /* {{{ */
zend_string_release_ex(fh->opened_path, 0);
fh->opened_path = NULL;
}
if (fh->buf) {
efree(fh->buf);
fh->buf = NULL;
}
efree(fh->buf);
fh->buf = NULL;
if (fh->filename) {
zend_string_release(fh->filename);
fh->filename = NULL;
Expand Down
8 changes: 2 additions & 6 deletions ext/calendar/calendar.c
Original file line number Diff line number Diff line change
Expand Up @@ -474,12 +474,8 @@ PHP_FUNCTION(jdtojewish)

RETVAL_NEW_STR(zend_strpprintf(0, "%s %s %s", heb_number_to_chars(day, fl, &dayp), JEWISH_HEB_MONTH_NAME(year)[month], heb_number_to_chars(year, fl, &yearp)));

if (dayp) {
efree(dayp);
}
if (yearp) {
efree(yearp);
}
efree(dayp);
efree(yearp);
}
}
/* }}} */
Expand Down
4 changes: 1 addition & 3 deletions ext/com_dotnet/com_handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,7 @@ static void function_dtor(zval *zv)
zend_internal_function *f = (zend_internal_function*)Z_PTR_P(zv);

zend_string_release_ex(f->function_name, 0);
if (f->arg_info) {
efree(f->arg_info);
}
efree(f->arg_info);
efree(f);
}

Expand Down
4 changes: 1 addition & 3 deletions ext/com_dotnet/com_persist.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,7 @@ CPH_METHOD(SaveToFile)
}
}

if (olefilename) {
efree(olefilename);
}
efree(olefilename);

if (FAILED(res)) {
php_com_throw_exception(res, NULL);
Expand Down
4 changes: 1 addition & 3 deletions ext/com_dotnet/com_variant.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,7 @@ PHP_COM_DOTNET_API zend_result php_com_zval_from_variant(zval *z, VARIANT *v, in
php_com_wrap_variant(z, v, codepage);
}

if (olestring) {
efree(olestring);
}
efree(olestring);

if (ret == FAILURE) {
php_error_docref(NULL, E_WARNING, "variant->zval: conversion from 0x%x ret=%d", V_VT(v), ret);
Expand Down
8 changes: 2 additions & 6 deletions ext/date/php_date.c
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,7 @@ static void _php_date_tzinfo_dtor(zval *zv) /* {{{ */
/* {{{ PHP_RINIT_FUNCTION */
PHP_RINIT_FUNCTION(date)
{
if (DATEG(timezone)) {
efree(DATEG(timezone));
}
efree(DATEG(timezone));
DATEG(timezone) = NULL;
DATEG(tzcache) = NULL;
DATEG(last_errors) = NULL;
Expand All @@ -424,9 +422,7 @@ PHP_RINIT_FUNCTION(date)
/* {{{ PHP_RSHUTDOWN_FUNCTION */
PHP_RSHUTDOWN_FUNCTION(date)
{
if (DATEG(timezone)) {
efree(DATEG(timezone));
}
efree(DATEG(timezone));
DATEG(timezone) = NULL;

return SUCCESS;
Expand Down
8 changes: 2 additions & 6 deletions ext/dba/dba_db4.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,8 @@ DBA_NEXTKEY_FUNC(db4)
key = zend_string_init(gkey.data, gkey.size, /* persistent */ false);
}
if (info->flags & DBA_PERSISTENT) {
if (gkey.data) {
free(gkey.data);
}
if (gval.data) {
free(gval.data);
}
free(gkey.data);
free(gval.data);
}
}

Expand Down
12 changes: 3 additions & 9 deletions ext/dba/dba_flatfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ DBA_CLOSE_FUNC(flatfile)
{
flatfile *dba = info->dbf;

if (dba->nextkey.dptr) {
efree(dba->nextkey.dptr);
}
efree(dba->nextkey.dptr);
pefree(dba, info->flags&DBA_PERSISTENT);
}

Expand Down Expand Up @@ -127,9 +125,7 @@ DBA_FIRSTKEY_FUNC(flatfile)
{
flatfile *dba = info->dbf;

if (dba->nextkey.dptr) {
efree(dba->nextkey.dptr);
}
efree(dba->nextkey.dptr);
dba->nextkey = flatfile_firstkey(dba);
if (dba->nextkey.dptr) {
return zend_string_init(dba->nextkey.dptr, dba->nextkey.dsize, /* persistent */ false);
Expand All @@ -145,9 +141,7 @@ DBA_NEXTKEY_FUNC(flatfile)
return NULL;
}

if (dba->nextkey.dptr) {
efree(dba->nextkey.dptr);
}
efree(dba->nextkey.dptr);
dba->nextkey = flatfile_nextkey(dba);
if (dba->nextkey.dptr) {
return zend_string_init(dba->nextkey.dptr, dba->nextkey.dsize, /* persistent */ false);
Expand Down
4 changes: 1 addition & 3 deletions ext/dba/dba_gdbm.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,7 @@ DBA_FIRSTKEY_FUNC(gdbm)
datum gkey;
zend_string *key = NULL;

if (dba->nextkey.dptr) {
free(dba->nextkey.dptr);
}
free(dba->nextkey.dptr);

gkey = gdbm_firstkey(dba->dbf);
if (gkey.dptr) {
Expand Down
16 changes: 4 additions & 12 deletions ext/dba/libinifile/inifile.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,16 @@ const char *inifile_version(void)
/* {{{ inifile_free_key */
void inifile_key_free(key_type *key)
{
if (key->group) {
efree(key->group);
}
if (key->name) {
efree(key->name);
}
efree(key->group);
efree(key->name);
memset(key, 0, sizeof(key_type));
}
/* }}} */

/* {{{ inifile_free_val */
void inifile_val_free(val_type *val)
{
if (val->value) {
efree(val->value);
}
efree(val->value);
memset(val, 0, sizeof(val_type));
}
/* }}} */
Expand Down Expand Up @@ -193,9 +187,7 @@ static int inifile_read(inifile *dba, line_type *ln) {
if (!ln->key.group) {
ln->key.group = estrdup("");
}
if (ln->key.name) {
efree(ln->key.name);
}
efree(ln->key.name);
ln->key.name = etrim(fline);
ln->val.value = etrim(pos+1);
ln->pos = php_stream_tell(dba->fp);
Expand Down
Loading