Skip to content

Commit c4097fa

Browse files
committed
tree-wide: Do not manually check for NULL before calling free()
Changes performed with Coccinelle: @@ expression e; @@ - if (e) { efree(e); - } @@ expression e; @@ - if (e) { efree(e); e = NULL; - } @@ expression e; @@ - if (e) { free(e); - } @@ expression e; @@ - if (e) { free(e); e = NULL; - } @@ expression e, e2; @@ - if (e) { pefree(e, e2); - } @@ expression e, e2; @@ - if (e) { pefree(e, e2); e = NULL; - }
1 parent aa366b5 commit c4097fa

File tree

89 files changed

+339
-886
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+339
-886
lines changed

Zend/Optimizer/zend_optimizer.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,9 +1208,7 @@ static void zend_redo_pass_two(zend_op_array *op_array)
12081208
efree(op_array->literals);
12091209
op_array->literals = (zval*)(((char*)op_array->opcodes) + ZEND_MM_ALIGNED_SIZE_EX(sizeof(zend_op) * op_array->last, 16));
12101210
} else {
1211-
if (op_array->literals) {
1212-
efree(op_array->literals);
1213-
}
1211+
efree(op_array->literals);
12141212
op_array->literals = NULL;
12151213
}
12161214
#endif
@@ -1318,9 +1316,7 @@ static void zend_redo_pass_two_ex(zend_op_array *op_array, zend_ssa *ssa)
13181316
efree(op_array->literals);
13191317
op_array->literals = (zval*)(((char*)op_array->opcodes) + ZEND_MM_ALIGNED_SIZE_EX(sizeof(zend_op) * op_array->last, 16));
13201318
} else {
1321-
if (op_array->literals) {
1322-
efree(op_array->literals);
1323-
}
1319+
efree(op_array->literals);
13241320
op_array->literals = NULL;
13251321
}
13261322
#endif

Zend/zend.c

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -790,10 +790,8 @@ static void compiler_globals_dtor(zend_compiler_globals *compiler_globals) /* {{
790790
compiler_globals->map_ptr_base = ZEND_MAP_PTR_BIASED_BASE(NULL);
791791
compiler_globals->map_ptr_size = 0;
792792
}
793-
if (compiler_globals->internal_run_time_cache) {
794-
pefree(compiler_globals->internal_run_time_cache, 1);
795-
compiler_globals->internal_run_time_cache = NULL;
796-
}
793+
pefree(compiler_globals->internal_run_time_cache, 1);
794+
compiler_globals->internal_run_time_cache = NULL;
797795
}
798796
/* }}} */
799797

@@ -1124,14 +1122,10 @@ zend_result zend_post_startup(void) /* {{{ */
11241122
compiler_globals->function_table = NULL;
11251123
free(compiler_globals->class_table);
11261124
compiler_globals->class_table = NULL;
1127-
if (compiler_globals->map_ptr_real_base) {
1128-
free(compiler_globals->map_ptr_real_base);
1129-
}
1125+
free(compiler_globals->map_ptr_real_base);
11301126
compiler_globals->map_ptr_real_base = NULL;
11311127
compiler_globals->map_ptr_base = ZEND_MAP_PTR_BIASED_BASE(NULL);
1132-
if (compiler_globals->internal_run_time_cache) {
1133-
pefree(compiler_globals->internal_run_time_cache, 1);
1134-
}
1128+
pefree(compiler_globals->internal_run_time_cache, 1);
11351129
compiler_globals->internal_run_time_cache = NULL;
11361130
if ((script_encoding_list = (zend_encoding **)compiler_globals->script_encoding_list)) {
11371131
compiler_globals_ctor(compiler_globals);
@@ -1215,10 +1209,8 @@ void zend_shutdown(void) /* {{{ */
12151209
CG(script_encoding_list) = NULL;
12161210
CG(script_encoding_list_size) = 0;
12171211
}
1218-
if (CG(internal_run_time_cache)) {
1219-
pefree(CG(internal_run_time_cache), 1);
1220-
CG(internal_run_time_cache) = NULL;
1221-
}
1212+
pefree(CG(internal_run_time_cache), 1);
1213+
CG(internal_run_time_cache) = NULL;
12221214
#endif
12231215
zend_map_ptr_static_last = 0;
12241216
zend_map_ptr_static_size = 0;

Zend/zend_API.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,8 +1116,9 @@ static zend_result zend_parse_arg(uint32_t arg_num, zval *arg, va_list *va, cons
11161116
} else {
11171117
zend_argument_type_error(arg_num, "must be of type %s, %s given", expected_type, zend_zval_value_name(arg));
11181118
}
1119-
} else if (error) {
1119+
} else {
11201120
efree(error);
1121+
11211122
}
11221123

11231124
return FAILURE;
@@ -4090,9 +4091,7 @@ static zend_always_inline bool zend_is_callable_check_func(zval *callable, zend_
40904091
if ((fcc->function_handler->common.fn_flags & ZEND_ACC_PRIVATE)
40914092
|| (!zend_check_protected(zend_get_function_root_class(fcc->function_handler), scope))) {
40924093
if (error) {
4093-
if (*error) {
4094-
efree(*error);
4095-
}
4094+
efree(*error);
40964095
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));
40974096
}
40984097
retval = 0;

