From patchwork Tue Jun 21 16:18:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Noah Goldstein X-Patchwork-Id: 1646144 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.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=m7nq1nd4; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LSBZ718jDz9s09 for ; Wed, 22 Jun 2022 02:20:15 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7DD2B38582B3 for ; Tue, 21 Jun 2022 16:20:12 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7DD2B38582B3 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1655828412; bh=dg1/kMI4HV3p/v6WI/NSJpPGKtgrN+st+hQXHLZMKDE=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=m7nq1nd43eBlhjShvNxc5+dzaB/px8DkBw2mrkMUS9JiDFIGWWF6W8IvHrQ+s780/ qKpg1EoLUJEaSO1RtrT6ctSq0z8O0FZK11AjmTy5HFxewjBgUH4ujwIK/hTsonIij7 gm0ZYsp7/gxYwvaVffwkgq94v84aQm68hnbnL2Rc= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by sourceware.org (Postfix) with ESMTPS id 240053858C51 for ; Tue, 21 Jun 2022 16:19:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 240053858C51 Received: by mail-pf1-x431.google.com with SMTP id d17so4574885pfq.9 for ; Tue, 21 Jun 2022 09:19:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=dg1/kMI4HV3p/v6WI/NSJpPGKtgrN+st+hQXHLZMKDE=; b=VZ3UQcftVggKixJgaqiGZKe6WoMHnerOKW7fddO21uyFm5leP9ag8syiPTSpicf2Od aVsuHb9uDVeMhApdxlF1mYXxlfpAKRokcZ00k7JdZAeBoI5p4fXYbNNd+XnGkpu6ocT5 FTPUIQYaD070lbzDaLbRclGhBJxDz6wTdAnOORft/uhLDZY9Ycxzu71I0NgU7U/aN8PB bQSM8droEIScHePgh2Cs6dCKulamrTWfGlC5zUCA7KKQ07bcME3nJzFMeAPVpMzvHMb9 W8YZ1mIe5FwL8yOpyO8MY3jyBowpOBJKaLYra2sD/F5/4OkbmGVGb4QHXL32dUkgF7kP EE3A== X-Gm-Message-State: AJIora9lUi7exF+9v8iHVq+ZbuRexHnMWe44TvtGX1OHTAAMS5+GtwHX ZS0R2y8tEcIjEYfSVA+0+4gQXkUV7nI= X-Google-Smtp-Source: AGRyM1vEtLgxdT80u9xBjhJG2e2h3FcFYNLpaox/to5ft7xRDb0v5L7CqLzmXM+ChFwgJYEui8Ro3g== X-Received: by 2002:a05:6a00:16c7:b0:520:6ede:2539 with SMTP id l7-20020a056a0016c700b005206ede2539mr30527991pfc.46.1655828395996; Tue, 21 Jun 2022 09:19:55 -0700 (PDT) Received: from noah-tgl.. ([192.55.60.47]) by smtp.gmail.com with ESMTPSA id c18-20020a170902b69200b00168eb15f4c1sm10920754pls.210.2022.06.21.09.19.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Jun 2022 09:19:55 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v1] stdlib: Remove attr_write from mbstows if dst is NULL [BZ: 29265] Date: Tue, 21 Jun 2022 09:18:23 -0700 Message-Id: <20220621161821.2940071-1-goldstein.w.n@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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: Noah Goldstein via Libc-alpha From: Noah Goldstein Reply-To: Noah Goldstein Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" mbstows is defined if dst is NULL and is defined to special cased if dst is NULL so the fortify objsize check if incorrect in that case. Tested on x86-64 linux. --- Note. I wasn't able to get the test to actually throw an error before the change. stdlib/Makefile | 3 +++ stdlib/bits/stdlib.h | 16 +++++++++++----- stdlib/testmb.c | 7 +++++++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/stdlib/Makefile b/stdlib/Makefile index 60fc59c12c..6ef725ef74 100644 --- a/stdlib/Makefile +++ b/stdlib/Makefile @@ -373,6 +373,9 @@ CFLAGS-tst-qsort.c += $(stack-align-test-flags) CFLAGS-tst-makecontext.c += -funwind-tables CFLAGS-tst-makecontext2.c += $(stack-align-test-flags) +CFLAGS-testmb.c += -D_FORTIFY_SOURCE=2 -Wall -Werror + + # Run a test on the header files we use. tests-special += $(objpfx)isomac.out diff --git a/stdlib/bits/stdlib.h b/stdlib/bits/stdlib.h index 277d099e22..9ab66db6a4 100644 --- a/stdlib/bits/stdlib.h +++ b/stdlib/bits/stdlib.h @@ -96,6 +96,11 @@ extern size_t __mbstowcs_chk (wchar_t *__restrict __dst, const char *__restrict __src, size_t __len, size_t __dstlen) __THROW __attr_access ((__write_only__, 1, 3)) __attr_access ((__read_only__, 2)); +extern size_t __REDIRECT_NTH (__mbstowcs_chk_nulldst, + (wchar_t *__restrict __dst, + const char *__restrict __src, + size_t __len), mbstowcs_chk) + __attr_access ((__read_only__, 2)); extern size_t __REDIRECT_NTH (__mbstowcs_alias, (wchar_t *__restrict __dst, const char *__restrict __src, @@ -108,16 +113,17 @@ extern size_t __REDIRECT_NTH (__mbstowcs_chk_warn, __warnattr ("mbstowcs called with dst buffer smaller than len " "* sizeof (wchar_t)"); -__fortify_function size_t +__always_inline __fortify_function size_t __NTH (mbstowcs (wchar_t *__restrict __dst, const char *__restrict __src, size_t __len)) { - return __glibc_fortify_n (mbstowcs, __len, sizeof (wchar_t), - __glibc_objsize (__dst), - __dst, __src, __len); + if (__builtin_constant_p (__dst) && __dst == NULL) + return __mbstowcs_chk_nulldst (__dst, __src, __len); + else + return __glibc_fortify_n (mbstowcs, __len, sizeof (wchar_t), + __glibc_objsize (__dst), __dst, __src, __len); } - extern size_t __wcstombs_chk (char *__restrict __dst, const wchar_t *__restrict __src, size_t __len, size_t __dstlen) __THROW diff --git a/stdlib/testmb.c b/stdlib/testmb.c index 45dae7db61..6ac4dfd21d 100644 --- a/stdlib/testmb.c +++ b/stdlib/testmb.c @@ -16,6 +16,13 @@ main (int argc, char *argv[]) lose = 1; } + i = mbstowcs (NULL, "bar", 4); + if (!(i == 3 && w[1] == 'a')) + { + puts ("mbstowcs FAILED2!"); + lose = 1; + } + mbstowcs (w, "blah", 5); i = wcstombs (c, w, 10); if (i != 4)