From patchwork Tue Sep 16 23:40:30 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Will Newton X-Patchwork-Id: 37482 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-ee0-f70.google.com (mail-ee0-f70.google.com [74.125.83.70]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id C8CF820549 for ; Tue, 16 Sep 2014 23:40:53 +0000 (UTC) Received: by mail-ee0-f70.google.com with SMTP id c13sf39751eek.5 for ; Tue, 16 Sep 2014 16:40:53 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:mailing-list :precedence:list-id:list-unsubscribe:list-subscribe:list-archive :list-post:list-help:sender:delivered-to:from:to:subject:date :message-id:x-original-sender:x-original-authentication-results; bh=2OBljx3lDY5m3N8TpeMtgmXzL44vT9FeNEISd1JdHQw=; b=T4BzZAiSLRfFYgG41OVBFoRbXiT0hjXB4LVOTkFj38RSX2NI1bxiD+dt/krX2pnv0U HMn1L4vYb+ZrhyuR5XvXdaJN029t9DPFippdXISuXpbMfKp+WjLLC7xRQGg+OIgRWlMh knfRhbEcCFh3mE5ciRwvitbphMVWNI/guk0qesD0oiYTgxBdrF2Nlqizbj8WOJVQATeT W6o8mVRbmptp5rdxm+sldBWtupbV6WSsXjm/vFBBYZl8LOhVF4zP7gj0zf8sp3jxyBES nGk5AcEK36Jp90A9X2j5za+o1IVytyMPvQlDNwvAFcj46GzNkuyiFCuJu6A9UC2xU9X9 KUgw== X-Gm-Message-State: ALoCoQmcetaulrv/msHTvhX5yeKvecxmdbyO77wyjfMLvOtsauthQaHBhyLK8G46u24TeWhUEmaY X-Received: by 10.194.100.3 with SMTP id eu3mr5816wjb.6.1410910852977; Tue, 16 Sep 2014 16:40:52 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.43.50 with SMTP id t18ls47010lal.104.gmail; Tue, 16 Sep 2014 16:40:52 -0700 (PDT) X-Received: by 10.152.21.98 with SMTP id u2mr17533611lae.60.1410910852826; Tue, 16 Sep 2014 16:40:52 -0700 (PDT) Received: from mail-lb0-x230.google.com (mail-lb0-x230.google.com [2a00:1450:4010:c04::230]) by mx.google.com with ESMTPS id 1si26274437lal.89.2014.09.16.16.40.52 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 16 Sep 2014 16:40:52 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 2a00:1450:4010:c04::230 as permitted sender) client-ip=2a00:1450:4010:c04::230; Received: by mail-lb0-f176.google.com with SMTP id z11so781284lbi.21 for ; Tue, 16 Sep 2014 16:40:52 -0700 (PDT) X-Received: by 10.112.130.168 with SMTP id of8mr38722900lbb.5.1410910852750; Tue, 16 Sep 2014 16:40:52 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.130.169 with SMTP id of9csp488155lbb; Tue, 16 Sep 2014 16:40:51 -0700 (PDT) X-Received: by 10.68.95.196 with SMTP id dm4mr54550824pbb.95.1410910851163; Tue, 16 Sep 2014 16:40:51 -0700 (PDT) Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id r1si32135752pdl.121.2014.09.16.16.40.50 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 16 Sep 2014 16:40:51 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-52915-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Received: (qmail 7991 invoked by alias); 16 Sep 2014 23:40:41 -0000 Mailing-List: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org Precedence: list List-Id: List-Unsubscribe: , List-Subscribe: List-Archive: List-Post: , List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 7982 invoked by uid 89); 16 Sep 2014 23:40:41 -0000 X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-pa0-f48.google.com X-Received: by 10.68.220.105 with SMTP id pv9mr55374267pbc.8.1410910837388; Tue, 16 Sep 2014 16:40:37 -0700 (PDT) From: Will Newton To: libc-alpha@sourceware.org Subject: [PATCH] string: Add tests for zero length string inputs Date: Tue, 16 Sep 2014 16:40:30 -0700 Message-Id: <1410910830-20900-1-git-send-email-will.newton@linaro.org> X-Original-Sender: will.newton@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 2a00:1450:4010:c04::230 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org; dkim=pass header.i=@sourceware.org X-Google-Group-Id: 836684582541 For the string functions that take string lengths as an argument we should ensure that no data is read or written if a length of zero is specified. Pointers to PROT_NONE memory are used to ensure that any reads or writes will cause a fault. ChangeLog: 2014-09-16 Will Newton * string/test-memccpy.c (do_test_zero_length): New function. (test_main): Call do_test_zero_length. * string/test-memchr.c: Likewise. * string/test-memcmp.c: Likewise. * string/test-memcpy.c: Likewise. * string/test-memmem.c: Likewise. * string/test-memmove.c: Likewise. * string/test-memrchr.c: Likewise. * string/test-memset.c: Likewise. * string/test-strncmp.c: Likewise. * string/test-strncpy.c: Likewise. * string/test-strnlen.c: Likewise. * string/test-strncasecmp.c (do_test_zero_length): New function. (test_locale): Call do_test_zero_length. * string/test-strncat.c (do_test_zero_length): New function. (main): Call do_test_zero_length. --- string/test-memccpy.c | 10 ++++++++++ string/test-memchr.c | 9 +++++++++ string/test-memcmp.c | 11 +++++++++++ string/test-memcpy.c | 11 +++++++++++ string/test-memmem.c | 10 ++++++++++ string/test-memmove.c | 12 ++++++++++++ string/test-memrchr.c | 10 ++++++++++ string/test-memset.c | 11 +++++++++++ string/test-strncasecmp.c | 15 +++++++++++++++ string/test-strncat.c | 24 ++++++++++++++++++++++++ string/test-strncmp.c | 11 +++++++++++ string/test-strncpy.c | 11 +++++++++++ string/test-strnlen.c | 10 ++++++++++ 13 files changed, 155 insertions(+) diff --git a/string/test-memccpy.c b/string/test-memccpy.c index 725d640..6192e5e 100644 --- a/string/test-memccpy.c +++ b/string/test-memccpy.c @@ -230,6 +230,15 @@ do_random_tests (void) } } +static void +do_test_zero_length (void) +{ + /* Test for behaviour with zero length and pointers to PROT_NONE + memory. */ + FOR_EACH_IMPL (impl, 0) + do_one_test (impl, buf2 + page_size, buf1 + BUF1PAGES * page_size, 0, 1, 0); +} + int test_main (void) { @@ -263,6 +272,7 @@ test_main (void) } do_random_tests (); + do_test_zero_length (); return ret; } diff --git a/string/test-memchr.c b/string/test-memchr.c index 0ba79b8..29904d1 100644 --- a/string/test-memchr.c +++ b/string/test-memchr.c @@ -141,6 +141,14 @@ do_random_tests (void) } } +static void do_test_zero_length (void) +{ + /* Test for behaviour with zero length and pointer to PROT_NONE + memory. */ + FOR_EACH_IMPL (impl, 0) + do_one_test (impl, (char *) (buf2 + page_size), 'a', 0, NULL); +} + int test_main (void) { @@ -167,6 +175,7 @@ test_main (void) } do_random_tests (); + do_test_zero_length (); return ret; } diff --git a/string/test-memcmp.c b/string/test-memcmp.c index 14090ed..80436c6 100644 --- a/string/test-memcmp.c +++ b/string/test-memcmp.c @@ -471,6 +471,16 @@ check2 (void) } } +static void +do_test_zero_length (void) +{ + /* Test for behaviour with zero length and pointers to PROT_NONE + memory. */ + FOR_EACH_IMPL (impl, 0) + do_one_test (impl, (CHAR *) (buf2 + page_size), + (CHAR *) (buf1 + BUF1PAGES * page_size), 0, 0); +} + int test_main (void) { @@ -519,6 +529,7 @@ test_main (void) } do_random_tests (); + do_test_zero_length (); return ret; } #include "../test-skeleton.c" diff --git a/string/test-memcpy.c b/string/test-memcpy.c index 136c985..4d2f65b 100644 --- a/string/test-memcpy.c +++ b/string/test-memcpy.c @@ -206,6 +206,16 @@ do_random_tests (void) } } +static void +do_test_zero_length (void) +{ + /* Test for behaviour with zero length and pointers to PROT_NONE + memory. */ + FOR_EACH_IMPL (impl, 0) + do_one_test (impl, (char *) (buf2 + page_size), + (char *) (buf1 + BUF1PAGES * page_size), 0); +} + int test_main (void) { @@ -247,6 +257,7 @@ test_main (void) do_test (0, 0, getpagesize ()); do_random_tests (); + do_test_zero_length (); return ret; } diff --git a/string/test-memmem.c b/string/test-memmem.c index caaa191..6199c22 100644 --- a/string/test-memmem.c +++ b/string/test-memmem.c @@ -151,6 +151,15 @@ static const char *const strs[] = "abc0", "aaaa0", "abcabc0" }; +static void +do_test_zero_length (void) +{ + /* Test for behaviour with zero length and pointer to PROT_NONE + memory. */ + FOR_EACH_IMPL (impl, 0) + do_one_test (impl, (char *) (buf2 + page_size), 0, + strs[0], strlen (strs[0]), NULL); +} int test_main (void) @@ -178,6 +187,7 @@ test_main (void) } do_random_tests (); + do_test_zero_length (); return ret; } diff --git a/string/test-memmove.c b/string/test-memmove.c index 7e1c41c..25abb57 100644 --- a/string/test-memmove.c +++ b/string/test-memmove.c @@ -244,6 +244,17 @@ do_random_tests (void) } } +static void +do_test_zero_length (void) +{ + /* Test for behaviour with zero length and pointers to PROT_NONE + memory. */ + FOR_EACH_IMPL (impl, 0) + do_one_test (impl, (char *) (buf2 + page_size), + (char *) (buf1 + BUF1PAGES * page_size), + (char *) (buf1 + BUF1PAGES * page_size), 0); +} + int test_main (void) { @@ -283,6 +294,7 @@ test_main (void) } do_random_tests (); + do_test_zero_length (); return ret; } diff --git a/string/test-memrchr.c b/string/test-memrchr.c index efe4e9f..23c4a8b 100644 --- a/string/test-memrchr.c +++ b/string/test-memrchr.c @@ -137,6 +137,15 @@ do_random_tests (void) } } +static void +do_test_zero_length (void) +{ + /* Test for behaviour with zero length and pointer to PROT_NONE + memory. */ + FOR_EACH_IMPL (impl, 0) + do_one_test (impl, (char *) (buf2 + page_size), 'a', 0, NULL); +} + int test_main (void) { @@ -163,6 +172,7 @@ test_main (void) } do_random_tests (); + do_test_zero_length (); return ret; } diff --git a/string/test-memset.c b/string/test-memset.c index 2171b0d..b5432ce 100644 --- a/string/test-memset.c +++ b/string/test-memset.c @@ -192,6 +192,15 @@ do_random_tests (void) } #endif +static void +do_test_zero_length (void) +{ + /* Test for behaviour with zero length and pointer to PROT_NONE + memory. */ + FOR_EACH_IMPL (impl, 0) + do_one_test (impl, (char *) (buf2 + page_size), 0, 0); +} + int test_main (void) { @@ -227,6 +236,8 @@ test_main (void) do_random_tests (); #endif + do_test_zero_length (); + return ret; } diff --git a/string/test-strncasecmp.c b/string/test-strncasecmp.c index 6ad54e0..78d2dec 100644 --- a/string/test-strncasecmp.c +++ b/string/test-strncasecmp.c @@ -53,9 +53,13 @@ simple_strncasecmp (const char *s1, const char *s2, size_t n) static int stupid_strncasecmp (const char *s1, const char *s2, size_t max) { + if (max == 0) + return 0; + size_t ns1 = strlen (s1) + 1; size_t ns2 = strlen (s2) + 1; size_t n = ns1 < ns2 ? ns1 : ns2; + if (n > max) n = max; int ret = 0; @@ -258,6 +262,16 @@ bz14195 (void) } static void +do_test_zero_length (void) +{ + /* Test for behaviour with zero length and pointers to PROT_NONE + memory. */ + FOR_EACH_IMPL (impl, 0) + do_one_test (impl, (char *) (buf2 + page_size), + (char *) (buf1 + BUF1PAGES * page_size), 0, 0); +} + +static void test_locale (const char *locale) { size_t i; @@ -270,6 +284,7 @@ test_locale (const char *locale) bz12205 (); bz14195 (); + do_test_zero_length (); printf ("%23s", locale); FOR_EACH_IMPL (impl, 0) diff --git a/string/test-strncat.c b/string/test-strncat.c index 4915c59..4eef967 100644 --- a/string/test-strncat.c +++ b/string/test-strncat.c @@ -31,6 +31,10 @@ char * stupid_strncat (char *dst, const char *src, size_t n) { char *ret = dst; + + if (n == 0) + return ret; + while (*dst++ != '\0'); --dst; while (n--) @@ -232,6 +236,25 @@ do_random_tests (void) } } +static void +do_test_zero_length (void) +{ + char dst[1]; + char *src = (char *) (buf2 + page_size); + dst[0] = '\0'; + + /* Test for behaviour with zero length and pointer to PROT_NONE + memory. */ + FOR_EACH_IMPL (impl, 0) + if (CALL (impl, dst, src, 0) != dst) + { + error (0, 0, "Wrong result in function %s %p != %p", impl->name, + CALL (impl, dst, src, 0), dst); + ret = 1; + return; + } +} + int main (void) { @@ -269,5 +292,6 @@ main (void) } do_random_tests (); + do_test_zero_length (); return ret; } diff --git a/string/test-strncmp.c b/string/test-strncmp.c index f3b2c68..225bab5 100644 --- a/string/test-strncmp.c +++ b/string/test-strncmp.c @@ -317,6 +317,16 @@ check2 (void) free (s2); } +static void +do_test_zero_length (void) +{ + /* Test for behaviour with zero length and pointers to PROT_NONE + memory. */ + FOR_EACH_IMPL (impl, 0) + do_one_test (impl, (char *) (buf2 + page_size), + (char *) (buf1 + BUF1PAGES * page_size), 0, 0); +} + int test_main (void) { @@ -387,6 +397,7 @@ test_main (void) } do_random_tests (); + do_test_zero_length (); return ret; } diff --git a/string/test-strncpy.c b/string/test-strncpy.c index 2326acc..455da43 100644 --- a/string/test-strncpy.c +++ b/string/test-strncpy.c @@ -245,6 +245,16 @@ do_random_tests (void) } } +static void +do_test_zero_length (void) +{ + /* Test for behaviour with zero length and pointers to PROT_NONE + memory. */ + FOR_EACH_IMPL (impl, 0) + do_one_test (impl, (char *) (buf2 + page_size), + (char *) (buf1 + BUF1PAGES * page_size), 0, 0); +} + int test_main (void) { @@ -278,6 +288,7 @@ test_main (void) } do_random_tests (); + do_test_zero_length (); return ret; } diff --git a/string/test-strnlen.c b/string/test-strnlen.c index be9edd2..c8fd05a 100644 --- a/string/test-strnlen.c +++ b/string/test-strnlen.c @@ -122,6 +122,15 @@ do_random_tests (void) } } +static void +do_test_zero_length (void) +{ + /* Test for behaviour with zero length and pointer to PROT_NONE + memory. */ + FOR_EACH_IMPL (impl, 0) + do_one_test (impl, (char *) (buf2 + page_size), 0, 0); +} + int test_main (void) { @@ -167,6 +176,7 @@ test_main (void) } do_random_tests (); + do_test_zero_length (); return ret; }