From 5ae30836134e6f8f50700f54f35b6c58d0cfb3df Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Sat, 19 Oct 2024 12:44:25 +0200 Subject: [PATCH 1/2] Use ZendMM for libgmp As is, ext/gmp uses the default memory allocator of the underlying libgmp; this allocator is fallible, but contrary to ZendMM, it `abort()`s without giving us an opportunity to do a cleaner shutdown. Furthermore, the libgmp allocator obviously doesn't heed memory_limit. Thus we install wrappers of the ZendMM API as libgmp allocator. --- ext/gmp/gmp.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/ext/gmp/gmp.c b/ext/gmp/gmp.c index f3cb4a6ce101..c8b1de057b9a 100644 --- a/ext/gmp/gmp.c +++ b/ext/gmp/gmp.c @@ -521,6 +521,21 @@ static int gmp_unserialize(zval *object, zend_class_entry *ce, const unsigned ch } /* }}} */ +static void *gmp_alloc(size_t size) +{ + return emalloc(size); +} + +static void *gmp_realloc(void *ptr, size_t old_size, size_t new_size) +{ + return erealloc(ptr, new_size); +} + +static void gmp_free(void *ptr, size_t size) +{ + efree(ptr); +} + /* {{{ ZEND_GINIT_FUNCTION */ static ZEND_GINIT_FUNCTION(gmp) { @@ -551,6 +566,8 @@ ZEND_MINIT_FUNCTION(gmp) register_gmp_symbols(module_number); + mp_set_memory_functions(gmp_alloc, gmp_realloc, gmp_free); + return SUCCESS; } /* }}} */ From 0dfb965c55a1c18b56f2d041edb4963e04e7cfcf Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Sat, 19 Oct 2024 13:36:53 +0200 Subject: [PATCH 2/2] Use `efree_size()` --- ext/gmp/gmp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/gmp/gmp.c b/ext/gmp/gmp.c index c8b1de057b9a..3198215074e2 100644 --- a/ext/gmp/gmp.c +++ b/ext/gmp/gmp.c @@ -533,7 +533,7 @@ static void *gmp_realloc(void *ptr, size_t old_size, size_t new_size) static void gmp_free(void *ptr, size_t size) { - efree(ptr); + efree_size(ptr, size); } /* {{{ ZEND_GINIT_FUNCTION */