Skip to content

No check NULL after call malloc function #17013

@xmat111998

Description

@xmat111998

Description

While reviewing the zend_register_functions function in php/Zend/zend_API.c, I noticed several lines that call malloc without checking for a NULL return value. This oversight could potentially lead to a crash in cases where memory allocation fails.

ZEND_API zend_result zend_register_functions(zend_class_entry *scope, const zend_function_entry *functions, HashTable *function_table, int type) /* {{{ */
{
...

			/* Treat return type as an extra argument */
			num_args++;
			new_arg_info = malloc(sizeof(zend_arg_info) * num_args);
			memcpy(new_arg_info, arg_info, sizeof(zend_arg_info) * num_args);
			reg_function->common.arg_info = new_arg_info + 1;
			for (i = 0; i < num_args; i++) {
				if (ZEND_TYPE_HAS_CLASS(new_arg_info[i].type)) {
					ZEND_ASSERT(ZEND_TYPE_HAS_NAME(new_arg_info[i].type)
						&& "Should be stored as simple name");
					const char *class_name = ZEND_TYPE_LITERAL_NAME(new_arg_info[i].type);

					size_t num_types = 1;
					const char *p = class_name;
					while ((p = strchr(p, '|'))) {
						num_types++;
						p++;
					}

					if (num_types == 1) {
						/* Simple class type */
						ZEND_TYPE_SET_PTR(new_arg_info[i].type,
							zend_string_init_interned(class_name, strlen(class_name), 1));
					} else {
						/* Union type */
						zend_type_list *list = malloc(ZEND_TYPE_LIST_SIZE(num_types));
						list->num_types = num_types;
						ZEND_TYPE_SET_LIST(new_arg_info[i].type, list);

...
	}
	return SUCCESS;
}

Specifically, on the line:

new_arg_info = malloc(sizeof(zend_arg_info) * num_args);

Can we add some line to check the return NULL of them?

PHP Version

PHP 8.0.28

Operating System

Linux

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions