From nobody Sat Sep 14 18:42:59 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4X5g6D0N5Qz5V6hC; Sat, 14 Sep 2024 18:43:00 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4X5g6C6x7pz477j; Sat, 14 Sep 2024 18:42:59 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1726339380; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=rDfU53sq3PA5NdWyQLqr5pchdtKuxT1ZQsAd6Z2S/Sk=; b=pl3D3dHodpPxPH1Kjd1tNtsJuzxfvcURGecyhvs/cUD9F2Ol/LIWZwJat1dyGUuUiYUOpw j5kwY0KnMS3Tp/lNKvQ1BuTrNdVTNi04axFHrc85TJSj05sO5DyA67oGWpOQtrSbm8//13 FnYaYpEf3Kb6716fmLp4BXRLICLPuLX2wenxU2o7MYAFQ7GznNhH6cg/j1WlR0pDvEy4X/ TvxChwaShjqRFIKIwZ2yQptv2IF4ubmjX/q96l0MIPL9OgJUvmeMSc1FVL+EGC92zFgQa8 lwivXbieNFVjClhHhCIQUKMT836gwWZMx2Dky6MkcUmNmVQkjgNd/8Id++a0vQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1726339380; a=rsa-sha256; cv=none; b=bqnJDebt3s7/q6ozbpHqM2KozGoZQ2t6El6cF+iF3fyCxSlI4g96EtjHw+ujv9LO2x5O4w VVA9BU9PCdiUzMHr/AZQkRr1xCeZ/cpQNKQ9dPJsDbbJ7tTes5kdit8Bxrehs4gPAosSx4 n1/W4f6stpsqaDE3s66DkRSoG8CIy0ZsbqgkTAv9UBXZ+Fq+WYvx1kX8tkLkcauLunySLy 9MgXOysioZhOxiXHUREyVUvTgpcTSyuHUShzMNP9pYQexxbwb5NQ/Zs5xXNzdOOpsyngT2 Oo6ChViXrqVbSmO4L7+3a7eXbL5O6yuWCJjWkKCylzrvqCFnyRlqsu/K/40hXA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1726339380; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=rDfU53sq3PA5NdWyQLqr5pchdtKuxT1ZQsAd6Z2S/Sk=; b=ntj2irBqFPDC2GlhmPyTF1aQziyWC+MMdf6SX6xOwRY6SaG/WXVUzGtenEZso3KOEpm877 1uy8Qy4d1m1/Ug1KJZ8gbWwlFKjaL6zTJk70oCE0S+52AOed2W8vZSc0WP2eHRivxTb2Qu WVUiTlnV2mGKduYN4YXB4uSw+MCUBW9l8m9WfI9ERsdKgQ/f4d1a++GH/Q2uNAjxtnB5Rv GyAd0imAsPmNiC4H+9vH+2mlaN2bKYJpT+SqLsyQrZGZGCRoDd5BFOsgJKuxMRKYtOp36e ZrmhGKpOVX8tEEl/Qhy+YyS96rJAD0cyk+dWLodrRo1Y+lHaSxyWkdTAN1vlaw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4X5g6C6XbHzdgZ; Sat, 14 Sep 2024 18:42:59 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 48EIgxuw070425; Sat, 14 Sep 2024 18:42:59 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 48EIgxsn070422; Sat, 14 Sep 2024 18:42:59 GMT (envelope-from git) Date: Sat, 14 Sep 2024 18:42:59 GMT Message-Id: <202409141842.48EIgxsn070422@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Robert Clausecker Subject: git: 61ed5748e4e9 - main - lib/libc/tests/string: improve memccpy "bounds" unit test List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: fuz X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 61ed5748e4e9c7397fcb2638b442f46ac5c9e7c5 Auto-Submitted: auto-generated The branch main has been updated by fuz: URL: https://cgit.FreeBSD.org/src/commit/?id=61ed5748e4e9c7397fcb2638b442f46ac5c9e7c5 commit 61ed5748e4e9c7397fcb2638b442f46ac5c9e7c5 Author: Robert Clausecker AuthorDate: 2024-07-19 20:50:28 +0000 Commit: Robert Clausecker CommitDate: 2024-09-14 18:42:19 +0000 lib/libc/tests/string: improve memccpy "bounds" unit test The purpose of the "bounds" test is to check that the function does not overread the array bounds. The old unit test, copied from the strlcpy() one, always ensured that we see the character c memccpy() is looking for in the source array before the array ends. While this is correct for strlcpy(), memccpy()'s specification does not guarantee that c is present within the given size limit. The updated test handles this case better, ensuring that the source array ends early if c is not supposed to be present. Reported by: getz Approved by: emaste See also: D46052 Event: GSoC 2024 Differential Revision: https://reviews.freebsd.org/D46051 --- lib/libc/tests/string/memccpy_test.c | 50 ++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/lib/libc/tests/string/memccpy_test.c b/lib/libc/tests/string/memccpy_test.c index 82f4ef34af54..4784fee4ede5 100644 --- a/lib/libc/tests/string/memccpy_test.c +++ b/lib/libc/tests/string/memccpy_test.c @@ -1,6 +1,6 @@ /*- * Copyright (c) 2009 David Schultz - * Copyright (c) 2023 The FreeBSD Foundation + * Copyright (c) 2023, 2024 The FreeBSD Foundation * All rights reserved. * * Portions of this software were developed by Robert Clausecker @@ -54,34 +54,57 @@ makebuf(size_t len, int guard_at_end) buf = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0); assert(buf); if (guard_at_end) { - assert(munmap(buf + alloc_size - page_size, page_size) == 0); + assert(mprotect(buf + alloc_size - page_size, page_size, PROT_NONE) == 0); return (buf + alloc_size - page_size - len); } else { - assert(munmap(buf, page_size) == 0); + assert(mprotect(buf, page_size, PROT_NONE) == 0); return (buf + page_size); } } static void -test_memccpy(const char *s) +freebuf(char * buf, size_t len, int guard_at_end) +{ + size_t alloc_size, page_size; + + page_size = getpagesize(); + alloc_size = roundup2(len, page_size) + page_size; + + if (guard_at_end) + munmap(buf + len + page_size - alloc_size, alloc_size); + else + munmap(buf - page_size, alloc_size); +} + +static void +test_memccpy(const char *s, size_t size) { char *src, *dst, *expected; - size_t size, bufsize, x; + size_t bufsize, x; int i, j; - size = strlen(s) + 1; for (i = 0; i <= 1; i++) { for (j = 0; j <= 1; j++) { - for (bufsize = 0; bufsize <= size + 10; bufsize++) { - src = makebuf(size, i); - memcpy(src, s, size); + for (bufsize = 0; bufsize <= size + 32; bufsize++) { dst = makebuf(bufsize, j); + if (bufsize < size) { + src = makebuf(bufsize, i); + memcpy(src, s, bufsize); + expected = NULL; + } else { + src = makebuf(size, i); + memcpy(src, s, size); + expected = dst + size; + } + memset(dst, 'X', bufsize); - expected = bufsize >= size ? dst + size : NULL; - assert(memccpy_fn(dst, src, src[size-1], bufsize) == expected); - assert(bufsize == 0 || strncmp(src, dst, bufsize - 1) == 0); + assert(memccpy_fn(dst, src, s[size-1], bufsize) == expected); + assert(memcmp(src, dst, MIN(bufsize, size)) == 0); for (x = size; x < bufsize; x++) assert(dst[x] == 'X'); + + freebuf(dst, bufsize, j); + freebuf(src, bufsize < size ? bufsize : size, i); } } } @@ -168,7 +191,8 @@ ATF_TC_BODY(bounds, tc) for (i = 0; i < sizeof(buf) - 1; i++) { buf[i] = ' ' + i; - test_memccpy(buf); + buf[i+1] = '\0'; + test_memccpy(buf, i + 1); } }