# MPFR v3.1.5 patch dated 2017-06-06
# downloaded via https://gforge.inria.fr/frs/?group_id=136
diff -Naurd mpfr-3.1.5-a/PATCHES mpfr-3.1.5-b/PATCHES
--- mpfr-3.1.5-a/PATCHES	2016-12-15 08:35:46.476430238 +0000
+++ mpfr-3.1.5-b/PATCHES	2016-12-15 08:35:46.544430346 +0000
@@ -0,0 +1 @@
+vasprintf
diff -Naurd mpfr-3.1.5-a/VERSION mpfr-3.1.5-b/VERSION
--- mpfr-3.1.5-a/VERSION	2016-09-27 07:58:14.000000000 +0000
+++ mpfr-3.1.5-b/VERSION	2016-12-15 08:35:46.544430346 +0000
@@ -1 +1 @@
-3.1.5
+3.1.5-p1
diff -Naurd mpfr-3.1.5-a/src/mpfr.h mpfr-3.1.5-b/src/mpfr.h
--- mpfr-3.1.5-a/src/mpfr.h	2016-09-27 07:58:15.000000000 +0000
+++ mpfr-3.1.5-b/src/mpfr.h	2016-12-15 08:35:46.540430340 +0000
@@ -27,7 +27,7 @@
 #define MPFR_VERSION_MAJOR 3
 #define MPFR_VERSION_MINOR 1
 #define MPFR_VERSION_PATCHLEVEL 5
-#define MPFR_VERSION_STRING "3.1.5"
+#define MPFR_VERSION_STRING "3.1.5-p1"
 
 /* Macros dealing with MPFR VERSION */
 #define MPFR_VERSION_NUM(a,b,c) (((a) << 16L) | ((b) << 8) | (c))
diff -Naurd mpfr-3.1.5-a/src/vasprintf.c mpfr-3.1.5-b/src/vasprintf.c
--- mpfr-3.1.5-a/src/vasprintf.c	2016-09-27 07:58:15.000000000 +0000
+++ mpfr-3.1.5-b/src/vasprintf.c	2016-12-15 08:35:46.520430308 +0000
@@ -1593,7 +1593,7 @@
         }
       else if (spec.spec == 'f' || spec.spec == 'F')
         {
-          if (spec.prec == -1)
+          if (spec.prec < 0)
             spec.prec = 6;
           if (regular_fg (np, p, spec, NULL) == -1)
             goto error;
diff -Naurd mpfr-3.1.5-a/src/version.c mpfr-3.1.5-b/src/version.c
--- mpfr-3.1.5-a/src/version.c	2016-09-27 07:58:15.000000000 +0000
+++ mpfr-3.1.5-b/src/version.c	2016-12-15 08:35:46.544430346 +0000
@@ -25,5 +25,5 @@
 const char *
 mpfr_get_version (void)
 {
-  return "3.1.5";
+  return "3.1.5-p1";
 }
diff -Naurd mpfr-3.1.5-a/tests/tsprintf.c mpfr-3.1.5-b/tests/tsprintf.c
--- mpfr-3.1.5-a/tests/tsprintf.c	2016-09-27 07:58:14.000000000 +0000
+++ mpfr-3.1.5-b/tests/tsprintf.c	2016-12-15 08:35:46.520430308 +0000
@@ -1251,6 +1251,25 @@
   check_emin_aux (MPFR_EMIN_MIN);
 }
 
