Skip to content

Commit 8319b0c

Browse files
committed
Fix memory leaks
1 parent d83bc0e commit 8319b0c

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

Zend/zend_autoload.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "zend_types.h"
2626
#include "zend_exceptions.h"
2727
#include "zend_interfaces.h"
28+
#include "zend_string.h"
2829

2930
#define HT_MOVE_TAIL_TO_HEAD(ht) \
3031
do { \
@@ -111,8 +112,11 @@ void* zend_autoload_call(zend_string *name, zend_string *lname, long type)
111112
static zend_string* zend_autoload_get_key(zend_fcall_info *fci)
112113
{
113114
switch (Z_TYPE(fci->function_name)) {
114-
case IS_STRING:
115-
return Z_STR(fci->function_name);
115+
case IS_STRING: {
116+
zend_string *ret = Z_STR(fci->function_name);
117+
zend_string_addref(ret);
118+
return ret;
119+
}
116120
case IS_OBJECT: {
117121
char handle[16];
118122
sprintf(handle, "%lu", (unsigned long) Z_OBJ_HANDLE(fci->function_name));
@@ -133,8 +137,10 @@ int zend_autoload_register(zend_autoload_func* func, zend_bool prepend)
133137
}
134138

135139
if (zend_hash_add_ptr(&EG(autoload.functions), key, func) == NULL) {
140+
zend_string_release(key);
136141
return FAILURE;
137142
}
143+
zend_string_release(key);
138144

139145
if (prepend) {
140146
HT_MOVE_TAIL_TO_HEAD(&EG(autoload.functions));
@@ -150,9 +156,17 @@ int zend_autoload_unregister(zend_autoload_func* func)
150156

151157
zend_hash_del(&EG(autoload.functions), key);
152158

159+
zend_string_release(key);
160+
153161
return SUCCESS;
154162
}
155163

164+
void zend_autoload_dtor(zval *pzv)
165+
{
166+
zend_autoload_func *func = Z_PTR_P(pzv);
167+
efree(func);
168+
}
169+
156170
ZEND_FUNCTION(autoload_register)
157171
{
158172
zend_autoload_func *func;
@@ -170,6 +184,7 @@ ZEND_FUNCTION(autoload_register)
170184
}
171185

172186
if (zend_autoload_register(func, prepend) == FAILURE) {
187+
efree(func);
173188
RETURN_FALSE;
174189
}
175190
RETURN_TRUE;

Zend/zend_autoload.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ typedef struct {
3535
void* zend_autoload_call(zend_string *name, zend_string *lname, long type);
3636
int zend_autoload_register(zend_autoload_func* func, zend_bool prepend);
3737
int zend_autoload_unregister(zend_autoload_func* func);
38+
void zend_autoload_dtor(zval *pzv);
3839

3940
#define ZEND_AUTOLOAD_CLASS (1<<0)
4041
#define ZEND_AUTOLOAD_FUNCTION (1<<1)

Zend/zend_constants.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,10 @@ zend_constant *zend_quick_get_constant(const zval *key, zend_ulong flags)
481481
}
482482
}
483483
}
484+
485+
if (c == NULL) {
486+
c = (zend_constant*) zend_autoload_call(Z_STR_P(key), Z_STR_P(key), ZEND_AUTOLOAD_CONSTANT);
487+
}
484488
return c;
485489
}
486490

Zend/zend_execute_API.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,10 @@ void init_executor(void) /* {{{ */
151151
EG(autoload_func) = NULL;
152152
EG(error_handling) = EH_NORMAL;
153153

154-
ZEND_INIT_SYMTABLE(&EG(autoload.stack.class));
155-
ZEND_INIT_SYMTABLE(&EG(autoload.stack.function));
156-
ZEND_INIT_SYMTABLE(&EG(autoload.stack.constant));
157-
ZEND_INIT_SYMTABLE(&EG(autoload.functions));
154+
zend_hash_init(&EG(autoload.stack.class), 8, NULL, NULL, 0);
155+
zend_hash_init(&EG(autoload.stack.function), 8, NULL, NULL, 0);
156+
zend_hash_init(&EG(autoload.stack.constant), 8, NULL, NULL, 0);
157+
zend_hash_init(&EG(autoload.functions), 8, NULL, zend_autoload_dtor, 0);
158158

159159
zend_vm_stack_init();
160160

@@ -371,6 +371,10 @@ void shutdown_executor(void) /* {{{ */
371371
zend_stack_destroy(&EG(user_error_handlers));
372372
zend_stack_destroy(&EG(user_exception_handlers));
373373
zend_objects_store_destroy(&EG(objects_store));
374+
zend_hash_destroy(&EG(autoload.functions));
375+
zend_hash_destroy(&EG(autoload.stack.class));
376+
zend_hash_destroy(&EG(autoload.stack.function));
377+
zend_hash_destroy(&EG(autoload.stack.constant));
374378
if (EG(in_autoload)) {
375379
zend_hash_destroy(EG(in_autoload));
376380
FREE_HASHTABLE(EG(in_autoload));

0 commit comments

Comments
 (0)