diff -Naurd mpfr-4.1.0-a/PATCHES mpfr-4.1.0-b/PATCHES
--- mpfr-4.1.0-a/PATCHES	2021-04-23 09:49:34.648281897 +0000
+++ mpfr-4.1.0-b/PATCHES	2021-04-23 09:49:34.696281616 +0000
@@ -0,0 +1 @@
+get_str_ndigits
diff -Naurd mpfr-4.1.0-a/VERSION mpfr-4.1.0-b/VERSION
--- mpfr-4.1.0-a/VERSION	2021-03-09 13:58:00.937650249 +0000
+++ mpfr-4.1.0-b/VERSION	2021-04-23 09:49:34.696281616 +0000
@@ -1 +1 @@
-4.1.0-p11
+4.1.0-p12
diff -Naurd mpfr-4.1.0-a/src/get_str.c mpfr-4.1.0-b/src/get_str.c
--- mpfr-4.1.0-a/src/get_str.c	2020-06-18 17:17:18.000000000 +0000
+++ mpfr-4.1.0-b/src/get_str.c	2021-04-23 09:49:34.680281710 +0000
@@ -2484,6 +2484,8 @@
 size_t
 mpfr_get_str_ndigits (int b, mpfr_prec_t p)
 {
+  MPFR_SAVE_EXPO_DECL (expo);
+
   MPFR_ASSERTN (2 <= b && b <= 62);
 
   /* deal first with power of two bases, since even for those, mpfr_ceil_mul
@@ -2497,17 +2499,26 @@
       return 1 + (p + k - 2) / k;
     }
 
+  MPFR_SAVE_EXPO_MARK (expo);
+
   /* the value returned by mpfr_ceil_mul is guaranteed to be
      1 + ceil(p*log(2)/log(b)) for p < 186564318007 (it returns one more
      for p=186564318007 and b=7 or 49) */
   MPFR_STAT_STATIC_ASSERT (MPFR_PREC_BITS >= 64 || MPFR_PREC_BITS <= 32);
+  if
 #if MPFR_PREC_BITS >= 64
   /* 64-bit numbers are supported by the C implementation, so that we can
      use the large constant below. If MPFR_PREC_BITS <= 32, the condition
      is always satisfied, so that we do not need any test. */
-  if (MPFR_LIKELY (p < 186564318007))
+    (MPFR_LIKELY (p < 186564318007))
+#else
+    (1)
 #endif
-    return 1 + mpfr_ceil_mul (IS_POW2(b) ? p - 1 : p, b, 1);
+  {
+    size_t ret = 1 + mpfr_ceil_mul (IS_POW2(b) ? p - 1 : p, b, 1);
+    MPFR_SAVE_EXPO_FREE (expo);
+    return ret;
+  }
 
   /* Now p is large and b is not a power of two. The code below works for any
      value of p and b, as long as b is not a power of two. Indeed, in such a
@@ -2541,6 +2552,8 @@
         mpfr_clear (d);
         mpfr_clear (u);
       }
+
+    MPFR_SAVE_EXPO_FREE (expo);
     return 1 + ret;
   }
 }
diff -Naurd mpfr-4.1.0-a/src/mpfr.h mpfr-4.1.0-b/src/mpfr.h
--- mpfr-4.1.0-a/src/mpfr.h	2021-03-09 13:58:00.933650293 +0000
+++ mpfr-4.1.0-b/src/mpfr.h	2021-04-23 09:49:34.692281639 +0000
@@ -27,7 +27,7 @@
 #define MPFR_VERSION_MAJOR 4
 #define MPFR_VERSION_MINOR 1
 #define MPFR_VERSION_PATCHLEVEL 0
-#define MPFR_VERSION_STRING "4.1.0-p11"
+#define MPFR_VERSION_STRING "4.1.0-p12"
 
 /* User macros:
    MPFR_USE_FILE:        Define it to make MPFR define functions dealing
diff -Naurd mpfr-4.1.0-a/src/version.c mpfr-4.1.0-b/src/version.c
--- mpfr-4.1.0-a/src/version.c	2021-03-09 13:58:00.933650293 +0000
+++ mpfr-4.1.0-b/src/version.c	2021-04-23 09:49:34.696281616 +0000
@@ -25,5 +25,5 @@
 const char *
 mpfr_get_version (void)
 {
-  return "4.1.0-p11";
+  return "4.1.0-p12";
 }
diff -Naurd mpfr-4.1.0-a/tests/tget_str.c mpfr-4.1.0-b/tests/tget_str.c
--- mpfr-4.1.0-a/tests/tget_str.c	2020-01-08 18:11:13.000000000 +0000
+++ mpfr-4.1.0-b/tests/tget_str.c	2021-04-23 09:49:34.680281710 +0000
@@ -1311,6 +1311,33 @@
   mpfr_clear (x);
 }
 
+static void
+test_ndigits_aux (int b, mpfr_prec_t p, size_t expected_m)
+{
+  size_t m;
+  mpfr_exp_t old_emin, old_emax, e[] = { MPFR_EMIN_MIN, 0, MPFR_EMAX_MAX };
+  mpfr_flags_t flags;
+  int i;
+
+  old_emin = mpfr_get_emin ();
+  old_emax = mpfr_get_emax ();
+
+  i = randlimb () % (numberof (e) + 1);
+  if (i < numberof (e))
+    {
+      set_emin (e[i]);
+      set_emax (e[i]);
+    }
+
+  __gmpfr_flags = flags = randlimb () & MPFR_FLAGS_ALL;
+  m = mpfr_get_str_ndigits (b, p);
+  MPFR_ASSERTN (m == expected_m);
+  MPFR_ASSERTN (__gmpfr_flags == flags);
+
+  set_emin (old_emin);
+  set_emax (old_emax);
+}
+
 /* test of mpfr_get_str_ndigits */
 static void
 test_ndigits (void)
@@ -1319,61 +1346,61 @@
 
   /* for b=2, we have 1 + ceil((p-1)*log(2)/log(b)) = p */
   for (p = MPFR_PREC_MIN; p <= 1024; p++)
-    MPFR_ASSERTN(mpfr_get_str_ndigits (2, p) == p);
+    test_ndigits_aux (2, p, p);
 
   /* for b=4, we have 1 + ceil((p-1)*log(2)/log(b)) = 1 + ceil((p-1)/2)
      = 1 + floor(p/2) */
   for (p = MPFR_PREC_MIN; p <= 1024; p++)
-    MPFR_ASSERTN(mpfr_get_str_ndigits (4, p) == 1 + (p / 2));
+    test_ndigits_aux (4, p, 1 + (p / 2));
 
   /* for b=8, we have 1 + ceil((p-1)*log(2)/log(b)) = 1 + ceil((p-1)/3)
      = 1 + floor((p+1)/3) */
   for (p = MPFR_PREC_MIN; p <= 1024; p++)
-    MPFR_ASSERTN(mpfr_get_str_ndigits (8, p) == 1 + ((p + 1) / 3));
+    test_ndigits_aux (8, p, 1 + ((p + 1) / 3));
 
   /* for b=16, we have 1 + ceil((p-1)*log(2)/log(b)) = 1 + ceil((p-1)/4)
      = 1 + floor((p+2)/4) */
   for (p = MPFR_PREC_MIN; p <= 1024; p++)
-    MPFR_ASSERTN(mpfr_get_str_ndigits (16, p) == 1 + ((p + 2) / 4));
+    test_ndigits_aux (16, p, 1 + ((p + 2) / 4));
 
   /* for b=32, we have 1 + ceil((p-1)*log(2)/log(b)) = 1 + ceil((p-1)/5)
      = 1 + floor((p+3)/5) */
   for (p = MPFR_PREC_MIN; p <= 1024; p++)
-    MPFR_ASSERTN(mpfr_get_str_ndigits (32, p) == 1 + ((p + 3) / 5));
+    test_ndigits_aux (32, p, 1 + ((p + 3) / 5));
 
   /* error < 1e-3 */
-  MPFR_ASSERTN(mpfr_get_str_ndigits (57, 35) == 8);
+  test_ndigits_aux (57, 35, 8);
 
   /* error < 1e-4 */
-  MPFR_ASSERTN(mpfr_get_str_ndigits (31, 649) == 133);
+  test_ndigits_aux (31, 649, 133);
 
   /* error < 1e-5 */
-  MPFR_ASSERTN(mpfr_get_str_ndigits (43, 5041) == 931);
+  test_ndigits_aux (43, 5041, 931);
 
   /* error < 1e-6 */
-  MPFR_ASSERTN(mpfr_get_str_ndigits (41, 17771) == 3319);
+  test_ndigits_aux (41, 17771, 3319);
 
   /* 20th convergent of log(2)/log(3) */
-  MPFR_ASSERTN(mpfr_get_str_ndigits (3, 630138897) == 397573381);
+  test_ndigits_aux (3, 630138897, 397573381);
 
 #if MPFR_PREC_BITS >= 64
   /* 21st convergent of log(2)/log(3) */
-  MPFR_ASSERTN(mpfr_get_str_ndigits (3, 9809721694) == 6189245292);
+  test_ndigits_aux (3, 9809721694, 6189245292);
 
   /* 22nd convergent of log(2)/log(3) */
-  MPFR_ASSERTN(mpfr_get_str_ndigits (3, 10439860591) == 6586818672);
+  test_ndigits_aux (3, 10439860591, 6586818672);
 
   /* 23rd convergent of log(2)/log(3) */
-  MPFR_ASSERTN(mpfr_get_str_ndigits (3, 103768467013) == 65470613322);
+  test_ndigits_aux (3, 103768467013, 65470613322);
 
   /* 24th convergent of log(2)/log(3) */
-  MPFR_ASSERTN(mpfr_get_str_ndigits (3, 217976794617) == 137528045314);
+  test_ndigits_aux (3, 217976794617, 137528045314);
 
-  MPFR_ASSERTN(mpfr_get_str_ndigits (3, 1193652440098) == 753110839882);
+  test_ndigits_aux (3, 1193652440098, 753110839882);
 
-  MPFR_ASSERTN(mpfr_get_str_ndigits (3, 683381996816440) == 431166034846569);
+  test_ndigits_aux (3, 683381996816440, 431166034846569);
 
-  MPFR_ASSERTN(mpfr_get_str_ndigits (7, 186564318007) == 66455550933);
+  test_ndigits_aux (7, 186564318007, 66455550933);
 #endif
 }
 
