From patchwork Thu Jul 6 22:47:08 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Sebor X-Patchwork-Id: 785296 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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 3x3Xtm4RsRz9ryk for ; Fri, 7 Jul 2017 08:47:26 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="GayTGIwS"; dkim-atps=neutral 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=gE7Znw2N8AN27S5IM3AjK/Nf5Bm+CzqFIoGiSMTI1ET2vgU3wb l6wlz4TdRZ5vvFNfRTpgouJAI45CzM++AwGW/FLBX/RpIJBi5Y3IfG7D6QY49pY7 EjicCkaKSU0ezlzVYcEB9FraeulnP2xZiKJ8bRVQpdfRMlQZGrqHMHAxQ= 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=nAtVoG8JHVsjzx/95+YhiBiAMX4=; b=GayTGIwSy0Wp6LiLoRnX Gxcar/pwodN9Ng1q1fjS73IU38u+cBkZ3h2KP92QRwSkBykwNAQ8cbrThmVGMlPN 3q7lyn5ktCtg2IhCQa1WVs1PYNbH3RCdYCrA1y+zawL3fn4F5y2UIA+8EF4ZXoMz GfUncT7f3geRgceyHnjqCwY= Received: (qmail 74953 invoked by alias); 6 Jul 2017 22:47:18 -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 74876 invoked by uid 89); 6 Jul 2017 22:47:14 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-24.3 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mail-qk0-f169.google.com Received: from mail-qk0-f169.google.com (HELO mail-qk0-f169.google.com) (209.85.220.169) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 06 Jul 2017 22:47:12 +0000 Received: by mail-qk0-f169.google.com with SMTP id p21so14715332qke.3 for ; Thu, 06 Jul 2017 15:47:12 -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=QBk9gPVJhBb4rgxGEj8foF1RTFMe9u0l8UFTs7aJLNo=; b=uP++3aLRiIWMNFdLv4lqr2ToUECduyz8F0iZFZrp+FsVCFCssawfjlVS6+MwA02/qj s7WTsSeSCEb9Nz2yLlOohgt31GhOmIiB16mo0nhxIXCKa2RhZgC25GdXej5XUxFsu3DT 5mOjuV+tCRe+H5WYg+a7OmyeJosgyRsc0kEYGmoUe0Zze0qjK2vjvuSCTf82A28joCqb NHvgKC7n7X+DEDKXCP3zgs3wmfmGWO9pssOJh9Yst2NiDyWshvlygQjeYF0yyXMo1DIN ztDBkr2JcvsHFVNRuur16xTgS1wyjKY54Hxg1vMa+fhUeY769NO4iJuadh0BTq5nY42J r4DA== X-Gm-Message-State: AKS2vOwYVplH2ebIdLWlXca3B3asQGk5WrXHhPNk3sLTf1lyc6oGAIwQ Z/8+WkPA+y4j6UnA X-Received: by 10.55.42.203 with SMTP id q72mr62777719qkq.12.1499381230790; Thu, 06 Jul 2017 15:47:10 -0700 (PDT) Received: from localhost.localdomain (75-171-229-159.hlrn.qwest.net. [75.171.229.159]) by smtp.gmail.com with ESMTPSA id j17sm1153699qta.49.2017.07.06.15.47.10 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 06 Jul 2017 15:47:10 -0700 (PDT) To: Gcc Patch List From: Martin Sebor Subject: [PATCH] prevent -Wall from resetting -Wstringop-overflow=2 to 1 (pr 81345) Message-ID: <811c1f62-3baa-3a66-7e98-f3fa41f38753@gmail.com> Date: Thu, 6 Jul 2017 16:47:08 -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 -Wstringop-overflow option defaults to 2 (for Object Size Checking type 1). But when -Wall is used it resets the default value to 1. This happens because when I added the option to c.opt I assumed it would default to, well, the default value set by the Init() directive regardless of whether or not -Wall was used. The attached patch explicitly specifies the defaults to correct this. Btw., I think this behavior is too surprising to be correct or (I hope) even intended for options with arguments. -Wstringop- overflow is specified like this: C ObjC C++ ObjC++ Joined RejectNegative UInteger Var(warn_stringop_overflow) Init(2) Warning LangEnabledBy(C ObjC C++ ObjC++, Wall) IntegerRange(0, 4) with the LangEnabledBy form used above documented like this: LangEnabledBy(language, opt) When compiling for the given language, the option is set to the value of -opt, IMO, it makes little sense for an option that takes an argument and that specifies a binary option like -Wall in LangEnabledBy to default to the binary value of the latter option. I think it would be more intuitive and convenient for it to default to the value set by its Init directive for the positive form of the binary option and to zero for the negative form (or to empty for strings, if that's ever done). Martin PR other/81345 - -Wall resets -Wstringop-overflow to 1 from the default 2 gcc/c-family/ChangeLog: PR other/81345 * c.opt (-Wstringop-overflow): Set defaults in LangEnabledBy. gcc/testsuite/ChangeLog: PR other/81345 * gcc.dg/pr81345.c: New test. diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index 05766c4..e0ad3ab 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -732,7 +732,7 @@ Warn about buffer overflow in string manipulation functions like memcpy and strcpy. Wstringop-overflow= -C ObjC C++ ObjC++ Joined RejectNegative UInteger Var(warn_stringop_overflow) Init(2) Warning LangEnabledBy(C ObjC C++ ObjC++, Wall) IntegerRange(0, 4) +C ObjC C++ ObjC++ Joined RejectNegative UInteger Var(warn_stringop_overflow) Init(2) Warning LangEnabledBy(C ObjC C++ ObjC++, Wall, 2, 0) IntegerRange(0, 4) Under the control of Object Size type, warn about buffer overflow in string manipulation functions like memcpy and strcpy. diff --git a/gcc/testsuite/gcc.dg/pr81345.c b/gcc/testsuite/gcc.dg/pr81345.c new file mode 100644 index 0000000..c2cbad7 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr81345.c @@ -0,0 +1,17 @@ +/* PR other/81345 - -Wall resets -Wstringop-overflow to 1 from the default 2 + { dg-do compile } + { dg-options "-O2 -Wall" } */ + +char a[3]; + +void f (const char *s) +{ + __builtin_strncpy (a, s, sizeof a + 1); /* { dg-warning "\\\[-Wstringop-overflow=]" } */ +} + +struct S { char a[3]; int i; }; + +void g (struct S *d, const char *s) +{ + __builtin_strncpy (d->a, s, sizeof d->a + 1); /* { dg-warning "\\\[-Wstringop-overflow=]" } */ +}