From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qk1-x72b.google.com (mail-qk1-x72b.google.com [IPv6:2607:f8b0:4864:20::72b]) by sourceware.org (Postfix) with ESMTPS id 8AC8C385782C for ; Tue, 4 May 2021 22:15:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 8AC8C385782C Received: by mail-qk1-x72b.google.com with SMTP id o5so10309594qkb.0 for ; Tue, 04 May 2021 15:15:47 -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:content-language; bh=kzMVcmoiBnLV0vXx/uDN9Z2b2n7qcTxgFW/rwgqQDuU=; b=kU91g74u4RY4ezhqmtZCM7x126y0B3G/MYbINa/RowVWOWI58yqC6uFXZc8eZ7+Jra PxFGbhSrlqmm0019d3LCY9vRstCxhvFhn3nKkPjbfeQiKu8o2xsR1WM6z6piFstvRveb r5KyU5e4UpMyLhnMjUfFivhLjFwz4t94iCT/Y4uasdpAKrD2X2ewW7gVZ78q4+cQEpFl ZWGCNIHXEj8KX3lLWeBNcevnkjsRq9NiEnf/E8OVsxERig8Sbl8NkYfXa8CDKmpRJIYw wkDNAT2kP6Ul1l352mAmd7rAELP/VBH/EOEUc1nhqSEndQ5Cn9ZA+HEN6yb0mQ5vdtDC c3pA== X-Gm-Message-State: AOAM533DH+vkjI/k3KMjdEFJ6yypgwzqEWmHpf3UrBoNahfq94WrsRlG d0OHVSuFZi/Vqt4C7I2TvpPyqcghvH0= X-Google-Smtp-Source: ABdhPJx6ONRKazah2OVtcOcFCx88Yqha3EVEd3eogm0yav5yaIu19pOLI8Itzq9l+cV6p41WJUcPGg== X-Received: by 2002:a37:789:: with SMTP id 131mr17001482qkh.130.1620166546956; Tue, 04 May 2021 15:15:46 -0700 (PDT) Received: from [192.168.0.41] (71-218-14-121.hlrn.qwest.net. [71.218.14.121]) by smtp.gmail.com with ESMTPSA id c2sm12206936qkk.2.2021.05.04.15.15.46 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 04 May 2021 15:15:46 -0700 (PDT) To: gcc-patches From: Martin Sebor Subject: [PATCH] run early sprintf warning after SSA (PR 100325) Message-ID: Date: Tue, 4 May 2021 16:15:45 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.2.2 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------7A15C0E6F835A5449479D18C" Content-Language: en-US X-Spam-Status: No, score=-10.3 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 autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 04 May 2021 22:15:50 -0000 This is a multi-part message in MIME format. --------------7A15C0E6F835A5449479D18C Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit With no optimization, -Wformat-overflow and -Wformat-truncation runs early to detect a subset of simple bugs. But as it turns out, the pass runs just a tad too early, before SSA. That causes it to miss a class of problems that can easily be detected once code is in SSA form, and I would expect might also cause false positives. The attached change moves the sprintf pass just after pass_build_ssa, similar to other early flow-sensitive warnings (-Wnonnull-compare and -Wuninitialized). Martin --------------7A15C0E6F835A5449479D18C Content-Type: text/x-patch; charset=UTF-8; name="gcc-100325.diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="gcc-100325.diff" PR middle-end/100325 - missing warning with -O0 on sprintf overflow with pointer plus offset gcc/ChangeLog: * passes.def (pass_warn_printf): Run after SSA. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/builtin-sprintf-warn-26.c: New test. diff --git a/gcc/passes.def b/gcc/passes.def index 55e8164d56b..de39fa48b3d 100644 --- a/gcc/passes.def +++ b/gcc/passes.def @@ -45,7 +45,6 @@ along with GCC; see the file COPYING3. If not see NEXT_PASS (pass_warn_function_return); NEXT_PASS (pass_coroutine_early_expand_ifns); NEXT_PASS (pass_expand_omp); - NEXT_PASS (pass_warn_printf); NEXT_PASS (pass_walloca, /*strict_mode_p=*/true); NEXT_PASS (pass_build_cgraph_edges); TERMINATE_PASS_LIST (all_lowering_passes) @@ -58,6 +57,7 @@ along with GCC; see the file COPYING3. If not see PUSH_INSERT_PASSES_WITHIN (pass_build_ssa_passes) NEXT_PASS (pass_fixup_cfg); NEXT_PASS (pass_build_ssa); + NEXT_PASS (pass_warn_printf); NEXT_PASS (pass_warn_nonnull_compare); NEXT_PASS (pass_early_warn_uninitialized); NEXT_PASS (pass_ubsan); diff --git a/gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-warn-26.c b/gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-warn-26.c index 16a551d9c8d..677b6345c5c 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-warn-26.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-warn-26.c @@ -22,17 +22,17 @@ void nowarn_4m3 () void warn_2m1 () { char *p = a + 2; - sprintf (p - 1, "%i", 123); // { dg-warning "-Wformat-overflow" "pr100325" { xfail *-*-* } } + sprintf (p - 1, "%i", 123); // { dg-warning "-Wformat-overflow" "pr100325" } } void warn_3m1 () { char *p = a + 3; - sprintf (p - 1, "%i", 12); // { dg-warning "-Wformat-overflow" "pr100325" { xfail *-*-* } } + sprintf (p - 1, "%i", 12); // { dg-warning "-Wformat-overflow" "pr100325" } } void warn_4m1 () { char *p = a + 4; - sprintf (p - 1, "%i", 1); // { dg-warning "-Wformat-overflow" "pr100325" { xfail *-*-* } } + sprintf (p - 1, "%i", 1); // { dg-warning "-Wformat-overflow" "pr100325" } } --------------7A15C0E6F835A5449479D18C--