From 10374f836908e9030143e9b1ea3d40e9b94027bb Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Wed, 23 Oct 2024 13:28:06 +0300 Subject: [PATCH] Prevent crash in GMPy_MPFR_Format() Closes #522 See https://github.com/aleaxit/gmpy/issues/522#issuecomment-2431541545 --- src/gmpy2_format.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/gmpy2_format.c b/src/gmpy2_format.c index fcb447a5..9e4422cc 100644 --- a/src/gmpy2_format.c +++ b/src/gmpy2_format.c @@ -319,6 +319,11 @@ GMPy_MPFR_Format(PyObject *self, PyObject *args) *(p3) = '\00'; buflen = mpfr_asprintf(&buffer, mpfrfmt, MPFR(self)); + if (buflen == -1) { + RUNTIME_ERROR("The maximum precision for string formatting " + "exceeded. Please use digits() method instead."); + return NULL; + } /* If there isn't a decimal point in the output and the output * only consists of digits, then append .0 */