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