[php-src] Issue #14286: ffi enum type make memory leak
| From: | avriltank | Date: | Tue, 21 May 2024 07:26:06 +0000 |
| Subject: | [php-src] Issue #14286: ffi enum type make memory leak | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-246937@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/14286
Author: avriltank
### Description
The following code:
```php
<?php
```
Resulted in this output:
```php
<?php
$ffi = FFI::cdef("
enum{
//enum TestEnum{ //if enum has a name there is no memory leak
TEST_ONE=0,
TEST_TWO=1,
};
");
echo $ffi->TEST_TWO; //echo 1
//the result is right but memory leak
```
i found the c ffi code when enum has no name make memory leak
```
void zend_ffi_make_enum_type(zend_ffi_dcl *dcl) /* {{{ */
{
zend_ffi_type *type = pemalloc(sizeof(zend_ffi_type), FFI_G(persistent));
type->kind = ZEND_FFI_TYPE_ENUM;
type->attr = FFI_G(default_type_attr) | (dcl->attr & ZEND_FFI_ENUM_ATTRS);
type->enumeration.tag_name = NULL;
if (type->attr & ZEND_FFI_ATTR_PACKED) {
type->size = zend_ffi_type_uint8.size;
type->align = zend_ffi_type_uint8.align;
type->enumeration.kind = ZEND_FFI_TYPE_UINT8;
} else {
type->size = zend_ffi_type_uint32.size;
type->align = zend_ffi_type_uint32.align;
type->enumeration.kind = ZEND_FFI_TYPE_UINT32;
}
dcl->type = ZEND_FFI_TYPE_MAKE_OWNED(type);
dcl->attr &= ~ZEND_FFI_ENUM_ATTRS;
}
```

### PHP Version
php 8.3
### Operating System
windows,linux