d3767b
From 6d059cebfdefbdf56910a858f8b603d37f10ef6d Mon Sep 17 00:00:00 2001
d3767b
From: Bruno Haible <bruno@clisp.org>
d3767b
Date: Sun, 23 Sep 2018 14:13:52 +0200
d3767b
Subject: [PATCH] vasnprintf: Fix heap memory overrun bug.
d3767b
d3767b
Reported by Ben Pfaff <blp@cs.stanford.edu> in
d3767b
<https://lists.gnu.org/archive/html/bug-gnulib/2018-09/msg00107.html>.
d3767b
d3767b
* lib/vasnprintf.c (convert_to_decimal): Allocate one more byte of
d3767b
memory.
d3767b
* tests/test-vasnprintf.c (test_function): Add another test.
d3767b
d3767b
Upstream-commit: 278b4175c9d7dd47c1a3071554aac02add3b3c35
d3767b
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
d3767b
---
d3767b
 gnulib-tests/test-vasnprintf.c | 21 ++++++++++++++++++++-
d3767b
 lib/vasnprintf.c               |  4 +++-
d3767b
 2 files changed, 23 insertions(+), 2 deletions(-)
d3767b
d3767b
diff --git a/gnulib-tests/test-vasnprintf.c b/gnulib-tests/test-vasnprintf.c
d3767b
index 19731bc..93d81d7 100644
d3767b
--- a/gnulib-tests/test-vasnprintf.c
d3767b
+++ b/gnulib-tests/test-vasnprintf.c
d3767b
@@ -53,7 +53,26 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
d3767b
       ASSERT (result != NULL);
d3767b
       ASSERT (strcmp (result, "12345") == 0);
d3767b
       ASSERT (length == 5);
d3767b
-      if (size < 6)
d3767b
+      if (size < 5 + 1)
d3767b
+        ASSERT (result != buf);
d3767b
+      ASSERT (memcmp (buf + size, &"DEADBEEF"[size], 8 - size) == 0);
d3767b
+      if (result != buf)
d3767b
+        free (result);
d3767b
+    }
d3767b
+
d3767b
+  /* Note: This test assumes IEEE 754 representation of 'double' floats.  */
d3767b
+  for (size = 0; size <= 8; size++)
d3767b
+    {
d3767b
+      size_t length;
d3767b
+      char *result;
d3767b
+
d3767b
+      memcpy (buf, "DEADBEEF", 8);
d3767b
+      length = size;
d3767b
+      result = my_asnprintf (buf, &length, "%2.0f", 1.6314159265358979e+125);
d3767b
+      ASSERT (result != NULL);
d3767b
+      ASSERT (strcmp (result, "163141592653589790215729350939528493057529598899734151772468186268423257777068536614838678161083520756952076273094236944990208") == 0);
d3767b
+      ASSERT (length == 126);
d3767b
+      if (size < 126 + 1)
d3767b
         ASSERT (result != buf);
d3767b
       ASSERT (memcmp (buf + size, &"DEADBEEF"[size], 8 - size) == 0);
d3767b
       if (result != buf)
d3767b
diff --git a/lib/vasnprintf.c b/lib/vasnprintf.c
d3767b
index 3b441d0..48ef7a6 100644
d3767b
--- a/lib/vasnprintf.c
d3767b
+++ b/lib/vasnprintf.c
d3767b
@@ -860,7 +860,9 @@ convert_to_decimal (mpn_t a, size_t extra_zeroes)
d3767b
   size_t a_len = a.nlimbs;
d3767b
   /* 0.03345 is slightly larger than log(2)/(9*log(10)).  */
d3767b
   size_t c_len = 9 * ((size_t)(a_len * (GMP_LIMB_BITS * 0.03345f)) + 1);
d3767b
-  char *c_ptr = (char *) malloc (xsum (c_len, extra_zeroes));
d3767b
+  /* We need extra_zeroes bytes for zeroes, followed by c_len bytes for the
d3767b
+     digits of a, followed by 1 byte for the terminating NUL.  */
d3767b
+  char *c_ptr = (char *) malloc (xsum (xsum (extra_zeroes, c_len), 1));
d3767b
   if (c_ptr != NULL)
d3767b
     {
d3767b
       char *d_ptr = c_ptr;
d3767b
-- 
d3767b
2.17.1
d3767b