aboutsummaryrefslogtreecommitdiff
path: root/gcc/selftest.c
diff options
context:
space:
mode:
authorDavid Malcolm <dmalcolm@redhat.com>2016-11-30 14:50:43 +0000
committerDavid Malcolm <dmalcolm@gcc.gnu.org>2016-11-30 14:50:43 +0000
commite613205cefe50e429bafba2bc4cb71126b1f36df (patch)
treef30b1f41724d3288f8bdba58298d4d1aecd8960d /gcc/selftest.c
parente6383ae7a73164277026cf815315e39f281e6aca (diff)
downloadgcc-e613205cefe50e429bafba2bc4cb71126b1f36df.zip
gcc-e613205cefe50e429bafba2bc4cb71126b1f36df.tar.gz
gcc-e613205cefe50e429bafba2bc4cb71126b1f36df.tar.bz2
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 'gcc/selftest.c')
-rw-r--r--gcc/selftest.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/gcc/selftest.c b/gcc/selftest.c
index 2a729be..6df73c2 100644
--- a/gcc/selftest.c
+++ b/gcc/selftest.c
@@ -198,6 +198,53 @@ read_file (const location &loc, const char *path)
return result;
}
+/* Selftests for libiberty. */
+
+/* Verify that both strndup and xstrndup generate EXPECTED
+ when called on SRC and N. */
+
+static void
+assert_strndup_eq (const char *expected, const char *src, size_t n)
+{
+ char *buf = strndup (src, n);
+ if (buf)
+ ASSERT_STREQ (expected, buf);
+ free (buf);
+
+ buf = xstrndup (src, n);
+ ASSERT_STREQ (expected, buf);
+ free (buf);
+}
+
+/* Verify that strndup and xstrndup work as expected. */
+
+static void
+test_strndup ()
+{
+ assert_strndup_eq ("", "test", 0);
+ assert_strndup_eq ("t", "test", 1);
+ assert_strndup_eq ("te", "test", 2);
+ assert_strndup_eq ("tes", "test", 3);
+ assert_strndup_eq ("test", "test", 4);
+ assert_strndup_eq ("test", "test", 5);
+
+ /* Test on an string without zero termination. */
+ const char src[4] = {'t', 'e', 's', 't'};
+ assert_strndup_eq ("", src, 0);
+ assert_strndup_eq ("t", src, 1);
+ assert_strndup_eq ("te", src, 2);
+ assert_strndup_eq ("tes", src, 3);
+ assert_strndup_eq ("test", src, 4);
+}
+
+/* Run selftests for libiberty. */
+
+static void
+test_libiberty ()
+{
+ test_strndup ();
+}
+
/* Selftests for the selftest system itself. */
/* Sanity-check the ASSERT_ macros with various passing cases. */
@@ -245,6 +292,7 @@ test_read_file ()
void
selftest_c_tests ()
{
+ test_libiberty ();
test_assertions ();
test_named_temp_file ();
test_read_file ();