com php-src: Fixed bug #77707 (FFI loses type information for opaque types): ext/ffi/ffi.c ext/ffi/tests/003.phpt ext/ffi/tests/004.phpt
ext/ffi/tests/017.phpt ext/ffi/tests/024.phpt
| From: | Dmitry Stogov | Date: | Mon, 11 Mar 2019 11:26:17 +0000 |
| Subject: | com php-src: Fixed bug #77707 (FFI loses type information for opaque types): ext/ffi/ffi.c ext/ffi/tests/003.phpt ext/ffi/tests/004.phpt ext/ffi/tests/017.phpt ext/ffi/tests/024.phpt |
||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-105072@lists.php.net to get a copy of this message | ||
Commit: e623df650fd099325154ffd43cc19002e1cb619e
Author: Dmitry Stogov <dmitry@zend.com> Mon, 11 Mar 2019 14:26:17 +0300
Parents: a1868d22917ac189dde1f6974bc43e1c498e1c6a
Branches: PHP-7.4 master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=e623df650fd099325154ffd43cc19002e1cb619e
Log:
Fixed bug #77707 (FFI loses type information for opaque types)
Bugs:
https://bugs.php.net/77707
Changed paths:
M ext/ffi/ffi.c
M ext/ffi/tests/003.phpt
M ext/ffi/tests/004.phpt
M ext/ffi/tests/017.phpt
M ext/ffi/tests/024.phpt
Diff:
diff --git a/ext/ffi/ffi.c b/ext/ffi/ffi.c
index d9c7d3f..f9c7fe0 100644
--- a/ext/ffi/ffi.c
+++ b/ext/ffi/ffi.c
@@ -88,7 +88,8 @@ struct _zend_ffi_type {
uint32_t attr;
union {
struct {
- zend_ffi_type_kind kind;
+ zend_string *tag_name;
+ zend_ffi_type_kind kind;
} enumeration;
struct {
zend_ffi_type *type;
@@ -98,6 +99,7 @@ struct _zend_ffi_type {
zend_ffi_type *type;
} pointer;
struct {
+ zend_string *tag_name;
HashTable fields;
} record;
struct {
@@ -1263,7 +1265,12 @@ static int zend_ffi_ctype_name(zend_ffi_ctype_name_buf *buf, const
zend_ffi_type
name = "int64_t";
break;
case ZEND_FFI_TYPE_ENUM:
- name = "<enum>";
+ if (type->enumeration.tag_name) {
+ zend_ffi_ctype_name_prepend(buf, ZSTR_VAL(type->enumeration.tag_name),
ZSTR_LEN(type->enumeration.tag_name));
+ } else {
+ zend_ffi_ctype_name_prepend(buf, "<anonymous>",
sizeof("<anonymous>")-1);
+ }
+ name = "enum ";
break;
case ZEND_FFI_TYPE_BOOL:
name = "bool";
@@ -1322,9 +1329,19 @@ static int zend_ffi_ctype_name(zend_ffi_ctype_name_buf *buf, const
zend_ffi_type
break;
case ZEND_FFI_TYPE_STRUCT:
if (type->attr & ZEND_FFI_ATTR_UNION) {
- name = "<union>";
+ if (type->record.tag_name) {
+ zend_ffi_ctype_name_prepend(buf, ZSTR_VAL(type->record.tag_name),
ZSTR_LEN(type->record.tag_name));
+ } else {
+ zend_ffi_ctype_name_prepend(buf, "<anonymous>",
sizeof("<anonymous>")-1);
+ }
+ name = "union ";
} else {
- name = "<struct>";
+ if (type->record.tag_name) {
+ zend_ffi_ctype_name_prepend(buf, ZSTR_VAL(type->record.tag_name),
ZSTR_LEN(type->record.tag_name));
+ } else {
+ zend_ffi_ctype_name_prepend(buf, "<anonymous>",
sizeof("<anonymous>")-1);
+ }
+ name = "struct ";
}
break;
default:
@@ -1984,7 +2001,15 @@ static void _zend_ffi_type_dtor(zend_ffi_type *type) /* {{{ */
type = ZEND_FFI_TYPE(type);
switch (type->kind) {
+ case ZEND_FFI_TYPE_ENUM:
+ if (type->enumeration.tag_name) {
+ zend_string_release(type->enumeration.tag_name);
+ }
+ break;
case ZEND_FFI_TYPE_STRUCT:
+ if (type->record.tag_name) {
+ zend_string_release(type->record.tag_name);
+ }
zend_hash_destroy(&type->record.fields);
break;
case ZEND_FFI_TYPE_POINTER:
@@ -5064,6 +5089,7 @@ 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;
@@ -5199,6 +5225,7 @@ void zend_ffi_make_struct_type(zend_ffi_dcl *dcl) /* {{{ */
type->attr |= ZEND_FFI_ATTR_UNION;
}
dcl->type = ZEND_FFI_TYPE_MAKE_OWNED(type);
+ type->record.tag_name = NULL;
zend_hash_init(&type->record.fields, 0, NULL, FFI_G(persistent) ?
zend_ffi_field_hash_persistent_dtor :zend_ffi_field_hash_dtor, FFI_G(persistent));
dcl->attr &= ~ZEND_FFI_STRUCT_ATTRS;
dcl->align = 0;
@@ -5765,6 +5792,7 @@ void zend_ffi_declare(const char *name, size_t name_len, zend_ffi_dcl *dcl) /*
{
void zend_ffi_declare_tag(const char *name, size_t name_len, zend_ffi_dcl *dcl, zend_bool
incomplete) /* {{{ */
{
zend_ffi_tag *tag;
+ zend_ffi_type *type;
if (!FFI_G(tags)) {
FFI_G(tags) = pemalloc(sizeof(HashTable), FFI_G(persistent));
@@ -5808,16 +5836,23 @@ void zend_ffi_declare_tag(const char *name, size_t name_len, zend_ffi_dcl
*dcl,
}
} else {
zend_ffi_tag *tag = pemalloc(sizeof(zend_ffi_tag), FFI_G(persistent));
+ zend_string *tag_name = zend_string_init(name, name_len, FFI_G(persistent));
if (dcl->flags & ZEND_FFI_DCL_STRUCT) {
tag->kind = ZEND_FFI_TAG_STRUCT;
zend_ffi_make_struct_type(dcl);
+ type = ZEND_FFI_TYPE(dcl->type);
+ type->record.tag_name = zend_string_copy(tag_name);
} else if (dcl->flags & ZEND_FFI_DCL_UNION) {
tag->kind = ZEND_FFI_TAG_UNION;
zend_ffi_make_struct_type(dcl);
+ type = ZEND_FFI_TYPE(dcl->type);
+ type->record.tag_name = zend_string_copy(tag_name);
} else if (dcl->flags & ZEND_FFI_DCL_ENUM) {
tag->kind = ZEND_FFI_TAG_ENUM;
zend_ffi_make_enum_type(dcl);
+ type = ZEND_FFI_TYPE(dcl->type);
+ type->enumeration.tag_name = zend_string_copy(tag_name);
} else {
ZEND_ASSERT(0);
}
@@ -5826,7 +5861,8 @@ void zend_ffi_declare_tag(const char *name, size_t name_len, zend_ffi_dcl
*dcl,
if (incomplete) {
dcl->type->attr |= ZEND_FFI_ATTR_INCOMPLETE_TAG;
}
- zend_hash_str_add_new_ptr(FFI_G(tags), name, name_len, tag);
+ zend_hash_add_new_ptr(FFI_G(tags), tag_name, tag);
+ zend_string_release(tag_name);
}
}
/* }}} */
diff --git a/ext/ffi/tests/003.phpt b/ext/ffi/tests/003.phpt
index 89c489e..d52e235 100644
--- a/ext/ffi/tests/003.phpt
+++ b/ext/ffi/tests/003.phpt
@@ -42,19 +42,19 @@ try {
echo "ok\n";
?>
--EXPECTF--
-object(FFI\CData:<struct>)#%d (1) {
+object(FFI\CData:struct _a)#%d (1) {
["x"]=>
int(0)
}
-object(FFI\CData:<struct>)#%d (1) {
+object(FFI\CData:struct _b)#%d (1) {
["x"]=>
int(0)
}
-object(FFI\CData:<struct>)#%d (1) {
+object(FFI\CData:struct _c)#%d (1) {
["x"]=>
int(0)
}
-object(FFI\CData:<struct>)#%d (1) {
+object(FFI\CData:struct _d)#%d (1) {
["x"]=>
int(0)
}
diff --git a/ext/ffi/tests/004.phpt b/ext/ffi/tests/004.phpt
index 3222208..e914b60 100644
--- a/ext/ffi/tests/004.phpt
+++ b/ext/ffi/tests/004.phpt
@@ -52,19 +52,19 @@ try {
echo "ok\n";
?>
--EXPECTF--
-object(FFI\CData:<enum>)#%d (1) {
+object(FFI\CData:enum _a)#%d (1) {
["cdata"]=>
int(0)
}
-object(FFI\CData:<enum>)#%d (1) {
+object(FFI\CData:enum _b)#%d (1) {
["cdata"]=>
int(0)
}
-object(FFI\CData:<enum>)#%d (1) {
+object(FFI\CData:enum _c)#%d (1) {
["cdata"]=>
int(0)
}
-object(FFI\CData:<enum>)#%d (1) {
+object(FFI\CData:enum _d)#%d (1) {
["cdata"]=>
int(0)
}
diff --git a/ext/ffi/tests/017.phpt b/ext/ffi/tests/017.phpt
index 1ae6ddf..718b078 100644
--- a/ext/ffi/tests/017.phpt
+++ b/ext/ffi/tests/017.phpt
@@ -26,7 +26,7 @@ ok
--EXPECTF--
FFI\ParserException: 'function' type is not allowed at line 1
FFI\ParserException: struct/union can't contain an instance of itself at line 1
-object(FFI\CData:<struct>)#%d (1) {
+object(FFI\CData:struct X)#%d (1) {
["ptr"]=>
NULL
}
diff --git a/ext/ffi/tests/024.phpt b/ext/ffi/tests/024.phpt
index 15ef704..95867b4 100644
--- a/ext/ffi/tests/024.phpt
+++ b/ext/ffi/tests/024.phpt
@@ -29,7 +29,7 @@ ffi.enable=1
?>
--EXPECTF--
int(20)
-object(FFI\CData:<struct>)#%d (6) {
+object(FFI\CData:struct <anonymous>)#%d (6) {
["a"]=>
int(1)
["b"]=>