Skip to content

Disallow invoking GMP constructor #10158

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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Zend/tests/temporary_cleaning_014.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ gmp
<?php
set_error_handler(function() { throw new Exception; });
try {
new GMP ?: null;
gmp_init(0) ?: null;
} catch (Exception $e) {
}
?>
Expand Down
7 changes: 7 additions & 0 deletions ext/gmp/gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,12 @@ static inline void gmp_create(zval *target, mpz_ptr *gmpnum_target) /* {{{ */
}
/* }}} */

static zend_function *gmp_get_constructor(zend_object *object)
{
zend_throw_error(NULL, "Cannot directly construct GMP, use gmp_init() instead");
return NULL;
}

static int gmp_cast_object(zend_object *readobj, zval *writeobj, int type) /* {{{ */
{
mpz_ptr gmpnum;
Expand Down Expand Up @@ -534,6 +540,7 @@ ZEND_MINIT_FUNCTION(gmp)
memcpy(&gmp_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
gmp_object_handlers.offset = XtOffsetOf(gmp_object, std);
gmp_object_handlers.free_obj = gmp_free_object_storage;
gmp_object_handlers.get_constructor = gmp_get_constructor;
gmp_object_handlers.cast_object = gmp_cast_object;
gmp_object_handlers.get_debug_info = gmp_get_debug_info;
gmp_object_handlers.clone_obj = gmp_clone_obj;
Expand Down
14 changes: 14 additions & 0 deletions ext/gmp/tests/bug-gh10155.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
GMP constructor should not be called directly
--EXTENSIONS--
gmp
--FILE--
<?php
try {
var_dump(new GMP(6));
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
?>
--EXPECT--
Cannot directly construct GMP, use gmp_init() instead