From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 7230B3858281 for ; Wed, 1 Feb 2023 02:26:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7230B3858281 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1675218379; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=5vy981XqLx6C+MCASN1kNgNM7rhP0ulu8l0rHQIvpWA=; b=XR/xCIRBZDD+rhCy8V3471xc75WJgB+gAV8VGypOlFsg0JfgPJAwr+4QQqKLXpSITFF/+r z2z8cDuLBud7HI/f2KorYbd439eplMm1xAN5Gb9/LQwEcTOFiKHYvuO6Uwv602z80173LX JUnw8G4ONILkZQCvjYfYx+quctdqlTk= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-544-7oC5E8DrP--6QJPnlCCh3g-1; Tue, 31 Jan 2023 21:26:17 -0500 X-MC-Unique: 7oC5E8DrP--6QJPnlCCh3g-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id EE9D5886C62 for ; Wed, 1 Feb 2023 02:26:16 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.2.17.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id C1CB2112132C; Wed, 1 Feb 2023 02:26:16 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [pushed] analyzer: fix -Wanalyzer-allocation-size false -ve on alloca [PR108616] Date: Tue, 31 Jan 2023 21:26:15 -0500 Message-Id: <20230201022615.824902-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Pushed to trunk as r13-5614-g70d34f2a30a5f1. gcc/analyzer/ChangeLog: PR analyzer/108616 * pending-diagnostic.cc (fixup_location_in_macro_p): Add "alloca" to macros that we shouldn't unwind inside. gcc/testsuite/ChangeLog: PR analyzer/108616 * gcc.dg/analyzer/allocation-size-multiline-3.c: New test. * gcc.dg/analyzer/test-alloca.h: New test. Signed-off-by: David Malcolm --- gcc/analyzer/pending-diagnostic.cc | 6 +++ .../analyzer/allocation-size-multiline-3.c | 44 +++++++++++++++++++ gcc/testsuite/gcc.dg/analyzer/test-alloca.h | 3 ++ 3 files changed, 53 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/analyzer/allocation-size-multiline-3.c create mode 100644 gcc/testsuite/gcc.dg/analyzer/test-alloca.h diff --git a/gcc/analyzer/pending-diagnostic.cc b/gcc/analyzer/pending-diagnostic.cc index 79e6c5528eb..e36ed4fd9c1 100644 --- a/gcc/analyzer/pending-diagnostic.cc +++ b/gcc/analyzer/pending-diagnostic.cc @@ -139,6 +139,12 @@ static bool fixup_location_in_macro_p (cpp_hashnode *macro) { ht_identifier ident = macro->ident; + + /* Don't unwind inside "alloca" macro, so that we don't suppress warnings + from it (due to being in system headers). */ + if (ht_ident_eq (ident, "alloca")) + return true; + /* Don't unwind inside macros, so that we don't suppress warnings from them (due to being in system headers). */ if (ht_ident_eq (ident, "va_start") diff --git a/gcc/testsuite/gcc.dg/analyzer/allocation-size-multiline-3.c b/gcc/testsuite/gcc.dg/analyzer/allocation-size-multiline-3.c new file mode 100644 index 00000000000..e27364a8e83 --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/allocation-size-multiline-3.c @@ -0,0 +1,44 @@ +/* Verify that we warn for incorrect uses of "alloca" (which may be in a + macro in a system header), and that the output looks correct. */ + +/* { dg-additional-options "-fdiagnostics-path-format=inline-events -fdiagnostics-show-caret -fanalyzer-fine-grained" } */ +/* { dg-require-effective-target alloca } */ + +#include +#include "test-alloca.h" + +void test_constant_99 (void) +{ + int32_t *ptr = alloca (99); /* { dg-warning "allocated buffer size is not a multiple of the pointee's size" } */ +} + +/* { dg-begin-multiline-output "" } + int32_t *ptr = alloca (99); + ^~~~~~ + 'test_constant_99': events 1-2 + | + | int32_t *ptr = alloca (99); + | ^~~~~~ + | | + | (1) allocated 99 bytes here + | (2) assigned to 'int32_t *' {aka 'int *'} here; 'sizeof (int32_t {aka int})' is '4' + | + { dg-end-multiline-output "" } */ + +void test_symbolic (int n) +{ + int32_t *ptr = alloca (n * 2); /* { dg-warning "allocated buffer size is not a multiple of the pointee's size" } */ +} + +/* { dg-begin-multiline-output "" } + int32_t *ptr = alloca (n * 2); + ^~~~~~ + 'test_symbolic': events 1-2 + | + | int32_t *ptr = alloca (n * 2); + | ^~~~~~ + | | + | (1) allocated 'n * 2' bytes here + | (2) assigned to 'int32_t *' {aka 'int *'} here; 'sizeof (int32_t {aka int})' is '4' + | + { dg-end-multiline-output "" } */ diff --git a/gcc/testsuite/gcc.dg/analyzer/test-alloca.h b/gcc/testsuite/gcc.dg/analyzer/test-alloca.h new file mode 100644 index 00000000000..f4045f92adc --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/test-alloca.h @@ -0,0 +1,3 @@ +#pragma GCC system_header + +#define alloca(X) __builtin_alloca(X) -- 2.26.3