From 341eccbc3efd82f1273d027d190ea87fde78a943 Mon Sep 17 00:00:00 2001 From: DJ Delorie Date: Tue, 22 Apr 2003 20:04:52 +0000 Subject: [PATCH] merge from gcc --- libiberty/ChangeLog | 5 +++++ libiberty/vsnprintf.c | 26 +++++++++++++++----------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/libiberty/ChangeLog b/libiberty/ChangeLog index 027fe91c0f1..ae5a7c5499b 100644 --- a/libiberty/ChangeLog +++ b/libiberty/ChangeLog @@ -1,3 +1,8 @@ +2003-04-22 Kaveh R. Ghazi + + * vsnprintf.c (vsnprintf): Don't pad string with extra nulls. + (main): Test that we don't write too much data. + 2003-04-16 Kaveh R. Ghazi * configure.in (funcs, AC_CHECK_FUNCS): Add snprintf and diff --git a/libiberty/vsnprintf.c b/libiberty/vsnprintf.c index 9328e430097..fd3dd18a91c 100644 --- a/libiberty/vsnprintf.c +++ b/libiberty/vsnprintf.c @@ -78,9 +78,13 @@ vsnprintf (s, n, format, ap) result = strlen (buf); if (n > 0) { - strncpy (s, buf, n); - if (n - 1 < (size_t) result) - s[n - 1] = 0; + if ((long) n > result) + memcpy (s, buf, result+1); + else + { + memcpy (s, buf, n-1); + s[n - 1] = 0; + } } free (buf); return result; @@ -114,35 +118,35 @@ main () CLEAR (buf); status = checkit (buf, 10, "%s:%d", "foobar", 9); - VERIFY (status==8 && strcmp (buf, "foobar:9") == 0); + VERIFY (status==8 && memcmp (buf, "foobar:9\0XXXXX\0", 15) == 0); CLEAR (buf); status = checkit (buf, 9, "%s:%d", "foobar", 9); - VERIFY (status==8 && strcmp (buf, "foobar:9") == 0); + VERIFY (status==8 && memcmp (buf, "foobar:9\0XXXXX\0", 15) == 0); CLEAR (buf); status = checkit (buf, 8, "%s:%d", "foobar", 9); - VERIFY (status==8 && strcmp (buf, "foobar:") == 0); + VERIFY (status==8 && memcmp (buf, "foobar:\0XXXXXX\0", 15) == 0); CLEAR (buf); status = checkit (buf, 7, "%s:%d", "foobar", 9); - VERIFY (status==8 && strcmp (buf, "foobar") == 0); + VERIFY (status==8 && memcmp (buf, "foobar\0XXXXXXX\0", 15) == 0); CLEAR (buf); status = checkit (buf, 6, "%s:%d", "foobar", 9); - VERIFY (status==8 && strcmp (buf, "fooba") == 0); + VERIFY (status==8 && memcmp (buf, "fooba\0XXXXXXXX\0", 15) == 0); CLEAR (buf); status = checkit (buf, 2, "%s:%d", "foobar", 9); - VERIFY (status==8 && strcmp (buf, "f") == 0); + VERIFY (status==8 && memcmp (buf, "f\0XXXXXXXXXXXX\0", 15) == 0); CLEAR (buf); status = checkit (buf, 1, "%s:%d", "foobar", 9); - VERIFY (status==8 && strcmp (buf, "") == 0); + VERIFY (status==8 && memcmp (buf, "\0XXXXXXXXXXXXX\0", 15) == 0); CLEAR (buf); status = checkit (buf, 0, "%s:%d", "foobar", 9); - VERIFY (status==8 && strcmp (buf, "XXXXXXXXXXXXXX") == 0); + VERIFY (status==8 && memcmp (buf, "XXXXXXXXXXXXXX\0", 15) == 0); return 0; } -- 2.30.2