Skip to content

Commit 6a91fab

Browse files
committed
Disallow invoking GMP constructor
Fixes GH-10155
1 parent adc2382 commit 6a91fab

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
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
@@ -270,6 +270,12 @@ static inline void gmp_create(zval *target, mpz_ptr *gmpnum_target) /* {{{ */
270270
}
271271
/* }}} */
272272

273+
static zend_function *gmp_get_constructor(zend_object *object)
274+
{
275+
zend_throw_error(NULL, "Cannot directly construct GMP, use gmp_init() instead");
276+
return NULL;
277+
}
278+
273279
static int gmp_cast_object(zend_object *readobj, zval *writeobj, int type) /* {{{ */
274280
{
275281
mpz_ptr gmpnum;
@@ -534,6 +540,7 @@ ZEND_MINIT_FUNCTION(gmp)
534540
memcpy(&gmp_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
535541
gmp_object_handlers.offset = XtOffsetOf(gmp_object, std);
536542
gmp_object_handlers.free_obj = gmp_free_object_storage;
543+
gmp_object_handlers.get_constructor = gmp_get_constructor;
537544
gmp_object_handlers.cast_object = gmp_cast_object;
538545
gmp_object_handlers.get_debug_info = gmp_get_debug_info;
539546
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

0 commit comments

Comments
 (0)