From patchwork Thu Mar 30 14:50:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joe Simmons-Talbott X-Patchwork-Id: 67123 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 74BD33870897 for ; Thu, 30 Mar 2023 14:50:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 74BD33870897 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1680187842; bh=EPZNi8/hEEgUECpLs9wVJunFQUwN4wo/h61ZGMFyzjQ=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=q7hFfnA6ZonGFb+6GYgntZRKrCIZmZvtZD94Pg+uolAOJXmWLnVuQcZqonOfVnhGq Dt1CtxfLL+RN5lvwy1xVZdlCZ37mkUZVvIpj6nRVVmKtIVT+BhehObYOTNfQN+Fhdj kvwdfO8VKp1BwLbPVauqcypSGUnH1R3oho+zpNCM= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 133413858434 for ; Thu, 30 Mar 2023 14:50:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 133413858434 Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-564-D1FPwKexPAipRM8iZJdgBw-1; Thu, 30 Mar 2023 10:50:16 -0400 X-MC-Unique: D1FPwKexPAipRM8iZJdgBw-1 Received: by mail-qt1-f199.google.com with SMTP id m7-20020a05622a118700b003e4e203bc30so10194810qtk.7 for ; Thu, 30 Mar 2023 07:50:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680187816; 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=EPZNi8/hEEgUECpLs9wVJunFQUwN4wo/h61ZGMFyzjQ=; b=DP4furDBAB9MEhWjBxDHQVLbjlRP/eTnh4SXs1KFp4kk7eV8ylVteQORLXTei6dN1F ATdblI/89bsoP+ru4B2SOge1d+SqdZhu88ePnBpyQ2TAGGUf9XY2p8GFvNUArtk4ulWe 7ItqVTE87lDWMLGU6GxEPGPFXYv13c13a7GVY61XQRAUljz//EVWGs1h1Zuaaf44SBYF iSA0aDGmzy00Gqb0zVjZrZhtwGTWcz1AuDw1vMpAR9TTqQhnOKG8C7Q14fqLkuFpvDZI 0smwGCRhUGGDZf4UslFOjaOHauVFfRgHxmGtMfwqWGF2yc5VzmM+Iyew7nZjl4juocoA awBw== X-Gm-Message-State: AAQBX9ftfPU5SwD5HMz1s7GNIOM/8Qt4+u5DhgGqpEndF8D/QuWkBhbh 0VBc3y6nfFEAgi11aP+XgaxUq8iiJOxust+i8Dh+FH0XF9UiwMMukD5mTtoLYIkVOzcCvMib3kg +MWc94sffN0/UGm1aqOpgo05IJThViZJ6IYPuOVfKubT7r/A39ppQg/5mMatMMCOznyzo7nm9vn KQZ/gG X-Received: by 2002:a05:6214:1c4e:b0:5c2:b08a:3b9e with SMTP id if14-20020a0562141c4e00b005c2b08a3b9emr38891762qvb.29.1680187815768; Thu, 30 Mar 2023 07:50:15 -0700 (PDT) X-Google-Smtp-Source: AKy350bGwrtXXCZIHGZtclexYKJj8ptUTJzlAaMxeYiLwx3OI7cIq7vun1hQTTmEUGGaKLkBNOJ7IQ== X-Received: by 2002:a05:6214:1c4e:b0:5c2:b08a:3b9e with SMTP id if14-20020a0562141c4e00b005c2b08a3b9emr38891713qvb.29.1680187815326; Thu, 30 Mar 2023 07:50:15 -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 f13-20020ad442cd000000b005dd8b9345b8sm5484384qvr.80.2023.03.30.07.50.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Mar 2023 07:50:14 -0700 (PDT) To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH v2 1/2] strdup: Add tests (BZ #30266) Date: Thu, 30 Mar 2023 10:50:07 -0400 Message-Id: <20230330145008.3035194-2-josimmon@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230330145008.3035194-1-josimmon@redhat.com> References: <20230329150500.2886135-1-josimmon@redhat.com> <20230330145008.3035194-1-josimmon@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.5 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 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Joe Simmons-Talbott via Libc-alpha From: Joe Simmons-Talbott Reply-To: Joe Simmons-Talbott Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Copy strcpy tests for strdup. Covers some basic testcases with random strings. Add a zero-length string testcase. Signed-off-by: Joe Simmons-Talbott --- string/Makefile | 1 + string/test-strdup.c | 241 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 242 insertions(+) create mode 100644 string/test-strdup.c diff --git a/string/Makefile b/string/Makefile index c84b49aaa5..472e318aea 100644 --- a/string/Makefile +++ b/string/Makefile @@ -157,6 +157,7 @@ tests := \ test-strcmp \ test-strcpy \ test-strcspn \ + test-strdup \ test-strlen \ test-strncasecmp \ test-strncat \ diff --git a/string/test-strdup.c b/string/test-strdup.c new file mode 100644 index 0000000000..04eedafc24 --- /dev/null +++ b/string/test-strdup.c @@ -0,0 +1,241 @@ +/* Test and measure strdup 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 + +#ifdef WIDE +# include +# define CHAR wchar_t +# define UCHAR wchar_t +# define sfmt "ls" +# define BIG_CHAR WCHAR_MAX +# define SMALL_CHAR 1273 +# define STRCMP wcscmp +# define MEMCMP wmemcmp +# define MEMSET wmemset +#else +# define CHAR char +# define UCHAR unsigned char +# define sfmt "s" +# define BIG_CHAR CHAR_MAX +# define SMALL_CHAR 127 +# define STRCMP strcmp +# define MEMCMP memcmp +# define MEMSET memset +#endif + +#ifndef STRDUP_RESULT +# define STRDUP_RESULT(dst, len) dst +# define TEST_MAIN +# ifndef WIDE +# define TEST_NAME "strdup" +# else +# define TEST_NAME "wcsdup" +# endif +# include "test-string.h" +# ifndef WIDE +# define STRDUP strdup +# else +# define STRDUP wcsdup +# endif + +IMPL (STRDUP, 1) + +/* Also check the generic implementation. */ +#undef STRDUP +#undef libc_hidden_builtin_def +#define libc_hidden_builtin_def(a) +#undef libc_hidden_def +#define libc_hidden_def(a) +#undef weak_alias +#define weak_alias(a,b) +#undef attribute_hidden +#define attribute_hidden +#endif + +typedef CHAR *(*proto_t) (const CHAR *); + +static void +do_zero_len_test (void) +{ + char src[1] = { '\0' }; + char *dst = strdup (src); + + TEST_COMPARE_STRING (dst, src); +} + +static void +do_one_test (const CHAR *src, + size_t len __attribute__((unused))) +{ + char *dst = strdup (src); + + if (STRCMP (dst, src) != 0) + { + error (0, 0, + "Wrong result in function strdup dst \"%" sfmt "\" src \"%" sfmt "\"", + dst, src); + ret = 1; + return; + } +} + +static void +do_test (size_t align1, size_t align2, size_t len, int max_char) +{ + size_t i; + CHAR *s1; +/* For wcsdup: align1 and align2 here mean alignment not in bytes, + but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t)) + len for wcschr here isn't in bytes but it's number of wchar_t symbols. */ + 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; + + do_one_test (s1, len); +} + +static void +do_random_tests (void) +{ + size_t i, j, n, align1, align2, len; + UCHAR *p1 = (UCHAR *) (buf1 + page_size) - 512; + UCHAR *p2 = (UCHAR *) (buf2 + page_size) - 512; + UCHAR *res; + + for (n = 0; n < ITERATIONS; n++) + { + /* align1 and align2 are expressed as wchar_t and not in bytes for wide + char test, and thus it will be equal to align times wchar_t size. + + For non wide version we need to check all alignments from 0 to 63 + since some assembly implementations have separate prolog for alignments + more 48. */ + + align1 = random () & (63 / sizeof (CHAR)); + if (random () & 1) + align2 = random () & (63 / sizeof (CHAR)); + else + align2 = align1 + (random () & 24); + len = random () & 511; + j = align1; + if (align2 > j) + j = align2; + if (len + j >= 511) + len = 510 - 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 () & BIG_CHAR; + if (i >= align1 && i < len + align1 && !p1[i]) + p1[i] = (random () & SMALL_CHAR) + 3; + } + } + + FOR_EACH_IMPL (impl, 1) + { + MEMSET (p2 - 64, '\1', 512 + 64); + res = (UCHAR *) CALL (impl, (CHAR *) (p1 + align1)); + for (j = 0; j < align2 + 64; ++j) + { + if (p2[j - 64] != '\1') + { + error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd)", + n, impl->name, align1, align2, len); + ret = 1; + break; + } + } + for (j = align2 + len + 1; j < 512; ++j) + { + if (p2[j] != '\1') + { + error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd)", + n, impl->name, align1, align2, len); + ret = 1; + break; + } + } + if (MEMCMP (p1 + align1, res, len + 1)) + { + error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)", + n, impl->name, align1, align2, len); + ret = 1; + } + } + } +} + + +int +test_main (void) +{ + size_t i; + + test_init (); + + printf ("%23s", ""); + FOR_EACH_IMPL (impl, 0) + printf ("\t%s", impl->name); + putchar ('\n'); + + for (i = 0; i < 16; ++i) + { + do_test (0, 0, i, SMALL_CHAR); + do_test (0, 0, i, BIG_CHAR); + do_test (0, i, i, SMALL_CHAR); + do_test (i, 0, i, BIG_CHAR); + } + + for (i = 1; i < 8; ++i) + { + do_test (0, 0, 8 << i, SMALL_CHAR); + do_test (8 - i, 2 * i, 8 << i, SMALL_CHAR); + } + + for (i = 1; i < 8; ++i) + { + do_test (i, 2 * i, 8 << i, SMALL_CHAR); + do_test (2 * i, i, 8 << i, BIG_CHAR); + do_test (i, i, 8 << i, SMALL_CHAR); + do_test (i, i, 8 << i, BIG_CHAR); + } + + do_zero_len_test (); + do_random_tests (); + + return ret; +} + +#include From patchwork Thu Mar 30 14:50:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joe Simmons-Talbott X-Patchwork-Id: 67124 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0500A38708AB for ; Thu, 30 Mar 2023 14:50:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0500A38708AB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1680187848; bh=lrUAtZ6R9714tFCOt+hV1WF5ZI74THidr5ScV6ZaCbo=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=J4w6QRiXMxOXSRah2iHIhLBtqtCtBZ78MecYUN/O8cfk4rW0Lt/ao4uWBtlSfslmT VWY6ZMFx+f8xa7hBvQnUzAHyreJrL/UOHZdjsV0zvWU1ENvucdvAapUAtLrWA8IKQp HEhHGCZvvhhTpr22XB/OffujDJr2PwRXG4j28gt8= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 11EC9385B50C for ; Thu, 30 Mar 2023 14:50:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 11EC9385B50C 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-518-prRSJIVWMviQtAQ_tSCQNg-1; Thu, 30 Mar 2023 10:50:19 -0400 X-MC-Unique: prRSJIVWMviQtAQ_tSCQNg-1 Received: by mail-qk1-f200.google.com with SMTP id 72-20020a37044b000000b0074694114c09so8910695qke.4 for ; Thu, 30 Mar 2023 07:50:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680187818; 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=lrUAtZ6R9714tFCOt+hV1WF5ZI74THidr5ScV6ZaCbo=; b=jlCDXoaaBRFgDxEEIlhvIm623lDouyImIyKPC43zi1oIRC6SEPx11tWiCaFj24wbgK DFCzqnMjli5WYtjwEI9TcSd02E/VxuAfgNmlJrFSnVSmaXXOGi3+G7J88rBywL1BRecT 88uKaKDICQRQQuTzJmTpLceP67yPGny6cLrr4vRbYVOFof5wTOcq4DRVHFoeOy6jNSyf Gf28gVwLmyWiXlkfoDi4vmhKmIuTqAbfP1edzXvoZCWv9gq+Z6fbzJiIuRqMCHUyyF7j 37SRr2bZJ0cQhuFbtlLLTnXdDhAjki68YynT1FzzHOakjBSpWyt1KJHTKMjPuYwwCMOe nRMw== X-Gm-Message-State: AAQBX9ca4LxVF2oVvRTf7IHl2RCChxMll2t8fwuEcub+rmMmXUED4jwc aM2zoxI/kG6EaSjj8KCxAktgzvd75TEXL0XvvPzPTYC3fvieMgATg9RAbJdxua15ee+bU5ilZ0e vSCRkAVWtEWCv5DIrxfaHR4hwiKfYkvKWMQkmuUd6KDaIzAeMjjHMczAuB+q/3EsD9BS03XoIfH EwOWhJ X-Received: by 2002:a05:6214:62e:b0:5cc:277c:b52 with SMTP id a14-20020a056214062e00b005cc277c0b52mr27103806qvx.51.1680187818417; Thu, 30 Mar 2023 07:50:18 -0700 (PDT) X-Google-Smtp-Source: AKy350alSME2KSTMOJnEi/l3CmFWN092arTjQBp2I8EW1lMBusMtvWahnqvLOG+2Y/MbkFlxKVyBwQ== X-Received: by 2002:a05:6214:62e:b0:5cc:277c:b52 with SMTP id a14-20020a056214062e00b005cc277c0b52mr27103700qvx.51.1680187817069; Thu, 30 Mar 2023 07:50:17 -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 f13-20020ad442cd000000b005dd8b9345b8sm5484384qvr.80.2023.03.30.07.50.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Mar 2023 07:50:16 -0700 (PDT) To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH v2 2/2] strndup: Add tests (BZ #30266) Date: Thu, 30 Mar 2023 10:50:08 -0400 Message-Id: <20230330145008.3035194-3-josimmon@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230330145008.3035194-1-josimmon@redhat.com> References: <20230329150500.2886135-1-josimmon@redhat.com> <20230330145008.3035194-1-josimmon@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.7 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 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Joe Simmons-Talbott via Libc-alpha From: Joe Simmons-Talbott Reply-To: Joe Simmons-Talbott Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" 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. Signed-off-by: Joe Simmons-Talbott --- string/Makefile | 1 + string/test-strndup.c | 255 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 256 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..ab6428f4fb --- /dev/null +++ b/string/test-strndup.c @@ -0,0 +1,255 @@ +/* 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 + +#ifdef WIDE +# include +# define CHAR wchar_t +# define UCHAR wchar_t +# define BIG_CHAR WCHAR_MAX +# define SMALL_CHAR 1273 +# define MEMCMP wmemcmp +# define MEMSET wmemset +# define STRNLEN wcsnlen +#else +# define CHAR char +# define UCHAR unsigned char +# define BIG_CHAR CHAR_MAX +# define SMALL_CHAR 127 +# define MEMCMP memcmp +# define MEMSET memset +# define STRNLEN strnlen +#endif /* !WIDE */ + + +#ifndef STRNDUP_RESULT +# define STRNDUP_RESULT(dst, len, n) dst +# define TEST_MAIN +# ifndef WIDE +# define TEST_NAME "strndup" +# else +# define TEST_NAME "wcsndup" +# endif /* WIDE */ +# include "test-string.h" +# ifndef WIDE +# define STRNDUP strndup +# else +# define STRNDUP wcsndup +# endif /* WIDE */ + + +IMPL (STRNDUP, 1) + + +#endif /* !STRNDUP_RESULT */ + +typedef CHAR *(*proto_t) (const CHAR *, size_t); + +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; + +/* For wcsndup: align1 and align2 here mean alignment not in bytes, + but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t)). */ + 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; + UCHAR *p1 = (UCHAR *) (buf1 + page_size) - 512; + UCHAR *res; + + for (n = 0; n < ITERATIONS; n++) + { + /* For wcsndup: align1 and align2 here mean align not in bytes, + but in wchar_ts, in bytes it will equal to align * (sizeof + (wchar_t)). */ + + 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 () & BIG_CHAR; + if (i >= align1 && i < len + align1 && !p1[i]) + p1[i] = (random () & SMALL_CHAR) + 3; + } + } + + res = (UCHAR *) strndup ((CHAR *) (p1 + align1), size); + j = len + 1; + if (size < j) + j = size; + if (MEMCMP (p1 + align1, res, j)) + { + error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)", + n, "strndup", align1, align2, len); + ret = 1; + } + } +} + +int +test_main (void) +{ + size_t i; + + test_init (); + + printf ("%28s", ""); + FOR_EACH_IMPL (impl, 0) + printf ("\t%s", impl->name); + putchar ('\n'); + + for (i = 1; i < 8; ++i) + { + do_test (i, i, 16, 16, SMALL_CHAR); + do_test (i, i, 16, 16, BIG_CHAR); + do_test (i, 2 * i, 16, 16, SMALL_CHAR); + do_test (2 * i, i, 16, 16, BIG_CHAR); + do_test (8 - i, 2 * i, 1 << i, 2 << i, SMALL_CHAR); + do_test (2 * i, 8 - i, 2 << i, 1 << i, SMALL_CHAR); + do_test (8 - i, 2 * i, 1 << i, 2 << i, BIG_CHAR); + do_test (2 * i, 8 - i, 2 << i, 1 << i, BIG_CHAR); + } + + for (i = 1; i < 8; ++i) + { + do_test (0, 0, 4 << i, 8 << i, SMALL_CHAR); + do_test (0, 0, 16 << i, 8 << i, SMALL_CHAR); + do_test (8 - i, 2 * i, 4 << i, 8 << i, SMALL_CHAR); + do_test (8 - i, 2 * i, 16 << i, 8 << i, SMALL_CHAR); + } + + do_random_tests (); + do_page_tests (); + return ret; +} + +#include