+static void
+test20161214 (void)
+{
+  mpfr_t x;
+  char buf[32];
+  const char s[] = "0x0.fffffffffffff8p+1024";
+  int r;
+
+  mpfr_init2 (x, 64);
+  mpfr_set_str (x, s, 16, MPFR_RNDN);
+  r = mpfr_snprintf (buf, 32, "%.*RDf", -2, x);
+  MPFR_ASSERTN(r == 316);
+  r = mpfr_snprintf (buf, 32, "%.*RDf", INT_MIN + 1, x);
+  MPFR_ASSERTN(r == 316);
+  r = mpfr_snprintf (buf, 32, "%.*RDf", INT_MIN, x);
+  MPFR_ASSERTN(r == 316);
+  mpfr_clear (x);
+}
+
 int
 main (int argc, char **argv)
 {
@@ -1271,6 +1290,7 @@
   mixed ();
   check_emax ();
   check_emin ();
+  test20161214 ();
 
 #if defined(HAVE_LOCALE_H) && defined(HAVE_SETLOCALE)
 #if MPFR_LCONV_DPTS
diff -Naurd mpfr-3.1.5-a/PATCHES mpfr-3.1.5-b/PATCHES
--- mpfr-3.1.5-a/PATCHES	2016-12-19 22:11:17.022676737 +0000
+++ mpfr-3.1.5-b/PATCHES	2016-12-19 22:11:17.094676820 +0000
@@ -0,0 +1 @@
+strtofr
diff -Naurd mpfr-3.1.5-a/VERSION mpfr-3.1.5-b/VERSION
--- mpfr-3.1.5-a/VERSION	2016-12-15 08:35:46.544430346 +0000
+++ mpfr-3.1.5-b/VERSION	2016-12-19 22:11:17.094676820 +0000
@@ -1 +1 @@
-3.1.5-p1
+3.1.5-p2
diff -Naurd mpfr-3.1.5-a/src/mpfr.h mpfr-3.1.5-b/src/mpfr.h
--- mpfr-3.1.5-a/src/mpfr.h	2016-12-15 08:35:46.540430340 +0000
+++ mpfr-3.1.5-b/src/mpfr.h	2016-12-19 22:11:17.090676815 +0000
@@ -27,7 +27,7 @@
 #define MPFR_VERSION_MAJOR 3
 #define MPFR_VERSION_MINOR 1
 #define MPFR_VERSION_PATCHLEVEL 5
-#define MPFR_VERSION_STRING "3.1.5-p1"
+#define MPFR_VERSION_STRING "3.1.5-p2"
 
 /* Macros dealing with MPFR VERSION */
 #define MPFR_VERSION_NUM(a,b,c) (((a) << 16L) | ((b) << 8) | (c))
diff -Naurd mpfr-3.1.5-a/src/strtofr.c mpfr-3.1.5-b/src/strtofr.c
--- mpfr-3.1.5-a/src/strtofr.c	2016-09-27 07:58:15.000000000 +0000
+++ mpfr-3.1.5-b/src/strtofr.c	2016-12-19 22:11:17.066676788 +0000
@@ -743,11 +743,14 @@
          of the pstr_size most significant digits of pstr->mant, with
          equality in case exact is non-zero. */
 
-      /* test if rounding is possible, and if so exit the loop */
-      if (exact || mpfr_can_round_raw (result, ysize,
-                                       (pstr->negative) ? -1 : 1,
-                                       ysize_bits - err - 1,
-                                       MPFR_RNDN, rnd, MPFR_PREC(x)))
+      /* test if rounding is possible, and if so exit the loop.
+         Note: we also need to be able to determine the correct ternary value,
+         thus we use the MPFR_PREC(x) + (rnd == MPFR_RNDN) trick.
+         For example if result = xxx...xxx111...111 and rnd = RNDN,
+         then we know the correct rounding is xxx...xx(x+1), but we cannot know
+         the correct ternary value. */
+      if (exact || mpfr_round_p (result, ysize, ysize_bits - err - 1,
+                                 MPFR_PREC(x) + (rnd == MPFR_RNDN)))
         break;
 
     next_loop:
diff -Naurd mpfr-3.1.5-a/src/version.c mpfr-3.1.5-b/src/version.c
--- mpfr-3.1.5-a/src/version.c	2016-12-15 08:35:46.544430346 +0000
+++ mpfr-3.1.5-b/src/version.c	2016-12-19 22:11:17.094676820 +0000
@@ -25,5 +25,5 @@
 const char *
 mpfr_get_version (void)
 {
-  return "3.1.5-p1";
+  return "3.1.5-p2";
 }
diff -Naurd mpfr-3.1.5-a/tests/tstrtofr.c mpfr-3.1.5-b/tests/tstrtofr.c
--- mpfr-3.1.5-a/tests/tstrtofr.c	2016-09-27 07:58:14.000000000 +0000
+++ mpfr-3.1.5-b/tests/tstrtofr.c	2016-12-19 22:11:17.066676788 +0000
@@ -1191,6 +1191,24 @@
   mpfr_clears (e, x1, x2, (mpfr_ptr) 0);
 }
 
