Skip to content

Commit 6ebc9aa

Browse files
committed
Disallow invoking GMP constructor
Fixes GH-10155
1 parent d952419 commit 6ebc9aa

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

Zend/tests/temporary_cleaning_014.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ gmp
66
<?php
77
set_error_handler(function() { throw new Exception; });
88
try {
9-
new GMP ?: null;
9+
gmp_init(0) ?: null;
1010
} catch (Exception $e) {
1111
}
1212
?>

ext/gmp/gmp.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,12 @@ static inline void gmp_create(zval *target, mpz_ptr *gmpnum_target) /* {{{ */
274274
}
275275
/* }}} */
276276

277+
static zend_function *gmp_get_constructor(zend_object *object)
278+
{
279+
zend_throw_error(NULL, "Cannot directly construct GMP, use gmp_init() instead");
280+
return NULL;
281+
}
282+
277283
static zend_result gmp_cast_object(zend_object *readobj, zval *writeobj, int type) /* {{{ */
278284
{
279285
mpz_ptr gmpnum;
@@ -539,6 +545,7 @@ ZEND_MINIT_FUNCTION(gmp)
539545
memcpy(&gmp_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
540546
gmp_object_handlers.offset = XtOffsetOf(gmp_object, std);
541547
gmp_object_handlers.free_obj = gmp_free_object_storage;
548+
gmp_object_handlers.get_constructor = gmp_get_constructor;
542549
gmp_object_handlers.cast_object = gmp_cast_object;
543550
gmp_object_handlers.get_debug_info = gmp_get_debug_info;
544551
gmp_object_handlers.clone_obj = gmp_clone_obj;

ext/gmp/tests/bug-gh10155.phpt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
GMP constructor should not be called directly
3+
--EXTENSIONS--
4+
gmp
5+
--FILE--
6+
<?php
7+
try {
8+
var_dump(new GMP(6));
9+
} catch (Error $e) {
10+
echo $e->getMessage(), "\n";
11+
}
12+
?>
13+
--EXPECT--
14+
Cannot directly construct GMP, use gmp_init() instead

ext/gmp/tests/gmp_dynamic_property.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ gmp
55
--FILE--
66
<?php
77

8-
$g = new GMP();
8+
$g = gmp_init(0);
99
$g->{1} = 123;
1010

1111
$serialized = serialize($g);

0 commit comments

Comments
 (0)