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

Conversation

nielsdos
Copy link
Member

@nielsdos nielsdos commented Jul 5, 2024

For top-level anonymous type definition we never store the declaration anywhere else nor the type anywhere else.
The declaration keeps owning the type and it goes out of scope. For anonymous fields this gets handled by the add_anonymous_field code that removes the type from the declaration.
This patch does something similar in the parsing code when it is detected we're dealing with an anonymous enum in a top-level declaration.

For top-level anonymous type definition we never store the declaration anywhere
else nor the type anywhere else.
The declaration keeps owning the type and it goes out of scope.
For anonymous fields this gets handled by the add_anonymous_field code that
removes the type from the declaration.
This patch does something similar in the parsing code when it is
detected we're dealing with an anonymous enum in a top-level declaration.
@nielsdos nielsdos marked this pull request as ready for review July 5, 2024 23:36
@nielsdos nielsdos requested a review from dstogov as a code owner July 5, 2024 23:36
@@ -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

ext/ffi/ffi.g Outdated
Comment on lines 101 to 102
)?
{if (!has_name && ((common_dcl.flags & (ZEND_FFI_DCL_ENUM | ZEND_FFI_DCL_STORAGE_CLASS)) == ZEND_FFI_DCL_ENUM)) zend_ffi_cleanup_dcl(&common_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 may be done in a simpler way.

diff --git a/ext/ffi/ffi.g b/ext/ffi/ffi.g
index e30d86b21c7..7d9646d2961 100644
--- a/ext/ffi/ffi.g
+++ b/ext/ffi/ffi.g
@@ -97,7 +97,10 @@ declarations:
 				initializer?
 				{zend_ffi_declare(name, name_len, &dcl);}
 			)*
-		)?
+		|
+			/* empty */
+			{if ((common_dcl.flags & (ZEND_FFI_DCL_ENUM | ZEND_FFI_DCL_STORAGE_CLASS)) == ZEND_FFI_DCL_ENUM) zend_ffi_cleanup_dcl(&common_dcl);}
+		)
 		";"
 	)*
 ;

Most probably the ((common_dcl.flags & (ZEND_FFI_DCL_ENUM | ZEND_FFI_DCL_STORAGE_CLASS)) == ZEND_FFI_DCL_ENUM) check also may be simplified.

ext/ffi/ffi.g Outdated
)?
|
/* empty */
{if (common_dcl.flags & ZEND_FFI_DCL_ENUM) zend_ffi_cleanup_dcl(&common_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 looks right.

I'm still not completely sure if common_dcl.flags & ZEND_FFI_DCL_ENUM is the right condition.
Currently we may also have similar structures and they also may lead to memory leaks.
GCC allows this syntax with a warning: unnamed struct/union that defines no instances.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah indeed it seems the following leaks too:

$ffi = FFI::cdef("
    struct {
        int x;
    };
    union {
        int x;
    };
");

I think besides those are the remaining two indeed. I changed the condition. Thanks.

@nielsdos nielsdos closed this in c0de721 Jul 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants