From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id A52BB3858C2D for ; Tue, 11 Apr 2023 19:05:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A52BB3858C2D Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681239937; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=i+7AtU6MlTOfk5OVmF0s+o3wc2xXUt9EddryY1faokE=; b=VUCskcSCZuYg+bYaHS0zIH1XX1Cm6DyPkRToPiRU4zaT/5o7DQsXY7zoPOEVKoyHZov+bg BqLBqkKi4rEKhCh6+n5phO0XuWYP86wph2RHnsY7eOkkdBvGHBlrGyaycvB3utL6UyqsY8 BDYNA6aGAX4MzYgRuE/kqVqQq3Vk0FA= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-523-pqKn82ryNnqeTGWK9zeihw-1; Tue, 11 Apr 2023 15:05:35 -0400 X-MC-Unique: pqKn82ryNnqeTGWK9zeihw-1 Received: by mail-qk1-f200.google.com with SMTP id d184-20020a37b4c1000000b007467c5d3abeso4861942qkf.19 for ; Tue, 11 Apr 2023 12:05:35 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1681239935; x=1683831935; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=i+7AtU6MlTOfk5OVmF0s+o3wc2xXUt9EddryY1faokE=; b=B069NRfhK9POOSOGvkEEZM1vpl8prDZ4Pn+oobrruPCzYC8vvA9KAfuyqSRJzcNkad cDix6Ygy1G81HDpKuPGQ7jelUTyeoy7wqX2lI0F8y6ALT/VSfsfpztxNxktn5scaHDEo ARy5Q9X6AaK6ABXA4VoLGG5oRMaNjDTK5240AEa6bT1nem1viq33RjCty/4yzF42JAp7 em7vjZzJlprlgzi9ebM2vJTHR2sNh+EstupwJ2eUCiAfGdiVtdGxESVCEnhZvnIRaHVU otskDY2cOkxOsUQlcdD7LRTwZAt81zx36EN/F3PPH7kNHThhpCz1n3kHBZMZNXAhFAAN fX2Q== X-Gm-Message-State: AAQBX9fl36Nhlzt2oHitpc/g1lk/wbEXsKxarPXM7Cc1WsaVIjlI4cKl Q4uxmBDUunq+Pk5B4Z5Fes+s5OyroMCDccyjeL5BqT2b2x3P948xdNYV8qGFgTMIx8+NmbtOht4 lU7Ywv2FbGgt66APuulPBDMpqHlmvJGZG4v8PdQ+ov5uEM87E6PHxzZIdqu6WleIjNWUUexDcQd Ukf6cv X-Received: by 2002:ac8:7c49:0:b0:3e3:882d:4b46 with SMTP id o9-20020ac87c49000000b003e3882d4b46mr22221458qtv.54.1681239934890; Tue, 11 Apr 2023 12:05:34 -0700 (PDT) X-Google-Smtp-Source: AKy350av0wVsW9gnv73gxn8Y6jcbEl2SufLdds+vZxNDolsLkdDRtINXJYa7rlBfjz84Ql8xNFlDEg== X-Received: by 2002:ac8:7c49:0:b0:3e3:882d:4b46 with SMTP id o9-20020ac87c49000000b003e3882d4b46mr22221400qtv.54.1681239934282; Tue, 11 Apr 2023 12:05:34 -0700 (PDT) Received: from oak.redhat.com (c-71-206-142-238.hsd1.va.comcast.net. [71.206.142.238]) by smtp.gmail.com with ESMTPSA id x1-20020ac85381000000b003e014845d9esm3760434qtp.74.2023.04.11.12.05.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Apr 2023 12:05:33 -0700 (PDT) From: Joe Simmons-Talbott To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH v5 3/4] string: Add tests for strndup (BZ #30266) Date: Tue, 11 Apr 2023 15:05:25 -0400 Message-Id: <20230411190526.2469122-4-josimmon@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230411190526.2469122-1-josimmon@redhat.com> References: <20230329150500.2886135-1-josimmon@redhat.com> <20230411190526.2469122-1-josimmon@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Copy strncpy tests for strndup. Covers some basic testcases with random strings. Remove tests that set the destination's bytes and checked the resulting buffer's bytes. Remove wide character test support since wcsndup() doesn't exist. --- string/Makefile | 1 + string/test-strndup.c | 200 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 201 insertions(+) create mode 100644 string/test-strndup.c diff --git a/string/Makefile b/string/Makefile index 472e318aea..2eef6f1dfc 100644 --- a/string/Makefile +++ b/string/Makefile @@ -163,6 +163,7 @@ tests := \ test-strncat \ test-strncmp \ test-strncpy \ + test-strndup \ test-strnlen \ test-strpbrk \ test-strrchr \ diff --git a/string/test-strndup.c b/string/test-strndup.c new file mode 100644 index 0000000000..841145dc92 --- /dev/null +++ b/string/test-strndup.c @@ -0,0 +1,200 @@ +/* Test strndup functions. + Copyright (C) 2023 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 + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +#define TEST_MAIN +#include "test-string.h" + +static void +do_one_test (const char *src, size_t len, size_t n) +{ + char *dst = strndup(src, n); + size_t s = (len > n ? n: len) * sizeof (char); + + TEST_COMPARE_BLOB(dst, s, src, s); +} + +static void +do_test (size_t align1, size_t align2, size_t len, size_t n, int max_char) +{ + size_t i; + char *s1; + + align1 &= 7; + if ((align1 + len) * sizeof (char) >= page_size) + return; + + align2 &= 7; + if ((align2 + len) * sizeof (char) >= page_size) + return; + + s1 = (char *) (buf1) + align1; + + for (i = 0; i < len; ++i) + s1[i] = 32 + 23 * i % (max_char - 32); + s1[len] = 0; + for (i = len + 1; (i + align1) * sizeof (char) < page_size && i < len + 64; + ++i) + s1[i] = 32 + 32 * i % (max_char - 32); + + do_one_test (s1, len, n); +} + +static void +do_page_tests (void) +{ + char *s1; + const size_t maxoffset = 64; + + /* Put s1 at the maxoffset from the edge of buf1's last page. */ + s1 = (char *) buf1 + BUF1PAGES * page_size / sizeof (char) - maxoffset; + /* s2 needs room to put a string with size of maxoffset + 1 at s2 + + (maxoffset - 1). */ + + memset (s1, 'a', maxoffset - 1); + s1[maxoffset - 1] = '\0'; + + /* Both strings are bounded to a page with read/write access and the next + page is protected with PROT_NONE (meaning that any access outside of the + page regions will trigger an invalid memory access). + + The loop copies the string s1 for all possible offsets up to maxoffset + for both inputs with a size larger than s1 (so memory access outside the + expected memory regions might trigger invalid access). */ + + for (size_t off1 = 0; off1 < maxoffset; off1++) + for (size_t off2 = 0; off2 < maxoffset; off2++) + do_one_test (s1 + off1, maxoffset - off1 - 1, + maxoffset + (maxoffset - off2)); +} + +static void +do_random_tests (void) +{ + size_t i, j, n, align1, align2, len, size, mode; + unsigned char *p1 = (unsigned char *) (buf1 + page_size) - 512; + unsigned char *res; + + for (n = 0; n < ITERATIONS; n++) + { + mode = random (); + if (mode & 1) + { + size = random () & 255; + align1 = 512 - size - (random () & 15); + if (mode & 2) + align2 = align1 - (random () & 24); + else + align2 = align1 - (random () & 31); + if (mode & 4) + { + j = align1; + align1 = align2; + align2 = j; + } + if (mode & 8) + len = size - (random () & 31); + else + len = 512; + if (len >= 512) + len = random () & 511; + } + else + { + align1 = random () & 31; + if (mode & 2) + align2 = random () & 31; + else + align2 = align1 + (random () & 24); + len = random () & 511; + j = align1; + if (align2 > j) + j = align2; + if (mode & 4) + { + size = random () & 511; + if (size + j > 512) + size = 512 - j - (random () & 31); + } + else + size = 512 - j; + if ((mode & 8) && len + j >= 512) + len = 512 - j - (random () & 7); + } + j = len + align1 + 64; + if (j > 512) + j = 512; + for (i = 0; i < j; i++) + { + if (i == len + align1) + p1[i] = 0; + else + { + p1[i] = random () & CHAR_MAX; + if (i >= align1 && i < len + align1 && !p1[i]) + p1[i] = (random () & 127) + 3; + } + } + + res = (unsigned char *) strndup ((char *) (p1 + align1), size); + j = len + 1; + if (size < j) + j = size; + TEST_COMPARE_BLOB (res, j, (char *) (p1 + align1), j); + free (res); + } +} + +int +test_main (void) +{ + size_t i; + + test_init (); + + printf ("%28s", ""); + printf ("\t%s", "strndup"); + putchar ('\n'); + + for (i = 1; i < 8; ++i) + { + do_test (i, i, 16, 16, 127); + do_test (i, i, 16, 16, CHAR_MAX); + do_test (i, 2 * i, 16, 16, 127); + do_test (2 * i, i, 16, 16, CHAR_MAX); + do_test (8 - i, 2 * i, 1 << i, 2 << i, 127); + do_test (2 * i, 8 - i, 2 << i, 1 << i, 127); + do_test (8 - i, 2 * i, 1 << i, 2 << i, CHAR_MAX); + do_test (2 * i, 8 - i, 2 << i, 1 << i, CHAR_MAX); + } + + for (i = 1; i < 8; ++i) + { + do_test (0, 0, 4 << i, 8 << i, 127); + do_test (0, 0, 16 << i, 8 << i, 127); + do_test (8 - i, 2 * i, 4 << i, 8 << i, 127); + do_test (8 - i, 2 * i, 16 << i, 8 << i, 127); + } + + do_random_tests (); + do_page_tests (); + return ret; +} + +#include -- 2.39.2