Skip to content

Commit 1e0b046

Browse files
committed
Fixed memory leaks in ext/ffi/tests/100.phpt on Mac OSX
1 parent ad1b62f commit 1e0b046

File tree

4 files changed

+25
-17
lines changed

4 files changed

+25
-17
lines changed

ext/ffi/ffi.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5709,7 +5709,7 @@ static int zend_ffi_validate_func_ret_type(zend_ffi_type *type) /* {{{ */
57095709
}
57105710
/* }}} */
57115711

5712-
void zend_ffi_make_func_type(zend_ffi_dcl *dcl, HashTable *args) /* {{{ */
5712+
void zend_ffi_make_func_type(zend_ffi_dcl *dcl, HashTable *args, zend_ffi_dcl *nested_dcl) /* {{{ */
57135713
{
57145714
zend_ffi_type *type;
57155715
zend_ffi_type *ret_type;
@@ -5725,6 +5725,7 @@ void zend_ffi_make_func_type(zend_ffi_dcl *dcl, HashTable *args) /* {{{ */
57255725
arg_type = ZEND_FFI_TYPE(arg_type);
57265726
if (arg_type->kind == ZEND_FFI_TYPE_VOID) {
57275727
if (zend_hash_num_elements(args) != 1) {
5728+
zend_ffi_cleanup_dcl(nested_dcl);
57285729
zend_ffi_cleanup_dcl(dcl);
57295730
zend_hash_destroy(args);
57305731
pefree(args, FFI_G(persistent));
@@ -5743,6 +5744,7 @@ void zend_ffi_make_func_type(zend_ffi_dcl *dcl, HashTable *args) /* {{{ */
57435744
}
57445745

57455746
if (zend_ffi_validate_func_ret_type(ret_type) != SUCCESS) {
5747+
zend_ffi_cleanup_dcl(nested_dcl);
57465748
zend_ffi_cleanup_dcl(dcl);
57475749
if (args) {
57485750
zend_hash_destroy(args);
@@ -5799,6 +5801,12 @@ void zend_ffi_make_func_type(zend_ffi_dcl *dcl, HashTable *args) /* {{{ */
57995801
#endif
58005802
default:
58015803
type->func.abi = FFI_DEFAULT_ABI;
5804+
zend_ffi_cleanup_dcl(nested_dcl);
5805+
if (args) {
5806+
zend_hash_destroy(args);
5807+
pefree(args, FFI_G(persistent));
5808+
}
5809+
_zend_ffi_type_dtor(type);
58025810
zend_ffi_parser_error("unsupported calling convention line %d", FFI_G(line));
58035811
break;
58045812
}

ext/ffi/ffi.g

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ declarator(zend_ffi_dcl *dcl, const char **name, size_t *name_len):
351351
")"
352352
{nested = 1;}
353353
)
354-
array_or_function_declarators(dcl)?
354+
array_or_function_declarators(dcl, &nested_dcl)?
355355
{if (nested) zend_ffi_nested_declaration(dcl, &nested_dcl);}
356356
;
357357

@@ -366,7 +366,7 @@ abstract_declarator(zend_ffi_dcl *dcl):
366366
")"
367367
{nested = 1;}
368368
)?
369-
array_or_function_declarators(dcl)?
369+
array_or_function_declarators(dcl, &nested_dcl)?
370370
{if (nested) zend_ffi_nested_declaration(dcl, &nested_dcl);}
371371
;
372372

@@ -383,7 +383,7 @@ parameter_declarator(zend_ffi_dcl *dcl, const char **name, size_t *name_len):
383383
| ID(name, name_len)
384384
| /* empty */
385385
)
386-
array_or_function_declarators(dcl)?
386+
array_or_function_declarators(dcl, &nested_dcl)?
387387
{if (nested) zend_ffi_nested_declaration(dcl, &nested_dcl);}
388388
;
389389

@@ -394,7 +394,7 @@ pointer(zend_ffi_dcl *dcl):
394394
)+
395395
;
396396

397-
array_or_function_declarators(zend_ffi_dcl *dcl):
397+
array_or_function_declarators(zend_ffi_dcl *dcl, zend_ffi_dcl *nested_dcl):
398398
{zend_ffi_dcl dummy = ZEND_FFI_ATTR_INIT;}
399399
{zend_ffi_val len = {.kind = ZEND_FFI_VAL_EMPTY};}
400400
{HashTable *args = NULL;}
@@ -419,7 +419,7 @@ array_or_function_declarators(zend_ffi_dcl *dcl):
419419
)
420420
)
421421
"]"
422-
array_or_function_declarators(dcl)?
422+
array_or_function_declarators(dcl, nested_dcl)?
423423
{dcl->attr |= attr;}
424424
{zend_ffi_make_array_type(dcl, &len);}
425425
| "("
@@ -437,9 +437,9 @@ array_or_function_declarators(zend_ffi_dcl *dcl):
437437
{attr |= ZEND_FFI_ATTR_VARIADIC;}
438438
)?
439439
")"
440-
array_or_function_declarators(dcl)?
440+
array_or_function_declarators(dcl, nested_dcl)?
441441
{dcl->attr |= attr;}
442-
{zend_ffi_make_func_type(dcl, args);}
442+
{zend_ffi_make_func_type(dcl, args, nested_dcl);}
443443
// | "(" (ID ("," ID)*)? ")" // TODO: ANSI function not-implemented ???
444444
)
445445
;

