Skip to content

Fix leaking definitions on FFI::cdef()->new() #11751

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 1 commit into from

Conversation

iluuu1994
Copy link
Member

Previously, FFI_G(symbols) and FFI_G(tags) were never cleaned up when calling new on an existing object. However, if cdef() is called without parameters these globals are NULL and might be created when new() creates new definitions. These would then be discarded without freeing them.

Furthermore, when calling new() on an existing object ffi->symbols and ffi->tags are reused for the operation. new() might add new definitions, leaking them into the next new() call. I'm not sure if this is desired behavior.

/cc @kocsismate

Previously, FFI_G(symbols) and FFI_G(tags) were never cleaned up when calling
new on an existing object. However, if cdef() is called without parameters these
globals are NULL and might be created when new() creates new definitions. These
would then be discarded without freeing them.

Furthermore, when calling new() on an existing object ffi->symbols and ffi->tags
are reused for the operation. new() might add new definitions, leaking them into
the next new() call. I'm not sure if this is desired behavior.
iluuu1994 referenced this pull request in symfony/symfony Jul 20, 2023
* 5.4:
  Fix deprecations on PHP 8.3
  [WebProfilerBundle] Fix error in case of 'Content-Type' set null in dev environment with no debug
  [Routing] Use vsprintf instead of sprintf + unpacking
Copy link
Member

@kocsismate kocsismate left a comment

Choose a reason for hiding this comment

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

Nice, thank you! I also tried something similar but made a few mistakes along the way: fe14c4c#diff-74a5c960477012cea7bb9b723ea9b0e51279274a8703e145f45e7970316e2558R3712

@iluuu1994
Copy link
Member Author

I'll merge this without the ext/ffi/tests/leaking_definition.phpt test, but it would be nice if @dstogov could verify whether that is a bug or intentional. For reference, in case of rebasing:

--TEST--
FFI::cdef()->new() shouldn't leak definitions into outer scope
--EXTENSIONS--
ffi
--XFAIL--
--FILE--
<?php
$ffi = \FFI::cdef('struct Example { uint32_t x; };');
var_dump($ffi->new('struct Example2 { uint32_t x; }'));
try {
    var_dump($ffi->new('struct Example2'));
} catch (\FFI\ParserException $e) {
    echo $e->getMessage(), "\n";
}
?>
--EXPECTF--
object(FFI\CData:struct Example2)#2 (1) {
  ["x"]=>
  int(0)
}
Incomplete struct "Example2" at line %d
- Incomplete struct "Example2" at line %d
+ object(FFI\CData:struct Example2)#2 (1) {
+   ["x"]=>
+   int(0)
+ }

@iluuu1994 iluuu1994 closed this in 11d6bea Jul 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants