diff options
author | Kaveh R. Ghazi <ghazi@caip.rutgers.edu> | 2003-04-22 19:58:57 +0000 |
---|---|---|
committer | Kaveh Ghazi <ghazi@gcc.gnu.org> | 2003-04-22 19:58:57 +0000 |
commit | 52dd32242018d531d0a974f6b4b61df28d491ab3 (patch) | |
tree | 24d4e0bc836479f84500f21dc6743355e5a85e4e /libiberty/vsnprintf.c | |
parent | b78f9414c6db8d7c9c0ed3466a2a75bc1a7d215c (diff) | |
download | gcc-52dd32242018d531d0a974f6b4b61df28d491ab3.zip gcc-52dd32242018d531d0a974f6b4b61df28d491ab3.tar.gz gcc-52dd32242018d531d0a974f6b4b61df28d491ab3.tar.bz2 |
vsnprintf.c (vsnprintf): Don't pad string with extra nulls.
* vsnprintf.c (vsnprintf): Don't pad string with extra nulls.
(main): Test that we don't write too much data.
From-SVN: r65955
Diffstat (limited to 'libiberty/vsnprintf.c')
-rw-r--r-- | libiberty/vsnprintf.c | 26 |
1 files changed, 15 insertions, 11 deletions
diff --git a/libiberty/vsnprintf.c b/libiberty/vsnprintf.c index 9328e43..fd3dd18 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; } |