From patchwork Wed Jan 22 14:59:19 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Sebor X-Patchwork-Id: 1227311 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-518029-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha1 header.s=default header.b=Z8bwm4nw; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=CsvW6/u7; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 482pTn2TKnz9sPn for ; Thu, 23 Jan 2020 01:59:39 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; q=dns; s=default; b=CyP4QGj0iu/7kZ+JKhRqmTb4Rn4s+KuNunfCTiwTRQJ/CNNrup Qbruhd0UYm/htVYjQ1KMbSGHajJMDWdKoJuqQawdbeidpALqREq6tTFXOk6ym63R F6dp+zozrwY3f40VeYQmrQSTeLouCoWV+G60C6Op01nJ//t2xL5dIrK4A= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; s= default; bh=cLR9ZiZxI7EyWnrUOMsgoDZAlCQ=; b=Z8bwm4nwit9OnlDg7p4T ldLzo0U3Rmr1qG4wNQI7KIzUNL1Iv6L9fG31k1djI1xQZLTfCawx5MbE7FhK5DbS IwNy1lDHpxqaY6+qHVOjmZnQXGW/NcF5WPdYMdPKm5g9uuYbqNlk/bkIHBowtaSQ rS5X6EFyJxNhfzd/XH1o3+Y= Received: (qmail 77966 invoked by alias); 22 Jan 2020 14:59:31 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 77957 invoked by uid 89); 22 Jan 2020 14:59:30 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, FREEMAIL_FROM, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=H*r:sk:dynamic X-HELO: mail-wr1-f54.google.com Received: from mail-wr1-f54.google.com (HELO mail-wr1-f54.google.com) (209.85.221.54) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 22 Jan 2020 14:59:28 +0000 Received: by mail-wr1-f54.google.com with SMTP id j42so7566907wrj.12 for ; Wed, 22 Jan 2020 06:59:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=to:from:subject:message-id:date:user-agent:mime-version :content-language; bh=1GXiZEFvB4P3TWuQwmdVXjFKXtOqPe4uoll0pdF2n44=; b=CsvW6/u7eClWWmE4pT1Xcn/IkuPdq5rU8gKbkZtU7ThPdDknpUynm9pjTK7NYOwnSD /iRalb6dhBgQF85fdLz08pF7U67LUjGe6EVKoq/nfZ0s5Cf610KpyB8WTVHV7N9pt1Ny 4ynz3D4SEd03DZLV0GoZGEcjPfDSNQr0X/7waYrGayjYXcReRobyzYRagS0rFia4PG6e mguv5A+1BoFJ1ftWa4RV1ZZXNYjaNQcpwR91sd4DdbPhdx1nO8wbLBwtf01vp/6yYtBB YyaoSyxnHxHCP27NFYTEY3U58FHMf3msfot153dqg3RINZKwkSEaPXUjm4+rcBxxrDUS rsMg== Received: from [10.0.0.34] (dynamic-194-228-13-172.ipv4.broadband.iol.cz. [194.228.13.172]) by smtp.gmail.com with ESMTPSA id i5sm4399126wml.31.2020.01.22.06.59.25 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 22 Jan 2020 06:59:25 -0800 (PST) To: gcc-patches@gcc.gnu.org From: Martin Sebor Subject: [PATCH] avoid -Wrestrict on sprintf %p with destination as argument (PR 84919) Message-ID: <29df4b53-dfc4-eb1c-3489-97a2dbcdf442@gmail.com> Date: Wed, 22 Jan 2020 15:59:19 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.2.2 MIME-Version: 1.0 X-IsSubscribed: yes The early front-end only implementation of -Wrestrict that's still present in GCC 10 issues a false postive for %p arguments that are the same as the destination. Bug 84919 reports an instance of this false positive in the Linux kernel. That attached patch suppresses the front-end warning for the sprintf family of functions, letting the sprintf pass that was in GCC 10 extended to also handle -Wrestrict for these functions, handle them instead. Tested on x86_64-linux. Since this is a regression I'd like to commit the fix to GCC 10. Martin PR c/84919 - bogus -Wrestrict on sprintf %p with destination as argument gcc/c-family/ChangeLog: PR c/84919 * c-common.c (check_function_arguments): Avoid overlap checking of sprintf functions. gcc/testsuite/ChangeLog: PR c/84919 * gcc.dg/Wrestrict-20.c: New test. diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index 4a7f3a52335..4bb21772c64 100644 --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -5732,8 +5732,26 @@ check_function_arguments (location_t loc, const_tree fndecl, const_tree fntype, if (warn_format) check_function_sentinel (fntype, nargs, argarray); - if (warn_restrict) - warned_p |= check_function_restrict (fndecl, fntype, nargs, argarray); + if (fndecl && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)) + { + switch (DECL_FUNCTION_CODE (fndecl)) + { + case BUILT_IN_SPRINTF: + case BUILT_IN_SPRINTF_CHK: + case BUILT_IN_SNPRINTF: + case BUILT_IN_SNPRINTF_CHK: + /* Let the sprintf pass handle these. */ + return warned_p; + + default: + break; + } + } + + /* check_function_restrict sets the DECL_READ_P for arguments + so it must be called unconditionally. */ + warned_p |= check_function_restrict (fndecl, fntype, nargs, argarray); + return warned_p; } diff --git a/gcc/testsuite/gcc.dg/Wrestrict-20.c b/gcc/testsuite/gcc.dg/Wrestrict-20.c new file mode 100644 index 00000000000..9826e7f4503 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wrestrict-20.c @@ -0,0 +1,41 @@ +/* PR c/84919 - bogus -Wrestrict on sprintf %p with destination as argument + { dg-do compile } + -O2 isn't strictly necessary but setting also verifies that the sprintf/ + strlen pass doesn't warn with non-constant arguments. + { dg-options "-O2 -Wall" } */ + +extern int sprintf (char* restrict, const char* restrict, ...); +extern int snprintf (char* restrict, __SIZE_TYPE__, const char* restrict, ...); + +char a[32]; + +void test_warn (char *p) +{ + a[0] = 0; + sprintf (a, "a=%s", a); /* { dg-warning "-Wrestrict" } */ + + p = a; + char *q = p + 1; + sprintf (p, "a=%s", q); /* { dg-warning "-Wrestrict" } */ +} + +void test_nowarn_front_end (char *d) +{ + sprintf (d, "%p", d); + snprintf (d, 32, "%p", d); + + sprintf (a, "p=%p", a); + snprintf (a, sizeof a, "%p", a); +} + +void test_nowarn_sprintf_pass (char *d) +{ + char *q = d; + + sprintf (d, "p=%p", q); + snprintf (d, 32, "p=%p", q); + + q = a; + sprintf (a, "a=%p", q); + snprintf (a, sizeof a, "a=%p", q); +}