ext/ffi/ffi_parser.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ static int parse_declarator(int sym, zend_ffi_dcl *dcl, const char **name, size_
271271
static int parse_abstract_declarator(int sym, zend_ffi_dcl *dcl);
272272
static int parse_parameter_declarator(int sym, zend_ffi_dcl *dcl, const char **name, size_t *name_len);
273273
static int parse_pointer(int sym, zend_ffi_dcl *dcl);
274-
static int parse_array_or_function_declarators(int sym, zend_ffi_dcl *dcl);
274+
static int parse_array_or_function_declarators(int sym, zend_ffi_dcl *dcl, zend_ffi_dcl *nested_dcl);
275275
static int parse_parameter_declaration(int sym, HashTable **args);
276276
static int parse_type_name(int sym, zend_ffi_dcl *dcl);
277277
static int parse_attributes(int sym, zend_ffi_dcl *dcl);
@@ -2579,7 +2579,7 @@ static int parse_declarator(int sym, zend_ffi_dcl *dcl, const char **name, size_
25792579
yy_error_sym("unexpected", sym);
25802580
}
25812581
if (sym == YY__LBRACK || sym == YY__LPAREN) {
2582-
sym = parse_array_or_function_declarators(sym, dcl);
2582+
sym = parse_array_or_function_declarators(sym, dcl, &nested_dcl);
25832583
}
25842584
if (nested) zend_ffi_nested_declaration(dcl, &nested_dcl);
25852585
return sym;
@@ -2604,7 +2604,7 @@ static int parse_abstract_declarator(int sym, zend_ffi_dcl *dcl) {
26042604
nested = 1;
26052605
}
26062606
if (sym == YY__LBRACK || sym == YY__LPAREN) {
2607-
sym = parse_array_or_function_declarators(sym, dcl);
2607+
sym = parse_array_or_function_declarators(sym, dcl, &nested_dcl);
26082608
}
26092609
if (nested) zend_ffi_nested_declaration(dcl, &nested_dcl);
26102610
return sym;
@@ -2634,7 +2634,7 @@ static int parse_parameter_declarator(int sym, zend_ffi_dcl *dcl, const char **n
26342634
yy_error_sym("unexpected", sym);
26352635
}
26362636
if (sym == YY__LBRACK || sym == YY__LPAREN) {
2637-
sym = parse_array_or_function_declarators(sym, dcl);
2637+
sym = parse_array_or_function_declarators(sym, dcl, &nested_dcl);
26382638
}
26392639
if (nested) zend_ffi_nested_declaration(dcl, &nested_dcl);
26402640
return sym;
@@ -2654,7 +2654,7 @@ static int parse_pointer(int sym, zend_ffi_dcl *dcl) {
26542654
return sym;
26552655
}
26562656

2657-
static int parse_array_or_function_declarators(int sym, zend_ffi_dcl *dcl) {
2657+
static int parse_array_or_function_declarators(int sym, zend_ffi_dcl *dcl, zend_ffi_dcl *nested_dcl) {
26582658
int sym2;
26592659
const unsigned char *save_pos;
26602660
const unsigned char *save_text;
@@ -2777,7 +2777,7 @@ static int parse_array_or_function_declarators(int sym, zend_ffi_dcl *dcl) {
27772777
}
27782778
sym = get_sym();
27792779
if (sym == YY__LBRACK || sym == YY__LPAREN) {
2780-
sym = parse_array_or_function_declarators(sym, dcl);
2780+
sym = parse_array_or_function_declarators(sym, dcl, nested_dcl);
27812781
}
27822782
dcl->attr |= attr;
27832783
zend_ffi_make_array_type(dcl, &len);
@@ -2839,10 +2839,10 @@ static int parse_array_or_function_declarators(int sym, zend_ffi_dcl *dcl) {
28392839
}
28402840
sym = get_sym();
28412841
if (sym == YY__LBRACK || sym == YY__LPAREN) {
2842-
sym = parse_array_or_function_declarators(sym, dcl);
2842+
sym = parse_array_or_function_declarators(sym, dcl, nested_dcl);
28432843
}
28442844
dcl->attr |= attr;
2845-
zend_ffi_make_func_type(dcl, args);
2845+
zend_ffi_make_func_type(dcl, args, nested_dcl);
28462846
} else {
28472847
yy_error_sym("unexpected", sym);
28482848
}

ext/ffi/php_ffi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ void zend_ffi_add_bit_field(zend_ffi_dcl *struct_dcl, const char *name, size_t n
224224
void zend_ffi_adjust_struct_size(zend_ffi_dcl *dcl);
225225
void zend_ffi_make_pointer_type(zend_ffi_dcl *dcl);
226226
void zend_ffi_make_array_type(zend_ffi_dcl *dcl, zend_ffi_val *len);
227-
void zend_ffi_make_func_type(zend_ffi_dcl *dcl, HashTable *args);
227+
void zend_ffi_make_func_type(zend_ffi_dcl *dcl, HashTable *args, zend_ffi_dcl *nested_dcl);
228228
void zend_ffi_add_arg(HashTable **args, const char *name, size_t name_len, zend_ffi_dcl *arg_dcl);
229229
void zend_ffi_declare(const char *name, size_t name_len, zend_ffi_dcl *dcl);
230230
void zend_ffi_add_attribute(zend_ffi_dcl *dcl, const char *name, size_t name_len);

0 commit comments

Comments
 (0)