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 1 commit
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
1 change: 1 addition & 0 deletions ext/ffi/ffi.g
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ declaration_specifiers(zend_ffi_dcl *dcl):
| attributes(dcl)
| type_qualifier(dcl)
| type_specifier(dcl)
{if (((dcl->flags & (ZEND_FFI_DCL_ENUM | ZEND_FFI_DCL_STORAGE_CLASS)) == ZEND_FFI_DCL_ENUM)) zend_ffi_cleanup_dcl(dcl);}
)
)+
;
Expand Down
1 change: 1 addition & 0 deletions ext/ffi/ffi_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -2166,6 +2166,7 @@ static int parse_declaration_specifiers(int sym, zend_ffi_dcl *dcl) {
case YY_ENUM:
case YY_ID:
sym = parse_type_specifier(sym, dcl);
if (((dcl->flags & (ZEND_FFI_DCL_ENUM | ZEND_FFI_DCL_STORAGE_CLASS)) == ZEND_FFI_DCL_ENUM)) zend_ffi_cleanup_dcl(dcl);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems not enough. The following code starts to fail.

<?php
$ffi = FFI::cdef("
    enum {
        TEST_ONE=1,
        TEST_TWO=2,
    } x;
");
?>

Uncaught FFI\ParserException: Unsupported type specifier combination at line 5

break;
default:
yy_error_sym("unexpected", 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
40 changes: 40 additions & 0 deletions ext/ffi/tests/gh14286.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--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;
");
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)
Loading