From patchwork Fri Sep 9 21:24:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 1125 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5044:0:0:0:0:0 with SMTP id h4csp901286wrt; Fri, 9 Sep 2022 14:24:53 -0700 (PDT) X-Google-Smtp-Source: AA6agR4szUFZMnVExj6MCI6gDXmaj3hU61xiQAlYUGvqfafBvQqNkPARU23F/HotK3Zvsml6D+8N X-Received: by 2002:a17:906:dc89:b0:731:67eb:b60b with SMTP id cs9-20020a170906dc8900b0073167ebb60bmr10904160ejc.614.1662758693045; Fri, 09 Sep 2022 14:24:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662758693; cv=none; d=google.com; s=arc-20160816; b=c6ir2M+FaHaLgo6etn16sH4ayccKVmxmGtK2EUgzYhSeexCR5KaIhlI9V4VMYz9nuw sgkk+WeygV8i5OEsMcEno6sSB5JZwthDdSHP242Ny+fT70H+jtTWUEgfasLtke8q66gK HNgGt4IcMkLQ1OwVLYTynrDXEoC05U4ZaTjEtZjp2B+WEWqEa1rdXXMOYXkRw4qpKGkC B/MwV+uEofauQQxgcOwkuewjDhHIbdxClOPV+j4VA5MlFmCNiNVfHyRIsCUWFxs50zLd owKtgRbhkqVsNolrazyCEwgUNUSeBHbAzQVogMHp/f4ozW38rDv1Pzbpc08HsvNexHnE 8+kg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=WfbZS6oLht1t6DyfDUZFhXZ+RrWWMQ+iuTY/70kZbgE=; b=QdRFgHXwTtoyJ35z1oG0v0/7rQVKhTrDr0DDDBiTb/IYOd2mGnBMxM4z0uezjiSf3O 2Z95LIEJVSOJ3yBYVG4BwM2aMfMQfu567fBO9Zuteouii45uuzUpELx4s03gbJ2IyOYH sJLqiJzPEjhb15wRl+183Soi0ad/yv45SqGxxZEDTgVvvynNFkoCLwRJqLpTfDUbF4Ba GP/x+B+2vmM14ZfYOdLVMRtQDiy4aQ2Vc6gVO7z0dHwzla/VBGaklcZC/Bis/Tr9CFTW K3wjDM+v0uUhRDe/A6yVuQv9bW9ms/WgMMYIamNYny2/Oxae3uF+MMZkfPrb0oDqMKq9 T01g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=ka553G4L; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id oz35-20020a1709077da300b007413f3cdd18si1382972ejc.238.2022.09.09.14.24.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Sep 2022 14:24:53 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=ka553G4L; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 18B14385734C for ; Fri, 9 Sep 2022 21:24:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 18B14385734C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1662758692; bh=WfbZS6oLht1t6DyfDUZFhXZ+RrWWMQ+iuTY/70kZbgE=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=ka553G4L9Y1OgyG1Nsbqu9dCGHSYiaNT04LVK2h4dOUO0SaH463O1uTRJNwJWXTzV dN6a97ZvYci/OerPX5oZZzMr6l/l3BGT/Rxh0zisZafhvAtL07KlgYojRoJ4mXALwz vRF2R1KXEUqMjlNGHnLqnQ83PQNjF9OYe36JaT2s= 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 42C2F3858429 for ; Fri, 9 Sep 2022 21:24:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 42C2F3858429 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-156-Sj-nwRSrOj283rUEzlur5w-1; Fri, 09 Sep 2022 17:24:03 -0400 X-MC-Unique: Sj-nwRSrOj283rUEzlur5w-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 510403C025AD for ; Fri, 9 Sep 2022 21:24:03 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.2.17.147]) by smtp.corp.redhat.com (Postfix) with ESMTP id 27DC140D282E; Fri, 9 Sep 2022 21:24:03 +0000 (UTC) To: gcc-patches@gcc.gnu.org Subject: [committed] analyzer: add test coverage for flexible array members [PR98247] Date: Fri, 9 Sep 2022 17:24:01 -0400 Message-Id: <20220909212401.626138-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.4 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_LOW, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1743528859474899544?= X-GMAIL-MSGID: =?utf-8?q?1743528859474899544?= Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Pushed to trunk as r13-2571-g084dc9a0c6cec1. gcc/testsuite/ChangeLog: PR analyzer/98247 * gcc.dg/analyzer/flexible-array-member-1.c: New test. Signed-off-by: David Malcolm --- .../gcc.dg/analyzer/flexible-array-member-1.c | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/analyzer/flexible-array-member-1.c diff --git a/gcc/testsuite/gcc.dg/analyzer/flexible-array-member-1.c b/gcc/testsuite/gcc.dg/analyzer/flexible-array-member-1.c new file mode 100644 index 00000000000..2df085a43f2 --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/flexible-array-member-1.c @@ -0,0 +1,100 @@ +#include +#include + +struct str { + size_t len; + char data[]; +}; + +struct str * +test_const_size (void) +{ + struct str *str = malloc(sizeof(str) + 10); + if (str) { + str->len = 10; + memset(str->data, 'x', 10); + return str; + } + return NULL; +} + +struct str * +test_const_size_oob_1 (void) +{ + /* Forgetting to add space for the trailing array. */ + struct str *str = malloc(sizeof(str)); + if (str) { + str->len = 10; + memset(str->data, 'x', 10); /* { dg-warning "heap-based buffer overflow" "Wanalyzer-out-of-bounds" } */ + /* { dg-warning "'memset' writing 10 bytes into a region of size 0 overflows the destination" "Wstringop-overflow" { target *-*-* } .-1 } */ + return str; + } + return NULL; +} + +struct str * +test_const_size_oob_2 (void) +{ + struct str *str = malloc(sizeof(str) + 10); + if (str) { + str->len = 10; + /* Using the wrong size here. */ + memset(str->data, 'x', 11); /* { dg-warning "heap-based buffer overflow" "Wanalyzer-out-of-bounds" } */ + /* { dg-warning "'memset' writing 11 bytes into a region of size 10 overflows the destination" "Wstringop-overflow" { target *-*-* } .-1 } */ + return str; + } + return NULL; +} + +struct str * +test_symbolic_size (size_t len) +{ + struct str *str = malloc(sizeof(str) + len); + if (str) { + str->len = len; + memset(str->data, 'x', len); + return str; + } + return NULL; +} + +struct str * +test_symbolic_size_oob (size_t len) +{ + /* Forgetting to add space for the trailing array. */ + struct str *str = malloc(sizeof(str)); + if (str) { + str->len = len; + memset(str->data, 'x', len); /* { dg-warning "heap-based buffer overflow" "PR analyzer/98247" { xfail *-*-* } } */ + // TODO(xfail): we don't yet complain about this case, which occurs when len > 0 + return str; + } + return NULL; +} + +struct str * +test_symbolic_size_with_terminator (size_t len) +{ + struct str *str = malloc(sizeof(str) + len + 1); + if (str) { + str->len = len; + memset(str->data, 'x', len); + str->data[len] = '\0'; + return str; + } + return NULL; +} + +struct str * +test_symbolic_size_with_terminator_oob (size_t len) +{ + /* Forgetting to add 1 for the terminator. */ + struct str *str = malloc(sizeof(str) + len); + if (str) { + str->len = len; + memset(str->data, 'x', len); + str->data[len] = '\0'; /* { dg-warning "heap-based buffer overflow" } */ + return str; + } + return NULL; +}