+/* Note: the number is 5^47/2^9. */
+static void
+bug20161217 (void)
+{
+  mpfr_t fp, z;
+  static const char * num = "0.1387778780781445675529539585113525390625e31";
+  int inex;
+
+  mpfr_init2 (fp, 110);
+  mpfr_init2 (z, 110);
+  inex = mpfr_strtofr (fp, num, NULL, 10, MPFR_RNDN);
+  MPFR_ASSERTN(inex == 0);
+  mpfr_set_str_binary (z, "10001100001000010011110110011101101001010000001011011110010001010100010100100110111101000010001011001100001101E-9");
+  MPFR_ASSERTN(mpfr_equal_p (fp, z));
+  mpfr_clear (fp);
+  mpfr_clear (z);
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -1205,6 +1223,7 @@
   test20100310 ();
   bug20120814 ();
   bug20120829 ();
+  bug20161217 ();
 
   tests_end_mpfr ();
   return 0;
diff -Naurd mpfr-3.1.5-a/PATCHES mpfr-3.1.5-b/PATCHES
--- mpfr-3.1.5-a/PATCHES	2017-06-06 19:21:17.580843571 +0000
+++ mpfr-3.1.5-b/PATCHES	2017-06-06 19:21:17.604843293 +0000
@@ -0,0 +1 @@
+ret-macro
diff -Naurd mpfr-3.1.5-a/VERSION mpfr-3.1.5-b/VERSION
--- mpfr-3.1.5-a/VERSION	2016-12-19 22:11:17.094676820 +0000
+++ mpfr-3.1.5-b/VERSION	2017-06-06 19:21:17.604843293 +0000
@@ -1 +1 @@
-3.1.5-p2
+3.1.5-p3
diff -Naurd mpfr-3.1.5-a/src/mpfr-impl.h mpfr-3.1.5-b/src/mpfr-impl.h
--- mpfr-3.1.5-a/src/mpfr-impl.h	2016-09-27 07:58:15.000000000 +0000
+++ mpfr-3.1.5-b/src/mpfr-impl.h	2017-06-06 19:21:17.592843433 +0000
@@ -873,7 +873,7 @@
    following two macros, unless the flag comes from another function
    returning the ternary inexact value */
 #define MPFR_RET(I) return \
-  (I) ? ((__gmpfr_flags |= MPFR_FLAGS_INEXACT), (I)) : 0
+  (I) != 0 ? ((__gmpfr_flags |= MPFR_FLAGS_INEXACT), (I)) : 0
 #define MPFR_RET_NAN return (__gmpfr_flags |= MPFR_FLAGS_NAN), 0
 
 #define MPFR_SET_ERANGE() (__gmpfr_flags |= MPFR_FLAGS_ERANGE)
diff -Naurd mpfr-3.1.5-a/src/mpfr.h mpfr-3.1.5-b/src/mpfr.h
--- mpfr-3.1.5-a/src/mpfr.h	2016-12-19 22:11:17.090676815 +0000
+++ mpfr-3.1.5-b/src/mpfr.h	2017-06-06 19:21:17.600843340 +0000
@@ -27,7 +27,7 @@
 #define MPFR_VERSION_MAJOR 3
 #define MPFR_VERSION_MINOR 1
 #define MPFR_VERSION_PATCHLEVEL 5
-#define MPFR_VERSION_STRING "3.1.5-p2"
+#define MPFR_VERSION_STRING "3.1.5-p3"
 
 /* Macros dealing with MPFR VERSION */
 #define MPFR_VERSION_NUM(a,b,c) (((a) << 16L) | ((b) << 8) | (c))
diff -Naurd mpfr-3.1.5-a/src/version.c mpfr-3.1.5-b/src/version.c
--- mpfr-3.1.5-a/src/version.c	2016-12-19 22:11:17.094676820 +0000
+++ mpfr-3.1.5-b/src/version.c	2017-06-06 19:21:17.604843293 +0000
@@ -25,5 +25,5 @@
 const char *
 mpfr_get_version (void)
 {
-  return "3.1.5-p2";
+  return "3.1.5-p3";
 }
diff -Naurd mpfr-3.1.5-a/PATCHES mpfr-3.1.5-b/PATCHES
--- mpfr-3.1.5-a/PATCHES	2017-06-06 19:50:30.708438500 +0000
+++ mpfr-3.1.5-b/PATCHES	2017-06-06 19:50:30.736438175 +0000
@@ -0,0 +1 @@
+tests-buffer-size
diff -Naurd mpfr-3.1.5-a/VERSION mpfr-3.1.5-b/VERSION
--- mpfr-3.1.5-a/VERSION	2017-06-06 19:21:17.604843293 +0000
+++ mpfr-3.1.5-b/VERSION	2017-06-06 19:50:30.736438175 +0000
@@ -1 +1 @@
-3.1.5-p3
+3.1.5-p4
diff -Naurd mpfr-3.1.5-a/src/mpfr.h mpfr-3.1.5-b/src/mpfr.h
--- mpfr-3.1.5-a/src/mpfr.h	2017-06-06 19:21:17.600843340 +0000
+++ mpfr-3.1.5-b/src/mpfr.h	2017-06-06 19:50:30.732438221 +0000
@@ -27,7 +27,7 @@
 #define MPFR_VERSION_MAJOR 3
 #define MPFR_VERSION_MINOR 1
 #define MPFR_VERSION_PATCHLEVEL 5
-#define MPFR_VERSION_STRING "3.1.5-p3"
+#define MPFR_VERSION_STRING "3.1.5-p4"
 
 /* Macros dealing with MPFR VERSION */
 #define MPFR_VERSION_NUM(a,b,c) (((a) << 16L) | ((b) << 8) | (c))
diff -Naurd mpfr-3.1.5-a/src/version.c mpfr-3.1.5-b/src/version.c
--- mpfr-3.1.5-a/src/version.c	2017-06-06 19:21:17.604843293 +0000
+++ mpfr-3.1.5-b/src/version.c	2017-06-06 19:50:30.736438175 +0000
@@ -25,5 +25,5 @@
 const char *
 mpfr_get_version (void)
 {
-  return "3.1.5-p3";
+  return "3.1.5-p4";
 }
diff -Naurd mpfr-3.1.5-a/tests/tl2b.c mpfr-3.1.5-b/tests/tl2b.c
--- mpfr-3.1.5-a/tests/tl2b.c	2016-09-27 07:58:14.000000000 +0000
+++ mpfr-3.1.5-b/tests/tl2b.c	2017-06-06 19:50:30.724438314 +0000
@@ -83,7 +83,7 @@
   mpfr_srcptr t;
   int beta, i;
   int error = 0;
-  char buffer[30];
+  char buffer[256];  /* larger than needed, for maintainability */
 
   for (beta = 2; beta <= BASE_MAX; beta++)
     {
diff -Naurd mpfr-3.1.5-a/tests/tpow_all.c mpfr-3.1.5-b/tests/tpow_all.c
--- mpfr-3.1.5-a/tests/tpow_all.c	2016-09-27 07:58:14.000000000 +0000
+++ mpfr-3.1.5-b/tests/tpow_all.c	2017-06-06 19:50:30.724438314 +0000
@@ -498,7 +498,7 @@
   for (i = 0; i <= 12; i++)
     {
       unsigned int flags = 0;
-      char sy[16];
+      char sy[256];  /* larger than needed, for maintainability */
 
       /* Test 2^(emin - i/4).
        * --> Underflow iff i > 4.
diff -Naurd mpfr-3.1.5-a/PATCHES mpfr-3.1.5-b/PATCHES
--- mpfr-3.1.5-a/PATCHES	2017-06-06 19:57:01.947910247 +0000
+++ mpfr-3.1.5-b/PATCHES	2017-06-06 19:57:01.971909970 +0000
@@ -0,0 +1 @@
+vasprintf-overflow-check
diff -Naurd mpfr-3.1.5-a/VERSION mpfr-3.1.5-b/VERSION
--- mpfr-3.1.5-a/VERSION	2017-06-06 19:50:30.736438175 +0000
+++ mpfr-3.1.5-b/VERSION	2017-06-06 19:57:01.971909970 +0000
@@ -1 +1 @@
-3.1.5-p4
+3.1.5-p5
diff -Naurd mpfr-3.1.5-a/src/mpfr.h mpfr-3.1.5-b/src/mpfr.h
--- mpfr-3.1.5-a/src/mpfr.h	2017-06-06 19:50:30.732438221 +0000
+++ mpfr-3.1.5-b/src/mpfr.h	2017-06-06 19:57:01.971909970 +0000
@@ -27,7 +27,7 @@
 #define MPFR_VERSION_MAJOR 3
 #define MPFR_VERSION_MINOR 1
 #define MPFR_VERSION_PATCHLEVEL 5
-#define MPFR_VERSION_STRING "3.1.5-p4"
+#define MPFR_VERSION_STRING "3.1.5-p5"
 
 /* Macros dealing with MPFR VERSION */
 #define MPFR_VERSION_NUM(a,b,c) (((a) << 16L) | ((b) << 8) | (c))
diff -Naurd mpfr-3.1.5-a/src/vasprintf.c mpfr-3.1.5-b/src/vasprintf.c
--- mpfr-3.1.5-a/src/vasprintf.c	2016-12-15 08:35:46.520430308 +0000
+++ mpfr-3.1.5-b/src/vasprintf.c	2017-06-06 19:57:01.963910062 +0000
@@ -1452,7 +1452,7 @@
                   struct printf_spec spec)
 {
   char *str;
-  long total;
+  unsigned int total;  /* can hold the sum of two non-negative int's + 1 */
   int uppercase;
 
   /* WARNING: left justification means right space padding */
@@ -1645,43 +1645,43 @@
 
   /* compute the number of characters to be written verifying it is not too
      much */
+
+#define INCR_TOTAL(v)                           \
+  do {                                          \
+    MPFR_ASSERTD ((v) >= 0);                    \
+    if (MPFR_UNLIKELY ((v) > INT_MAX))          \
+      goto error;                               \
+    total += (v);                               \
+    if (MPFR_UNLIKELY (total > INT_MAX))        \
+      goto error;                               \
+  } while (0)
+
   total = np->sign ? 1 : 0;
-  total += np->prefix_size;
-  total += np->ip_size;
-  if (MPFR_UNLIKELY (total < 0 || total > INT_MAX))
-    goto error;
-  total += np->ip_trailing_zeros;
-  if (MPFR_UNLIKELY (total < 0 || total > INT_MAX))
-    goto error;
+  INCR_TOTAL (np->prefix_size);
+  INCR_TOTAL (np->ip_size);
+  INCR_TOTAL (np->ip_trailing_zeros);
+  MPFR_ASSERTD (np->ip_size + np->ip_trailing_zeros >= 1);
   if (np->thousands_sep)
     /* ' flag, style f and the thousands separator in current locale is not
        reduced to the null character */
-    total += (np->ip_size + np->ip_trailing_zeros) / 3;
-  if (MPFR_UNLIKELY (total < 0 || total > INT_MAX))
-    goto error;
+    INCR_TOTAL ((np->ip_size + np->ip_trailing_zeros - 1) / 3);
   if (np->point)
     ++total;
-  total += np->fp_leading_zeros;
-  if (MPFR_UNLIKELY (total < 0 || total > INT_MAX))
-    goto error;
-  total += np->fp_size;
-  if (MPFR_UNLIKELY (total < 0 || total > INT_MAX))
-    goto error;
-  total += np->fp_trailing_zeros;
-  if (MPFR_UNLIKELY (total < 0 || total > INT_MAX))
-    goto error;
-  total += np->exp_size;
-  if (MPFR_UNLIKELY (total < 0 || total > INT_MAX))
-    goto error;
+  INCR_TOTAL (np->fp_leading_zeros);
+  INCR_TOTAL (np->fp_size);
+  INCR_TOTAL (np->fp_trailing_zeros);
+  INCR_TOTAL (np->exp_size);
 
   if (spec.width > total)
     /* pad with spaces or zeros depending on np->pad_type */
     {
       np->pad_size = spec.width - total;
       total += np->pad_size; /* here total == spec.width,
-                                so 0 < total < INT_MAX */
+                                so 0 < total <= INT_MAX */
+      MPFR_ASSERTD (total == spec.width);
     }
 
+  MPFR_ASSERTD (total > 0 && total <= INT_MAX);
   return total;
 
  error:
diff -Naurd mpfr-3.1.5-a/src/version.c mpfr-3.1.5-b/src/version.c
--- mpfr-3.1.5-a/src/version.c	2017-06-06 19:50:30.736438175 +0000
+++ mpfr-3.1.5-b/src/version.c	2017-06-06 19:57:01.971909970 +0000
@@ -25,5 +25,5 @@
 const char *
 mpfr_get_version (void)
 {
-  return "3.1.5-p4";
+  return "3.1.5-p5";
 }
diff -Naurd mpfr-3.1.5-a/PATCHES mpfr-3.1.5-b/PATCHES
--- mpfr-3.1.5-a/PATCHES	2017-06-06 20:17:02.489704106 +0000
+++ mpfr-3.1.5-b/PATCHES	2017-06-06 20:17:02.513703814 +0000
@@ -0,0 +1 @@
+printf-errno
diff -Naurd mpfr-3.1.5-a/VERSION mpfr-3.1.5-b/VERSION
--- mpfr-3.1.5-a/VERSION	2017-06-06 19:57:01.971909970 +0000
+++ mpfr-3.1.5-b/VERSION	2017-06-06 20:17:02.513703814 +0000
@@ -1 +1 @@
-3.1.5-p5
+3.1.5-p6
diff -Naurd mpfr-3.1.5-a/src/mpfr.h mpfr-3.1.5-b/src/mpfr.h
--- mpfr-3.1.5-a/src/mpfr.h	2017-06-06 19:57:01.971909970 +0000
+++ mpfr-3.1.5-b/src/mpfr.h	2017-06-06 20:17:02.513703814 +0000
@@ -27,7 +27,7 @@
 #define MPFR_VERSION_MAJOR 3
 #define MPFR_VERSION_MINOR 1
 #define MPFR_VERSION_PATCHLEVEL 5
-#define MPFR_VERSION_STRING "3.1.5-p5"
+#define MPFR_VERSION_STRING "3.1.5-p6"
 
 /* Macros dealing with MPFR VERSION */
 #define MPFR_VERSION_NUM(a,b,c) (((a) << 16L) | ((b) << 8) | (c))
diff -Naurd mpfr-3.1.5-a/src/printf.c mpfr-3.1.5-b/src/printf.c
--- mpfr-3.1.5-a/src/printf.c	2016-09-27 07:58:15.000000000 +0000
+++ mpfr-3.1.5-b/src/printf.c	2017-06-06 20:17:02.501703960 +0000
@@ -40,7 +40,6 @@
 # endif /* HAVE___VA_COPY */
 #endif /* HAVE_VA_COPY */
 
-#include <errno.h>
 #include "mpfr-impl.h"
 
 #ifdef _MPFR_H_HAVE_FILE
diff -Naurd mpfr-3.1.5-a/src/vasprintf.c mpfr-3.1.5-b/src/vasprintf.c
--- mpfr-3.1.5-a/src/vasprintf.c	2017-06-06 19:57:01.963910062 +0000
+++ mpfr-3.1.5-b/src/vasprintf.c	2017-06-06 20:17:02.501703960 +0000
@@ -52,6 +52,8 @@
 #include <stddef.h>             /* for ptrdiff_t */
 #endif
 
+#include <errno.h>
+
 #define MPFR_NEED_LONGLONG_H
 #include "mpfr-intmax.h"
 #include "mpfr-impl.h"
diff -Naurd mpfr-3.1.5-a/src/version.c mpfr-3.1.5-b/src/version.c
--- mpfr-3.1.5-a/src/version.c	2017-06-06 19:57:01.971909970 +0000
+++ mpfr-3.1.5-b/src/version.c	2017-06-06 20:17:02.513703814 +0000
@@ -25,5 +25,5 @@
 const char *
 mpfr_get_version (void)
 {
-  return "3.1.5-p5";
+  return "3.1.5-p6";
 }
diff -Naurd mpfr-3.1.5-a/tests/tprintf.c mpfr-3.1.5-b/tests/tprintf.c
--- mpfr-3.1.5-a/tests/tprintf.c	2016-09-27 07:58:14.000000000 +0000
+++ mpfr-3.1.5-b/tests/tprintf.c	2017-06-06 20:17:02.501703960 +0000
@@ -26,6 +26,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <stddef.h>
+#include <errno.h>
 
 #include "mpfr-intmax.h"
 #include "mpfr-test.h"
@@ -109,6 +110,33 @@
 }
 
 static void
+check_vprintf_overflow (const char *fmt, ...)
+{
+  va_list ap;
+  int r, e;
+
+  va_start (ap, fmt);
+  errno = 0;
+  r = mpfr_vprintf (fmt, ap);
+  e = errno;
+  va_end (ap);
+
+  if (r != -1
+#ifdef EOVERFLOW
+      || e != EOVERFLOW
+#endif
+      )
+    {
+      putchar ('\n');
+      fprintf (stderr, "Error in mpfr_vprintf(\"%s\", ...)\n"
+               "Got r = %d, errno = %d\n", fmt, r, e);
+      exit (1);
+    }
+
+  putchar ('\n');
+}
+
+static void
 check_invalid_format (void)
 {
   int i = 0;
@@ -167,8 +195,8 @@
   mpfr_set_ui (x, 1, MPFR_RNDN);
   mpfr_nextabove (x);
 
-  check_vprintf_failure ("%Rb", x);
-  check_vprintf_failure ("%RA %RA %Ra %Ra", x, x, x, x);
+  check_vprintf_overflow ("%Rb", x);
+  check_vprintf_overflow ("%RA %RA %Ra %Ra", x, x, x, x);
 
   mpfr_clear (x);
 }
diff -Naurd mpfr-3.1.5-a/PATCHES mpfr-3.1.5-b/PATCHES
--- mpfr-3.1.5-a/PATCHES	2017-06-06 20:24:00.580702002 +0000
+++ mpfr-3.1.5-b/PATCHES	2017-06-06 20:24:00.604701719 +0000
@@ -0,0 +1 @@
+tsprintf-setlocale
diff -Naurd mpfr-3.1.5-a/VERSION mpfr-3.1.5-b/VERSION
--- mpfr-3.1.5-a/VERSION	2017-06-06 20:17:02.513703814 +0000
+++ mpfr-3.1.5-b/VERSION	2017-06-06 20:24:00.604701719 +0000
@@ -1 +1 @@
-3.1.5-p6
+3.1.5-p7
diff -Naurd mpfr-3.1.5-a/src/mpfr.h mpfr-3.1.5-b/src/mpfr.h
--- mpfr-3.1.5-a/src/mpfr.h	2017-06-06 20:17:02.513703814 +0000
+++ mpfr-3.1.5-b/src/mpfr.h	2017-06-06 20:24:00.604701719 +0000
@@ -27,7 +27,7 @@
 #define MPFR_VERSION_MAJOR 3
 #define MPFR_VERSION_MINOR 1
 #define MPFR_VERSION_PATCHLEVEL 5
-#define MPFR_VERSION_STRING "3.1.5-p6"
+#define MPFR_VERSION_STRING "3.1.5-p7"
 
 /* Macros dealing with MPFR VERSION */
 #define MPFR_VERSION_NUM(a,b,c) (((a) << 16L) | ((b) << 8) | (c))
diff -Naurd mpfr-3.1.5-a/src/version.c mpfr-3.1.5-b/src/version.c
--- mpfr-3.1.5-a/src/version.c	2017-06-06 20:17:02.513703814 +0000
+++ mpfr-3.1.5-b/src/version.c	2017-06-06 20:24:00.604701719 +0000
@@ -25,5 +25,5 @@
 const char *
 mpfr_get_version (void)
 {
-  return "3.1.5-p6";
+  return "3.1.5-p7";
 }
diff -Naurd mpfr-3.1.5-a/tests/tsprintf.c mpfr-3.1.5-b/tests/tsprintf.c
--- mpfr-3.1.5-a/tests/tsprintf.c	2016-12-15 08:35:46.520430308 +0000
+++ mpfr-3.1.5-b/tests/tsprintf.c	2017-06-06 20:24:00.596701813 +0000
@@ -1273,13 +1273,12 @@
 int
 main (int argc, char **argv)
 {
-  char *locale;
 
   tests_start_mpfr ();
 
 #if defined(HAVE_LOCALE_H) && defined(HAVE_SETLOCALE)
   /* currently, we just check with 'C' locale */
-  locale = setlocale (LC_ALL, "C");
+  setlocale (LC_ALL, "C");
 #endif
 
   bug20111102 ();
@@ -1297,7 +1296,7 @@
   locale_da_DK ();
   /* Avoid a warning by doing the setlocale outside of this #if */
 #endif
-  setlocale (LC_ALL, locale);
+  setlocale (LC_ALL, "C");
 #endif
 
   if (getenv ("MPFR_CHECK_LIBC_PRINTF"))
diff -Naurd mpfr-3.1.5-a/PATCHES mpfr-3.1.5-b/PATCHES
--- mpfr-3.1.5-a/PATCHES	2017-06-06 20:31:35.919341495 +0000
+++ mpfr-3.1.5-b/PATCHES	2017-06-06 20:31:35.943341213 +0000
@@ -0,0 +1 @@
+mpf-compat-signed
diff -Naurd mpfr-3.1.5-a/VERSION mpfr-3.1.5-b/VERSION
--- mpfr-3.1.5-a/VERSION	2017-06-06 20:24:00.604701719 +0000
+++ mpfr-3.1.5-b/VERSION	2017-06-06 20:31:35.943341213 +0000
@@ -1 +1 @@
-3.1.5-p7
+3.1.5-p8
diff -Naurd mpfr-3.1.5-a/src/mpf2mpfr.h mpfr-3.1.5-b/src/mpf2mpfr.h
--- mpfr-3.1.5-a/src/mpf2mpfr.h	2016-09-27 07:58:15.000000000 +0000
+++ mpfr-3.1.5-b/src/mpf2mpfr.h	2017-06-06 20:31:35.931341353 +0000
@@ -93,15 +93,15 @@
 #undef mpf_div_2exp
 #define mpf_div_2exp(x,y,z) mpfr_div_2exp(x,y,z,MPFR_DEFAULT_RND)
 #undef mpf_fits_slong_p
-#define mpf_fits_slong_p(x) mpfr_fits_ulong_p(x,MPFR_DEFAULT_RND)
+#define mpf_fits_slong_p(x) mpfr_fits_slong_p(x,MPFR_DEFAULT_RND)
 #undef mpf_fits_ulong_p
 #define mpf_fits_ulong_p(x) mpfr_fits_ulong_p(x,MPFR_DEFAULT_RND)
 #undef mpf_fits_sint_p
-#define mpf_fits_sint_p(x) mpfr_fits_uint_p(x,MPFR_DEFAULT_RND)
+#define mpf_fits_sint_p(x) mpfr_fits_sint_p(x,MPFR_DEFAULT_RND)
 #undef mpf_fits_uint_p
 #define mpf_fits_uint_p(x) mpfr_fits_uint_p(x,MPFR_DEFAULT_RND)
 #undef mpf_fits_sshort_p
-#define mpf_fits_sshort_p(x) mpfr_fits_ushort_p(x,MPFR_DEFAULT_RND)
+#define mpf_fits_sshort_p(x) mpfr_fits_sshort_p(x,MPFR_DEFAULT_RND)
 #undef mpf_fits_ushort_p
 #define mpf_fits_ushort_p(x) mpfr_fits_ushort_p(x,MPFR_DEFAULT_RND)
 #undef mpf_get_str
@@ -113,7 +113,7 @@
 #undef mpf_get_ui
 #define mpf_get_ui(x) mpfr_get_ui(x,MPFR_DEFAULT_RND)
 #undef mpf_get_si
-#define mpf_get_si(x) mpfr_get_ui(x,MPFR_DEFAULT_RND)
+#define mpf_get_si(x) mpfr_get_si(x,MPFR_DEFAULT_RND)
 #undef mpf_inp_str
 #define mpf_inp_str(x,y,z) mpfr_inp_str(x,y,z,MPFR_DEFAULT_RND)
 #undef mpf_set_str
diff -Naurd mpfr-3.1.5-a/src/mpfr-impl.h mpfr-3.1.5-b/src/mpfr-impl.h
--- mpfr-3.1.5-a/src/mpfr-impl.h	2017-06-06 19:21:17.592843433 +0000
+++ mpfr-3.1.5-b/src/mpfr-impl.h	2017-06-06 20:31:35.931341353 +0000
@@ -342,11 +342,15 @@
 #define MPFR_FLAGS_DIVBY0 32
 #define MPFR_FLAGS_ALL 63
 
-/* Replace some common functions for direct access to the global vars */
-#define mpfr_get_emin() (__gmpfr_emin + 0)
-#define mpfr_get_emax() (__gmpfr_emax + 0)
-#define mpfr_get_default_rounding_mode() (__gmpfr_default_rounding_mode + 0)
-#define mpfr_get_default_prec() (__gmpfr_default_fp_bit_precision + 0)
+/* Replace some common functions for direct access to the global vars.
+   The casts prevent these macros from being used as a lvalue (and this
+   method makes sure that the expressions have the correct type). */
+#define mpfr_get_emin() ((mpfr_exp_t) __gmpfr_emin)
+#define mpfr_get_emax() ((mpfr_exp_t) __gmpfr_emax)
+#define mpfr_get_default_rounding_mode() \
+  ((mpfr_rnd_t) __gmpfr_default_rounding_mode)
+#define mpfr_get_default_prec() \
+  ((mpfr_prec_t) __gmpfr_default_fp_bit_precision)
 
 #define mpfr_clear_flags() \
   ((void) (__gmpfr_flags = 0))
diff -Naurd mpfr-3.1.5-a/src/mpfr.h mpfr-3.1.5-b/src/mpfr.h
--- mpfr-3.1.5-a/src/mpfr.h	2017-06-06 20:24:00.604701719 +0000
+++ mpfr-3.1.5-b/src/mpfr.h	2017-06-06 20:31:35.939341259 +0000
@@ -27,7 +27,7 @@
 #define MPFR_VERSION_MAJOR 3
 #define MPFR_VERSION_MINOR 1
 #define MPFR_VERSION_PATCHLEVEL 5
-#define MPFR_VERSION_STRING "3.1.5-p7"
+#define MPFR_VERSION_STRING "3.1.5-p8"
 
 /* Macros dealing with MPFR VERSION */
 #define MPFR_VERSION_NUM(a,b,c) (((a) << 16L) | ((b) << 8) | (c))
diff -Naurd mpfr-3.1.5-a/src/version.c mpfr-3.1.5-b/src/version.c
--- mpfr-3.1.5-a/src/version.c	2017-06-06 20:24:00.604701719 +0000
+++ mpfr-3.1.5-b/src/version.c	2017-06-06 20:31:35.943341213 +0000
@@ -25,5 +25,5 @@
 const char *
 mpfr_get_version (void)
 {
-  return "3.1.5-p7";
+  return "3.1.5-p8";
 }
diff -Naurd mpfr-3.1.5-a/tests/mpf_compat.h mpfr-3.1.5-b/tests/mpf_compat.h
--- mpfr-3.1.5-a/tests/mpf_compat.h	2016-09-27 07:58:14.000000000 +0000
+++ mpfr-3.1.5-b/tests/mpf_compat.h	2017-06-06 20:31:35.931341353 +0000
@@ -20,16 +20,10 @@
 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
 
-#if defined (__cplusplus)
-#include <cstdio>
-#else
-#include <stdio.h>
-#endif
 #include <stdlib.h>
-#include <string.h>
 
-#include "gmp.h"
-#include "mpfr.h"
+#include "mpfr-impl.h"
+
 #ifdef MPFR
 #include "mpf2mpfr.h"
 #endif
@@ -228,6 +222,16 @@
       exit (1);
     }
 
+  /* non-regression tests for bugs fixed in revision 11565 */
+  mpf_set_si (x, -1);
+  MPFR_ASSERTN(mpf_fits_ulong_p (x) == 0);
+  MPFR_ASSERTN(mpf_fits_slong_p (x) != 0);
+  MPFR_ASSERTN(mpf_fits_uint_p (x) == 0);
+  MPFR_ASSERTN(mpf_fits_sint_p (x) != 0);
+  MPFR_ASSERTN(mpf_fits_ushort_p (x) == 0);
+  MPFR_ASSERTN(mpf_fits_sshort_p (x) != 0);
+  MPFR_ASSERTN(mpf_get_si (x) == -1);
+
   /* clear all variables */
   mpf_clear (y);
   mpf_clear (x);
