diff options
author | David Malcolm <dmalcolm@redhat.com> | 2016-11-30 14:50:43 +0000 |
---|---|---|
committer | David Malcolm <dmalcolm@gcc.gnu.org> | 2016-11-30 14:50:43 +0000 |
commit | e613205cefe50e429bafba2bc4cb71126b1f36df (patch) | |
tree | f30b1f41724d3288f8bdba58298d4d1aecd8960d /libiberty | |
parent | e6383ae7a73164277026cf815315e39f281e6aca (diff) | |
download | gcc-e613205cefe50e429bafba2bc4cb71126b1f36df.tar.gz |
libiberty: avoid reading past end of buffer in strndup/xstrndup (PR c/78498)
gcc/ChangeLog:
PR c/78498
* selftest.c (selftest::assert_strndup_eq): New function.
(selftest::test_strndup): New function.
(selftest::test_libiberty): New function.
(selftest::selftest_c_tests): Call test_libiberty.
gcc/testsuite/ChangeLog:
PR c/78498
* gcc.dg/format/pr78494.c: New test case.
libiberty/ChangeLog:
PR c/78498
* strndup.c (strlen): Delete decl.
(strnlen): Add decl.
(strndup): Call strnlen rather than strlen.
* xstrndup.c (xstrndup): Likewise.
From-SVN: r243030
Diffstat (limited to 'libiberty')
-rw-r--r-- | libiberty/ChangeLog | 8 | ||||
-rw-r--r-- | libiberty/strndup.c | 7 | ||||
-rw-r--r-- | libiberty/xstrndup.c | 5 |
3 files changed, 11 insertions, 9 deletions
diff --git a/libiberty/ChangeLog b/libiberty/ChangeLog index 42164c406af..f0959c99f5c 100644 --- a/libiberty/ChangeLog +++ b/libiberty/ChangeLog @@ -1,3 +1,11 @@ +2016-11-30 David Malcolm <dmalcolm@redhat.com> + + PR c/78498 + * strndup.c (strlen): Delete decl. + (strnlen): Add decl. + (strndup): Call strnlen rather than strlen. + * xstrndup.c (xstrndup): Likewise. + 2016-11-29 Nathan Sidwell <nathan@acm.org> * cp-demangle.c (d_print_comp_inner): Fix parameter indentation. diff --git a/libiberty/strndup.c b/libiberty/strndup.c index 9e9b4e2991f..4556b962746 100644 --- a/libiberty/strndup.c +++ b/libiberty/strndup.c @@ -33,7 +33,7 @@ memory was available. The result is always NUL terminated. #include "ansidecl.h" #include <stddef.h> -extern size_t strlen (const char*); +extern size_t strnlen (const char *s, size_t maxlen); extern PTR malloc (size_t); extern PTR memcpy (PTR, const PTR, size_t); @@ -41,10 +41,7 @@ char * strndup (const char *s, size_t n) { char *result; - size_t len = strlen (s); - - if (n < len) - len = n; + size_t len = strnlen (s, n); result = (char *) malloc (len + 1); if (!result) diff --git a/libiberty/xstrndup.c b/libiberty/xstrndup.c index 0a41f608ec0..c3d2d833902 100644 --- a/libiberty/xstrndup.c +++ b/libiberty/xstrndup.c @@ -48,10 +48,7 @@ char * xstrndup (const char *s, size_t n) { char *result; - size_t len = strlen (s); - - if (n < len) - len = n; + size_t len = strnlen (s, n); result = XNEWVEC (char, len + 1); |