Zend/zend_compile.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,10 +349,8 @@ void zend_oparray_context_begin(zend_oparray_context *prev_context, zend_op_arra
349349

350350
void zend_oparray_context_end(zend_oparray_context *prev_context) /* {{{ */
351351
{
352-
if (CG(context).brk_cont_array) {
353-
efree(CG(context).brk_cont_array);
354-
CG(context).brk_cont_array = NULL;
355-
}
352+
efree(CG(context).brk_cont_array);
353+
CG(context).brk_cont_array = NULL;
356354
if (CG(context).labels) {
357355
zend_hash_destroy(CG(context).labels);
358356
FREE_HASHTABLE(CG(context).labels);

Zend/zend_gc.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -472,10 +472,8 @@ static zend_always_inline void gc_remove_from_roots(gc_root_buffer *root)
472472

473473
static void root_buffer_dtor(zend_gc_globals *gc_globals)
474474
{
475-
if (gc_globals->buf) {
476-
free(gc_globals->buf);
477-
gc_globals->buf = NULL;
478-
}
475+
free(gc_globals->buf);
476+
gc_globals->buf = NULL;
479477
}
480478

481479
static void gc_globals_ctor_ex(zend_gc_globals *gc_globals)

Zend/zend_opcode.c

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -512,18 +512,12 @@ ZEND_API void destroy_zend_class(zval *zv)
512512
} ZEND_HASH_FOREACH_END();
513513
zend_hash_destroy(&ce->constants_table);
514514
}
515-
if (ce->iterator_funcs_ptr) {
516-
free(ce->iterator_funcs_ptr);
517-
}
518-
if (ce->arrayaccess_funcs_ptr) {
519-
free(ce->arrayaccess_funcs_ptr);
520-
}
515+
free(ce->iterator_funcs_ptr);
516+
free(ce->arrayaccess_funcs_ptr);
521517
if (ce->num_interfaces > 0) {
522518
free(ce->interfaces);
523519
}
524-
if (ce->properties_info_table) {
525-
free(ce->properties_info_table);
526-
}
520+
free(ce->properties_info_table);
527521
if (ce->attributes) {
528522
zend_hash_release(ce->attributes);
529523
}
@@ -613,12 +607,8 @@ ZEND_API void destroy_op_array(zend_op_array *op_array)
613607
if (op_array->attributes) {
614608
zend_hash_release(op_array->attributes);
615609
}
616-
if (op_array->live_range) {
617-
efree(op_array->live_range);
618-
}
619-
if (op_array->try_catch_array) {
620-
efree(op_array->try_catch_array);
621-
}
610+
efree(op_array->live_range);
611+
efree(op_array->try_catch_array);
622612
if (zend_extension_flags & ZEND_EXTENSIONS_HAVE_OP_ARRAY_DTOR) {
623613
if (op_array->fn_flags & ZEND_ACC_DONE_PASS_TWO) {
624614
zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_dtor_handler, op_array);

Zend/zend_ptr_stack.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ ZEND_API void zend_ptr_stack_n_pop(zend_ptr_stack *stack, int count, ...)
7171

7272
ZEND_API void zend_ptr_stack_destroy(zend_ptr_stack *stack)
7373
{
74-
if (stack->elements) {
75-
pefree(stack->elements, stack->persistent);
76-
}
74+
pefree(stack->elements, stack->persistent);
7775
}
7876

7977

Zend/zend_smart_string.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,8 @@ static zend_always_inline size_t smart_string_alloc(smart_string *str, size_t le
6666
}
6767

6868
static zend_always_inline void smart_string_free_ex(smart_string *str, bool persistent) {
69-
if (str->c) {
70-
pefree(str->c, persistent);
71-
str->c = NULL;
72-
}
69+
pefree(str->c, persistent);
70+
str->c = NULL;
7371
str->a = str->len = 0;
7472
}
7573

Zend/zend_stack.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,8 @@ ZEND_API bool zend_stack_is_empty(const zend_stack *stack)
7777

7878
ZEND_API void zend_stack_destroy(zend_stack *stack)
7979
{
80-
if (stack->elements) {
81-
efree(stack->elements);
82-
stack->elements = NULL;
83-
}
80+
efree(stack->elements);
81+
stack->elements = NULL;
8482
}
8583

8684

@@ -151,10 +149,8 @@ ZEND_API void zend_stack_clean(zend_stack *stack, void (*func)(void *), bool fre
151149
}
152150
}
153151
if (free_elements) {
154-
if (stack->elements) {
155-
efree(stack->elements);
156-
stack->elements = NULL;
157-
}
152+
efree(stack->elements);
153+
stack->elements = NULL;
158154
stack->top = stack->max = 0;
159155
}
160156
}

Zend/zend_stream.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,8 @@ static void zend_file_handle_dtor(zend_file_handle *fh) /* {{{ */
235235
zend_string_release_ex(fh->opened_path, 0);
236236
fh->opened_path = NULL;
237237
}
238-
if (fh->buf) {
239-
efree(fh->buf);
240-
fh->buf = NULL;
241-
}
238+
efree(fh->buf);
239+
fh->buf = NULL;
242240
if (fh->filename) {
243241
zend_string_release(fh->filename);
244242
fh->filename = NULL;

0 commit comments

Comments
 (0)