From patchwork Mon Mar 13 19:00:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 66312 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 96A8A3857838 for ; Mon, 13 Mar 2023 19:01:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 96A8A3857838 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1678734062; bh=/3WiEzmsgb0vNByxX1DD3TXKa462N7N8Vb1tAK5yenU=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=RmDyaY0bXh3VdfP8BFiIukKq7MoQwQ9Z97SDdxb7JoDmoU2VpcIOaBHsEf7/ol/Jx S3SCq890TRAj1k5gLTVa4eSZyvzN9saJwDixYcuO4Dbb6ZQqMXrGr5pAm3uC7NVAc7 cmvJX79MuhYjzyFQv+9WosZH2nHDrlQX5xEFTgyA= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 A23E73858C1F for ; Mon, 13 Mar 2023 19:00:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A23E73858C1F 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-362-6_kHWfAjPyWnK1BUPNyQFw-1; Mon, 13 Mar 2023 15:00:33 -0400 X-MC-Unique: 6_kHWfAjPyWnK1BUPNyQFw-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B4B5B803513 for ; Mon, 13 Mar 2023 19:00:32 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.2.17.70]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8C7F5C017D7; Mon, 13 Mar 2023 19:00:32 +0000 (UTC) To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [pushed] testsuite: add test coverage for PR analyzer/108045 Date: Mon, 13 Mar 2023 15:00:31 -0400 Message-Id: <20230313190031.3358543-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_NUMSUBJECT, 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 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-Patchwork-Original-From: David Malcolm via Gcc-patches From: David Malcolm Reply-To: David Malcolm Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" PR analyzer/108045 reports a leak false positive from -fanalyzer. The false +ve as reported seems to have been fixed by r13-6589-g14f5e56a8a766c, but with -fanalyzer-call-summaries there are two additional false +ves which aren't fixed by that commit. This patch adds test coverage for these cases. Successfully regrtested on x86_64-pc-linux-gnu. Pushed to trunk as r13-6640-g129664c67b7745. gcc/testsuite/ChangeLog: PR analyzer/108045 * gcc.dg/analyzer/leak-pr108045-with-call-summaries.c: New test. * gcc.dg/analyzer/leak-pr108045-without-call-summaries.c: New test. Signed-off-by: David Malcolm --- .../leak-pr108045-with-call-summaries.c | 106 +++++++++++++++++ .../leak-pr108045-without-call-summaries.c | 107 ++++++++++++++++++ 2 files changed, 213 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/analyzer/leak-pr108045-with-call-summaries.c create mode 100644 gcc/testsuite/gcc.dg/analyzer/leak-pr108045-without-call-summaries.c diff --git a/gcc/testsuite/gcc.dg/analyzer/leak-pr108045-with-call-summaries.c b/gcc/testsuite/gcc.dg/analyzer/leak-pr108045-with-call-summaries.c new file mode 100644 index 00000000000..d63be06a9bb --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/leak-pr108045-with-call-summaries.c @@ -0,0 +1,106 @@ +/* { dg-additional-options "-fanalyzer-call-summaries" } */ + +typedef __SIZE_TYPE__ size_t; +#define NULL ((void *)0) + +/* data structures */ + +struct screen_s { + size_t rows; + size_t cols; + char **data; +}; + +struct context_s { + struct screen_s *scr; +}; + +/* global context variable */ + +static struct context_s *ctx; + +/* prototypes */ + +struct screen_s *screen_create(size_t cols, size_t rows); +void screen_destroy(struct screen_s *scr); +void resize_screen(size_t cols, size_t rows); + +/* functions */ + +struct screen_s *screen_create(size_t cols, size_t rows) +{ + struct screen_s *result = NULL; + + result = __builtin_calloc(1, sizeof(*result)); + if (!result) + return NULL; + + result->cols = cols; + result->rows = rows; + + /* make one allocation which will be accessed like a 2D array */ + result->data = __builtin_calloc(rows, sizeof(result->data) + sizeof(*result->data) * cols); + if (!result->data) { + __builtin_free(result); + return NULL; + } + + /* obtain pointer to start of data area */ + char *ptr = (char *)(result->data + rows); + + /* setup pointers for each row of data to allow 2D array access */ + for (size_t row = 0; row < rows; row++) + result->data[row] = (ptr + row * cols); + /* array can now be accessed like data[row][col] */ + + return result; +} + +void screen_destroy(struct screen_s *scr) +{ + if (!scr) + return; + + __builtin_free(scr->data); + + scr->data = NULL; + scr->rows = 0; + scr->cols = 0; + + __builtin_free(scr); +} + +void resize_screen(size_t cols, size_t rows) +{ + /* create a new screen */ + struct screen_s *new_scr = NULL; + new_scr = screen_create(cols, rows); /* { dg-bogus "leak" "PR 108045" { xfail *-*-* } } */ + if (!new_scr) { + return; + } + + /* swap the old screen with the new one */ + struct screen_s *old_scr = ctx->scr; + ctx->scr = new_scr; + + /* omitted: copy the old screen contents to the new screen */ + + /* free the old screen */ + screen_destroy(old_scr); +} + +int main(void) +{ + ctx = __builtin_calloc(1, sizeof(*ctx)); + if (!ctx) + __builtin_abort(); + + ctx->scr = screen_create(80, 25); /* { dg-bogus "leak" "PR 108045" { xfail *-*-* } } */ + resize_screen(100, 20); + + /* tidy up and quit */ + screen_destroy(ctx->scr); + __builtin_free(ctx); + ctx = NULL; + return 0; +} diff --git a/gcc/testsuite/gcc.dg/analyzer/leak-pr108045-without-call-summaries.c b/gcc/testsuite/gcc.dg/analyzer/leak-pr108045-without-call-summaries.c new file mode 100644 index 00000000000..ae7a7d5c159 --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/leak-pr108045-without-call-summaries.c @@ -0,0 +1,107 @@ +/* { dg-additional-options "-fno-analyzer-call-summaries" } */ +/* { dg-additional-options "-Wno-analyzer-too-complex" } */ + +typedef __SIZE_TYPE__ size_t; +#define NULL ((void *)0) + +/* data structures */ + +struct screen_s { + size_t rows; + size_t cols; + char **data; +}; + +struct context_s { + struct screen_s *scr; +}; + +/* global context variable */ + +static struct context_s *ctx; + +/* prototypes */ + +struct screen_s *screen_create(size_t cols, size_t rows); +void screen_destroy(struct screen_s *scr); +void resize_screen(size_t cols, size_t rows); + +/* functions */ + +struct screen_s *screen_create(size_t cols, size_t rows) +{ + struct screen_s *result = NULL; + + result = __builtin_calloc(1, sizeof(*result)); + if (!result) + return NULL; + + result->cols = cols; + result->rows = rows; + + /* make one allocation which will be accessed like a 2D array */ + result->data = __builtin_calloc(rows, sizeof(result->data) + sizeof(*result->data) * cols); + if (!result->data) { + __builtin_free(result); + return NULL; + } + + /* obtain pointer to start of data area */ + char *ptr = (char *)(result->data + rows); + + /* setup pointers for each row of data to allow 2D array access */ + for (size_t row = 0; row < rows; row++) + result->data[row] = (ptr + row * cols); + /* array can now be accessed like data[row][col] */ + + return result; +} + +void screen_destroy(struct screen_s *scr) +{ + if (!scr) + return; + + __builtin_free(scr->data); + + scr->data = NULL; + scr->rows = 0; + scr->cols = 0; + + __builtin_free(scr); /* { dg-bogus "leak" } */ +} + +void resize_screen(size_t cols, size_t rows) +{ + /* create a new screen */ + struct screen_s *new_scr = NULL; + new_scr = screen_create(cols, rows); /* { dg-bogus "leak" } */ + if (!new_scr) { + return; + } + + /* swap the old screen with the new one */ + struct screen_s *old_scr = ctx->scr; + ctx->scr = new_scr; + + /* omitted: copy the old screen contents to the new screen */ + + /* free the old screen */ + screen_destroy(old_scr); +} + +int main(void) +{ + ctx = __builtin_calloc(1, sizeof(*ctx)); + if (!ctx) + __builtin_abort(); + + ctx->scr = screen_create(80, 25); /* { dg-bogus "leak" } */ + resize_screen(100, 20); + + /* tidy up and quit */ + screen_destroy(ctx->scr); + __builtin_free(ctx); + ctx = NULL; + return 0; +}