From df075e0920223f12ffb8d0a0e54c9d5db181ee76 Mon Sep 17 00:00:00 2001 From: Roland McGrath Date: Mon, 23 Sep 2002 03:57:55 +0000 Subject: [PATCH] * string/bug-stpncpy-offend.c: New file. * string/Makefile (tests): Add it. --- ChangeLog | 5 +++++ string/Makefile | 4 ++-- string/bug-stpncpy-offend.c | 53 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 string/bug-stpncpy-offend.c diff --git a/ChangeLog b/ChangeLog index e47cf4d..f2eee03 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2002-09-22 Roland McGrath + + * string/bug-stpncpy-offend.c: New file. + * string/Makefile (tests): Add it. + 2002-09-21 Carlos O'Donell * sysdeps/hppa/abort-instr.h: New file. diff --git a/string/Makefile b/string/Makefile index 5d5b09e..b0f5571 100644 --- a/string/Makefile +++ b/string/Makefile @@ -1,4 +1,4 @@ -# Copyright (C) 1991-1999, 2000, 2001 Free Software Foundation, Inc. +# Copyright (C) 1991-1999,2000,2001,2002 Free Software Foundation, Inc. # This file is part of the GNU C Library. # The GNU C Library is free software; you can redistribute it and/or @@ -48,7 +48,7 @@ o-objects.ob := memcpy.o memset.o memchr.o tests := tester inl-tester noinl-tester testcopy test-ffs \ tst-strlen stratcliff tst-svc tst-inlcall \ bug-strncat1 bug-strspn1 bug-strpbrk1 tst-bswap \ - tst-strtok tst-strxfrm bug-strcoll1 + tst-strtok tst-strxfrm bug-strcoll1 bug-stpncpy-offend distribute := memcopy.h pagecopy.h tst-svc.expect diff --git a/string/bug-stpncpy-offend.c b/string/bug-stpncpy-offend.c new file mode 100644 index 0000000..3a782f7 --- /dev/null +++ b/string/bug-stpncpy-offend.c @@ -0,0 +1,53 @@ +/* Test program for stpncpy reading off the end of the source string. */ + +#include +#include +#include + +static int do_test (void); +#define TEST_FUNCTION do_test () +#include + +static int +do_test (void) +{ + /* We get two pages of memory and then protect the second one so + we are sure to fault if we access past the end of the first page. + Then we test the odd-size string ending just on the page boundary. */ + + static const char test_string[] = "Seven."; + const size_t pagesz = getpagesize (); + char *buf = mmap (NULL, pagesz * 2, + PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0); + char *s1, *r; + volatile size_t len = sizeof test_string; + + if (buf == MAP_FAILED) + { + perror ("mmap"); + return 1; + } + if (mprotect (buf + pagesz, pagesz, PROT_NONE) != 0) + { + perror ("mprotect"); + return 2; + } + + s1 = buf + pagesz - sizeof test_string; + memcpy (s1, test_string, sizeof test_string); + + r = stpncpy (buf, s1, len); + if (r != buf + len - 1) + { + printf ("r = buf + %d != %zu\n", r - buf, len - 1); + return 3; + } + r = stpncpy (s1, buf, len); + if (r != s1 + len - 1) + { + printf ("r = s1 + %d != %zu\n", r - s1, len - 1); + return 3; + } + + return 0; +} -- 2.7.4