From patchwork Thu Apr 12 20:52:28 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Sebor X-Patchwork-Id: 897801 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-476311-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.b="gD8+IMPb"; 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 40MY573PxQz9s0q for ; Fri, 13 Apr 2018 06:52:42 +1000 (AEST) 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=Opifd1qw+Dnz9oU7ci6YbHVFo8UDOF8KAh83d6V+GRJzmUW0kT 0GRwCgZ3avbIbHsanncL+YrCQ9g9fRFZFJtU2BJbjDXa1xzPhj54NLlLXY60EVOE HT3JajVATF1Ltw8yx+r+vWiwZ4f67g19F+x/xC5nb3+2zIBD2SXxDbqco= 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=vLT25X3FIqaMdDekMsvTCWcwsdM=; b=gD8+IMPb+wHa1AcQ81yI 8c2ETxod4LojXuaaf+XKgXiK41Bqputo9eSoySl4X1JjaWEyrRloPHjYdFqJ3UnO f6wFSKr6Ss4LcoKQ/Bwyc+6Ux3THkpFaNORZ0R5XJ6JMrdldX9cZo2kEqb2GPnwk HaLG5AKYP2S3dzRxpR/sQUg= Received: (qmail 105392 invoked by alias); 12 Apr 2018 20:52:35 -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 105371 invoked by uid 89); 12 Apr 2018 20:52:34 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-10.7 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=straightforward X-HELO: mail-ot0-f175.google.com Received: from mail-ot0-f175.google.com (HELO mail-ot0-f175.google.com) (74.125.82.175) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 12 Apr 2018 20:52:33 +0000 Received: by mail-ot0-f175.google.com with SMTP id 23-v6so7563771otj.0 for ; Thu, 12 Apr 2018 13:52:32 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version; bh=ofjliBVSKUoAjeb8e4x9KUrxNXnQ8ScZ4HORcGE7uG8=; b=Q6u/N53/ofvKzbp2WOJcUwsqtGidj2nJ0fXVRcDjHE0e4lQbVIkDjadIhuRnwBSWD1 nTmuaSpZRUt13S28hBWhFDf3BUuK+vOOaAfG4sehQJcJljO7HXpNC9GZV9Tv9/We4z7G zH8aCv8kmWPOw215biftdOI4Md7EWOqMm4po1TZs4KMqyVD+IzTWFNj0Bs/awUHG94jE EibY45UFI5H87bovo8Uio/N3xTCM8nzg2W+8mC4AsHomrkh6TMrDOaz4RYGi+gGtgiMR gNZf6SoVqZqOUQfSqtWCv1ZVu9t34+VRZREq8NTtLeC3hvMlUJe2YlCd4/KfI6HInkSE F3gw== X-Gm-Message-State: ALQs6tA5qZLkyl9Zxk3V2rggJ3Vyxdb3awP+uWnvAwNZp7Gd6Mftz33X /mFG9EQYbfuj0tuOpETSdkortw== X-Google-Smtp-Source: AIpwx4/bIzizfDGAjBXfdXVk1Rrn9+d8FSRMpo2sseOQjIkm+aNTV9fP7kW9mO+2p8id9kyokO0eFg== X-Received: by 2002:a9d:29ae:: with SMTP id n43-v6mr1862779otb.313.1523566351054; Thu, 12 Apr 2018 13:52:31 -0700 (PDT) Received: from localhost.localdomain (174-16-100-37.hlrn.qwest.net. [174.16.100.37]) by smtp.gmail.com with ESMTPSA id w8-v6sm2704513oth.19.2018.04.12.13.52.29 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 12 Apr 2018 13:52:29 -0700 (PDT) To: Gcc Patch List From: Martin Sebor Subject: [PATCH] avoid duplicate warning for strcmp with a nonstring (PR 85359) Message-ID: Date: Thu, 12 Apr 2018 14:52:28 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 X-IsSubscribed: yes The attached patch makes a small tweak to avoid issuing a duplicate warning for calls to strcmp with a nonstring argument. The most onerous part of this was figuring out how to test for the absence of duplicate warnings. The "hack" I used (dg-regexp) is in place until a more straightforward solution becomes available. (David Malcolm has something planned for GCC 9.) Martin PR middle-end/85359 - duplicate -Wstringop-overflow for a strcmp call with a nonstring pointer gcc/ChangeLog: PR middle-end/85359 * builtins.c (expand_builtin_strcmp): Take care to avoid issuing a duplicate warning. gcc/testsuite/ChangeLog: PR middle-end/85359 * gcc.dg/attr-nonstring.c: New test. Index: gcc/builtins.c =================================================================== --- gcc/builtins.c (revision 259298) +++ gcc/builtins.c (working copy) @@ -4570,14 +4570,15 @@ expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED } } - /* Check to see if the argument was declared attribute nonstring - and if so, issue a warning since at this point it's not known - to be nul-terminated. */ tree fndecl = get_callee_fndecl (exp); - maybe_warn_nonstring_arg (fndecl, exp); - if (result) { + /* Check to see if the argument was declared attribute nonstring + and if so, issue a warning since at this point it's not known + to be nul-terminated. Avoid doing this when RESULT is false + and let expand_call() do it. */ + maybe_warn_nonstring_arg (fndecl, exp); + /* Return the value in the proper mode for this function. */ machine_mode mode = TYPE_MODE (TREE_TYPE (exp)); if (GET_MODE (result) == mode) Index: gcc/testsuite/gcc.dg/attr-nonstring.c =================================================================== --- gcc/testsuite/gcc.dg/attr-nonstring.c (nonexistent) +++ gcc/testsuite/gcc.dg/attr-nonstring.c (working copy) @@ -0,0 +1,58 @@ +/* PR middle-end/85359 - duplicate -Wstringop-overflow for a strcmp call + with a nonstring pointer + { dg-do compile } + { dg-options "-O2 -Wall" } */ + +extern char* strchr (const char*, int); +extern char* strrchr (const char*, int); +extern char* stpcpy (char*, const char*); +extern char* strcpy (char*, const char*); +extern int strcmp (const char*, const char*); +extern char* strstr (const char*, const char*); + +#define NONSTRING __attribute__ ((nonstring)) + +int strcmp_nonstring_1 (NONSTRING const char *a, const char *b) +{ + /* dg-warning matches one or more instances of the warning so it's + no good on its own. Use dg-regexp instead to verify that just + one instance of the warning is issued. See gcc.dg/pr64223-1 + for a different approach. */ + return strcmp (a, b); /* { dg-regexp "\[^\n\r\]+: warning: .strcmp. argument 1 declared attribute .nonstring. \\\[-Wstringop-overflow=]" } */ +} + +int strcmp_nonstring_2 (const char *a, NONSTRING const char *b) +{ + return strcmp (a, b); /* { dg-regexp "\[^\n\r\]+: warning: .strcmp. argument 2 declared attribute .nonstring. \\\[-Wstringop-overflow=]" } */ +} + + +char* stpcpy_nonstring (char *a, NONSTRING const char *b) +{ + return stpcpy (a, b); /* { dg-regexp "\[^\n\r\]+: warning: .stpcpy. argument 2 declared attribute .nonstring. \\\[-Wstringop-overflow=]" } */ +} + +char* strchr_nonstring (NONSTRING const char *s, int c) +{ + return strchr (s, c); /* { dg-regexp "\[^\n\r\]+: warning: .strchr. argument 1 declared attribute .nonstring. \\\[-Wstringop-overflow=]" } */ +} + +char* strrchr_nonstring (NONSTRING const char *s, int c) +{ + return strrchr (s, c); /* { dg-regexp "\[^\n\r\]+: warning: .strrchr. argument 1 declared attribute .nonstring. \\\[-Wstringop-overflow=]" } */ +} + +char* strcpy_nonstring (char *a, NONSTRING const char *b) +{ + return strcpy (a, b); /* { dg-regexp "\[^\n\r\]+: warning: .strcpy. argument 2 declared attribute .nonstring. \\\[-Wstringop-overflow=]" } */ +} + +char* strstr_nonstring_1 (NONSTRING const char *a, const char *b) +{ + return strstr (a, b); /* { dg-regexp "\[^\n\r\]+: warning: .strstr. argument 1 declared attribute .nonstring. \\\[-Wstringop-overflow=]" } */ +} + +char* strstr_nonstring_2 (const char *a, NONSTRING const char *b) +{ + return strstr (a, b); /* { dg-regexp "\[^\n\r\]+: warning: .strstr. argument 2 declared attribute .nonstring. \\\[-Wstringop-overflow=]" } */ +}