From patchwork Tue Jun 20 18:18:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frederic Berat X-Patchwork-Id: 1797503 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=n0tPjUJp; dkim-atps=neutral Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Qlw0V5j71z20Wk for ; Wed, 21 Jun 2023 04:20:10 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CEECC385841B for ; Tue, 20 Jun 2023 18:20:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CEECC385841B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1687285208; bh=5wsV9wIlc1HEJ90PJoS6PqqZBm+RUsjUbKxVcF+G0Fk=; 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=n0tPjUJphYpBwyaQjyw3zCp2kSPlr+HEGCMTFlhRRupSR9BpHYpzOWrf+5OyE7YPr 1xo7tXBJqHGiaDERMszs3TEfHMx8fWSJ1cYO6Bjuv/aEZIc8dW46qsyhbT978rI2GI d2Y7Or33LuVVVyy6JPo9ZaUYXBwXLYzcu3AMctoY= 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 7D6033858C78 for ; Tue, 20 Jun 2023 18:19:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7D6033858C78 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-280-14wBQLAIP3m8Esz7yk8Cvg-1; Tue, 20 Jun 2023 14:19:31 -0400 X-MC-Unique: 14wBQLAIP3m8Esz7yk8Cvg-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 64C88185A794; Tue, 20 Jun 2023 18:19:31 +0000 (UTC) Received: from Nymeria-redhat.redhat.com (unknown [10.39.193.72]) by smtp.corp.redhat.com (Postfix) with ESMTPS id BE4A8112132C; Tue, 20 Jun 2023 18:19:30 +0000 (UTC) To: libc-alpha@sourceware.org Cc: siddhesh@gotplt.org, fberat@redhat.com Subject: [PATCH 05/21] stdio-common: tests: Incorrect maxlen parameter for swprintf Date: Tue, 20 Jun 2023 20:18:53 +0200 Message-ID: <20230620181910.1506893-6-fberat@redhat.com> In-Reply-To: <20230620181910.1506893-1-fberat@redhat.com> References: <20230620181910.1506893-1-fberat@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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: =?utf-8?q?Fr=C3=A9d=C3=A9ric_B=C3=A9rat_via_Libc-alpha?= From: Frederic Berat Reply-To: =?utf-8?b?RnLDqWTDqXJpYyBCw6lyYXQ=?= Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" Few tests using swprintf are passing incorrect maxlen parameter. This triggers an abort when _FORTIFY_SOURCE is enabled. Reviewed-by: Siddhesh Poyarekar --- stdio-common/tst-printf-bz25691.c | 8 ++++---- stdio-common/tst-vfprintf-mbs-prec.c | 15 ++++++++------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/stdio-common/tst-printf-bz25691.c b/stdio-common/tst-printf-bz25691.c index db81fdf590..44e9ea7d9d 100644 --- a/stdio-common/tst-printf-bz25691.c +++ b/stdio-common/tst-printf-bz25691.c @@ -78,12 +78,12 @@ do_test (void) wchar_t *result = xmalloc (resultsize); int ret; - ret = swprintf (result, resultsize, L"%.65537s", mbs); + ret = swprintf (result, mbssize, L"%.65537s", mbs); TEST_COMPARE (ret, mbssize - 1); TEST_COMPARE_BLOB (result, (ret + 1) * sizeof (wchar_t), expected, expectedsize * sizeof (wchar_t)); - ret = swprintf (result, resultsize, L"%1$.65537s", mbs); + ret = swprintf (result, mbssize, L"%1$.65537s", mbs); TEST_COMPARE (ret, mbssize - 1); TEST_COMPARE_BLOB (result, (ret + 1) * sizeof (wchar_t), expected, expectedsize * sizeof (wchar_t)); @@ -91,10 +91,10 @@ do_test (void) /* Same test, but with an invalid multibyte sequence. */ mbs[mbssize - 2] = 0xff; - ret = swprintf (result, resultsize, L"%.65537s", mbs); + ret = swprintf (result, mbssize, L"%.65537s", mbs); TEST_COMPARE (ret, -1); - ret = swprintf (result, resultsize, L"%1$.65537s", mbs); + ret = swprintf (result, mbssize, L"%1$.65537s", mbs); TEST_COMPARE (ret, -1); free (mbs); diff --git a/stdio-common/tst-vfprintf-mbs-prec.c b/stdio-common/tst-vfprintf-mbs-prec.c index 38350ed60f..41c4139f21 100644 --- a/stdio-common/tst-vfprintf-mbs-prec.c +++ b/stdio-common/tst-vfprintf-mbs-prec.c @@ -441,7 +441,8 @@ test_mbs_long (const char *mbs, const wchar_t *wide, const size_t *length) static void test_wide_long (const char *mbs, const wchar_t *wide, const size_t *length) { - wchar_t buf[2000]; +#define BUF_SIZE 2000 + wchar_t buf[BUF_SIZE]; _Static_assert (sizeof (buf) > sizeof (wchar_t) * WIDE_STRING_LENGTH, "buffer size consistent with string length"); const wchar_t *suffix = L"||TERM"; @@ -450,13 +451,13 @@ test_wide_long (const char *mbs, const wchar_t *wide, const size_t *length) /* Test formatting of the entire string. */ { - int ret = swprintf (buf, sizeof (buf), L"%s%ls", mbs, suffix); + int ret = swprintf (buf, BUF_SIZE, L"%s%ls", mbs, suffix); TEST_VERIFY (ret == WIDE_STRING_LENGTH + wcslen (suffix)); TEST_VERIFY (wmemcmp (buf, wide, WIDE_STRING_LENGTH) == 0); TEST_VERIFY (wcscmp (buf + WIDE_STRING_LENGTH, suffix) == 0); /* Left-justified string, printed in full. */ - ret = swprintf (buf, sizeof (buf), L"%-1500s%ls", mbs, suffix); + ret = swprintf (buf, BUF_SIZE, L"%-1500s%ls", mbs, suffix); TEST_VERIFY (ret == 1500 + wcslen (suffix)); TEST_VERIFY (wmemcmp (buf, wide, WIDE_STRING_LENGTH) == 0); for (size_t i = WIDE_STRING_LENGTH; i < 1500; ++i) @@ -464,7 +465,7 @@ test_wide_long (const char *mbs, const wchar_t *wide, const size_t *length) TEST_VERIFY (wcscmp (buf + 1500, suffix) == 0); /* Right-justified string, printed in full. */ - ret = swprintf (buf, sizeof (buf), L"%1500s%ls", mbs, suffix); + ret = swprintf (buf, BUF_SIZE, L"%1500s%ls", mbs, suffix); TEST_VERIFY (ret == 1500 + wcslen (suffix)); size_t padding = 1500 - WIDE_STRING_LENGTH; for (size_t i = 0; i < padding; ++i) @@ -484,14 +485,14 @@ test_wide_long (const char *mbs, const wchar_t *wide, const size_t *length) printf ("info: %s: wide_len=%d actual_wide_len=%zu\n", __func__, wide_len, actual_wide_len); - int ret = swprintf (buf, sizeof (buf), L"%.*s%ls", + int ret = swprintf (buf, BUF_SIZE, L"%.*s%ls", wide_len, mbs, suffix); TEST_VERIFY (ret == actual_wide_len + wcslen (suffix)); TEST_VERIFY (wmemcmp (buf, wide, actual_wide_len) == 0); TEST_VERIFY (wcscmp (buf + actual_wide_len, suffix) == 0); /* Left-justified string, printed in full. */ - ret = swprintf (buf, sizeof (buf), L"%-1500.*s%ls", + ret = swprintf (buf, BUF_SIZE, L"%-1500.*s%ls", wide_len, mbs, suffix); TEST_VERIFY (ret == 1500 + wcslen (suffix)); TEST_VERIFY (wmemcmp (buf, wide, actual_wide_len) == 0); @@ -500,7 +501,7 @@ test_wide_long (const char *mbs, const wchar_t *wide, const size_t *length) TEST_VERIFY (wcscmp (buf + 1500, suffix) == 0); /* Right-justified string, printed in full. */ - ret = swprintf (buf, sizeof (buf), L"%1500.*s%ls", + ret = swprintf (buf, BUF_SIZE, L"%1500.*s%ls", wide_len, mbs, suffix); TEST_VERIFY (ret == 1500 + wcslen (suffix)); size_t padding = 1500 - actual_wide_len;