Skip to content

GH-14286 (ffi enum type (when enum has no name) make memory leak) #14839

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

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion ext/ffi/ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -3544,7 +3544,7 @@ ZEND_METHOD(FFI, scope) /* {{{ */
}
/* }}} */

static void zend_ffi_cleanup_dcl(zend_ffi_dcl *dcl) /* {{{ */
void zend_ffi_cleanup_dcl(zend_ffi_dcl *dcl) /* {{{ */
{
if (dcl) {
zend_ffi_type_dtor(dcl->type);
Expand Down
5 changes: 4 additions & 1 deletion ext/ffi/ffi.g
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ declarations:
initializer?
{zend_ffi_declare(name, name_len, &dcl);}
)*
)?
|
/* empty */
{if (common_dcl.flags & (ZEND_FFI_DCL_ENUM | ZEND_FFI_DCL_STRUCT | ZEND_FFI_DCL_UNION)) zend_ffi_cleanup_dcl(&common_dcl);}
)
";"
)*
;
Expand Down
4 changes: 4 additions & 0 deletions ext/ffi/ffi_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -2056,6 +2056,10 @@ static int parse_declarations(int sym) {
}
zend_ffi_declare(name, name_len, &dcl);
}
} else if (sym == YY__SEMICOLON) {
if (common_dcl.flags & (ZEND_FFI_DCL_ENUM | ZEND_FFI_DCL_STRUCT | ZEND_FFI_DCL_UNION)) zend_ffi_cleanup_dcl(&common_dcl);
} else {
yy_error_sym("unexpected", sym);
}
if (sym != YY__SEMICOLON) {
yy_error_sym("';' expected, got", sym);
Expand Down
1 change: 1 addition & 0 deletions ext/ffi/php_ffi.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ typedef struct _zend_ffi_val {

zend_result zend_ffi_parse_decl(const char *str, size_t len);
zend_result zend_ffi_parse_type(const char *str, size_t len, zend_ffi_dcl *dcl);
void zend_ffi_cleanup_dcl(zend_ffi_dcl *dcl);

/* parser callbacks */
void ZEND_NORETURN zend_ffi_parser_error(const char *msg, ...);
Expand Down
46 changes: 46 additions & 0 deletions ext/ffi/tests/gh14286_1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
--TEST--
GH-14286 (ffi enum type (when enum has no name) make memory leak)
--EXTENSIONS--
ffi
--INI--
ffi.enable=1
--FILE--
<?php
$ffi = FFI::cdef("
enum {
TEST_ONE=1,
TEST_TWO=2,
};
enum TestEnum {
TEST_THREE=3,
};
struct TestStruct {
enum {
TEST_FOUR=4,
} test1;
enum TestEnum2 {
TEST_FIVE=5,
} test2;
};
typedef enum { TEST_SIX=6 } TestEnum3;
struct {
int x;
};
union {
int x;
};
");
var_dump($ffi->TEST_ONE);
var_dump($ffi->TEST_TWO);
var_dump($ffi->TEST_THREE);
var_dump($ffi->TEST_FOUR);
var_dump($ffi->TEST_FIVE);
var_dump($ffi->TEST_SIX);
?>
--EXPECT--
int(1)
int(2)
int(3)
int(4)
int(5)
int(6)
25 changes: 25 additions & 0 deletions ext/ffi/tests/gh14286_2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
GH-14286 (ffi enum type (when enum has no name) make memory leak)
--EXTENSIONS--
ffi
--SKIPIF--
<?php
if (PHP_DEBUG || getenv('SKIP_ASAN')) die("xfail: FFI cleanup after parser error is nor implemented");
?>
--INI--
ffi.enable=1
--FILE--
<?php
try {
$ffi = FFI::cdef("
enum {
TEST_ONE=1,
TEST_TWO=2,
} x;
");
} catch (Throwable $e) {
echo $e->getMessage(), "\n";
}
?>
--EXPECT--
Failed resolving C variable 'x'
Loading