From patchwork Sat Oct 14 12:26:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Iain Buclaw X-Patchwork-Id: 1848796 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gdcproject.org header.i=@gdcproject.org header.a=rsa-sha256 header.s=MBO0001 header.b=nFYmDlAw; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4S72fh4qcXz1yq3 for ; Sat, 14 Oct 2023 23:26:20 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9158C385840C for ; Sat, 14 Oct 2023 12:26:18 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mout-p-101.mailbox.org (mout-p-101.mailbox.org [IPv6:2001:67c:2050:0:465::101]) by sourceware.org (Postfix) with ESMTPS id 363713858C60 for ; Sat, 14 Oct 2023 12:26:07 +0000 (GMT) ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 363713858C60 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2001:67c:2050:0:465::101 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697286368; cv=none; b=sZUrr7KFEB3o3zMlG7UoAGxdNje5ZJkeFEwgHUTzwBxNhjVejNMJheQeKqku6f87AKmuPACOCroTMSuz6ZQoZqeuyzlOkRVT6KfqX+MQCnWTq6Ab3cn3C6Vysxs8khtYTe7Mwe4F9CjUVBKgQf9OntXbdAbfQbb3PPf68HEAgUM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697286368; c=relaxed/simple; bh=fbQNAe8NB974fbNV2jHWbnO1MeR4YOwfo6MEyk7ZWjE=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=NkPOk/NnQwAKkjInWIsGTdAMihx7iv6TYDWUsuFQwIqwcfUt6DsRvzUC4BLCZWw8Hrm5VQ46/zcAujDAfYRpKeKqdeUJ8uyTa6txlU/admRWr0GuAgURAil5r2PvntTp3ngU71KYGH51aVHfgXjxXHw4UO6rAA9pznF/Df/BSSA= ARC-Authentication-Results: i=1; server2.sourceware.org DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 363713858C60 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=gdcproject.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gdcproject.org Received: from smtp1.mailbox.org (smtp1.mailbox.org [10.196.197.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-101.mailbox.org (Postfix) with ESMTPS id 4S72fN1FKZz9sWP; Sat, 14 Oct 2023 14:26:04 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gdcproject.org; s=MBO0001; t=1697286364; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=B617/ZdxRn/RsUzQ69c81lES8LLjLnb149IHlok2fS4=; b=nFYmDlAwnugcWpcWj+Cp6sJPc6dA9Iq5CCOy4fNlOx+KYxeqdfYj3FocPO3GK8RIWi5chG 75FwSCpLWYiGY39en1hCrpqtmt2mfkFISk3V1jkmlV5x7Km88goToOqYS3H6t6t52fl0yn 7d7RDtl+D/3jIivJC90F3udmkll0b2CSuOjsJPf1k9ZL6/5++Q4mufDjrTcv4ERM8XZM1L bmqJKEHjdgPRxxpJDGYuAff63fvUgHNwP1ieqd9ChJwOl+hG5F3NtbZl7/EesqCLKG6+oG yzt7VJw4ZZEt9jxz8A3k4daA6/OJ6zdTK9FfFmW4XptGKyTZGdfch4O0OjfqGg== From: Iain Buclaw To: gcc-patches@gcc.gnu.org Cc: Iain Buclaw Subject: [committed] Fix ICE in set_cell_span, at text-art/table.cc:148 with D front-end and -fanalyzer Date: Sat, 14 Oct 2023 14:26:01 +0200 Message-Id: <20231014122601.1667638-1-ibuclaw@gdcproject.org> MIME-Version: 1.0 X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_PASS, TXREP, WEIRD_PORT 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Hi, The internal error in analyzer turned out to be caused by a subtly invalid tree representation of STRING_CSTs in the D front-end, fixed by including the terminating NULL as part of the TREE_STRING_POINTER. When adding a first analyzer test for D, it flagged up another subtle mismatch in one assignment in the module support routines as well, fixed by generating the correct field type for the compiler-generated struct. Bootstrapped and regression tested on x86_64-linux-gnu/-m32, and committed to mainline. Regards, Iain. --- PR d/111537 gcc/d/ChangeLog: * expr.cc (ExprVisitor::visit (StringExp *)): Include null terminator in STRING_CST string. * modules.cc (get_compiler_dso_type): Generate ModuleInfo** type for the minfo fields. gcc/testsuite/ChangeLog: * gdc.dg/analyzer/analyzer.exp: New test. * gdc.dg/analyzer/pr111537.d: New test. --- gcc/d/expr.cc | 6 +-- gcc/d/modules.cc | 9 ++-- gcc/testsuite/gdc.dg/analyzer/analyzer.exp | 51 ++++++++++++++++++++++ gcc/testsuite/gdc.dg/analyzer/pr111537.d | 7 +++ 4 files changed, 66 insertions(+), 7 deletions(-) create mode 100644 gcc/testsuite/gdc.dg/analyzer/analyzer.exp create mode 100644 gcc/testsuite/gdc.dg/analyzer/pr111537.d diff --git a/gcc/d/expr.cc b/gcc/d/expr.cc index 7038655bc94..551d004c241 100644 --- a/gcc/d/expr.cc +++ b/gcc/d/expr.cc @@ -2535,13 +2535,13 @@ public: { /* Copy the string contents to a null terminated string. */ dinteger_t length = (e->len * e->sz); - char *string = XALLOCAVEC (char, length + 1); + char *string = XALLOCAVEC (char, length + e->sz); + memset (string, 0, length + e->sz); if (length > 0) memcpy (string, e->string, length); - string[length] = '\0'; /* String value and type includes the null terminator. */ - tree value = build_string (length, string); + tree value = build_string (length + e->sz, string); TREE_TYPE (value) = make_array_type (tb->nextOf (), length + 1); value = build_address (value); diff --git a/gcc/d/modules.cc b/gcc/d/modules.cc index f2180d30546..8d6c8f0f9ad 100644 --- a/gcc/d/modules.cc +++ b/gcc/d/modules.cc @@ -277,12 +277,13 @@ get_compiler_dso_type (void) DECL_CHAIN (field) = fields; fields = field; - field = create_field_decl (build_pointer_type (get_moduleinfo_type ()), - NULL, 1, 1); + tree moduleinfo_ptr_ptr_type = + build_pointer_type (build_pointer_type (get_moduleinfo_type ())); + + field = create_field_decl (moduleinfo_ptr_ptr_type, NULL, 1, 1); DECL_CHAIN (field) = fields; fields = field; - field = create_field_decl (build_pointer_type (get_moduleinfo_type ()), - NULL, 1, 1); + field = create_field_decl (moduleinfo_ptr_ptr_type, NULL, 1, 1); DECL_CHAIN (field) = fields; fields = field; diff --git a/gcc/testsuite/gdc.dg/analyzer/analyzer.exp b/gcc/testsuite/gdc.dg/analyzer/analyzer.exp new file mode 100644 index 00000000000..7b82b8e0cd1 --- /dev/null +++ b/gcc/testsuite/gdc.dg/analyzer/analyzer.exp @@ -0,0 +1,51 @@ +# Copyright (C) 2023 Free Software Foundation, Inc. + +# This file is part of GCC. +# +# GCC is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free +# Software Foundation; either version 3, or (at your option) any later +# version. +# +# GCC is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +# for more details. +# +# You should have received a copy of the GNU General Public License +# along with GCC; see the file COPYING3. If not see +# . + +# GCC testsuite that uses the `dg.exp' driver. + +# Load support procs. +load_lib gdc-dg.exp + +# If the analyzer has not been enabled, bail. +if { ![check_effective_target_analyzer] } { + return +} + +global DEFAULT_DFLAGS +if [info exists DEFAULT_DFLAGS] then { + set save_default_dflags $DEFAULT_DFLAGS +} + +# If a testcase doesn't have special options, use these. +set DEFAULT_DFLAGS "-fanalyzer -Wanalyzer-too-complex -fanalyzer-call-summaries" + +# Initialize `dg'. +dg-init + +# Main loop. +gdc-dg-runtest [lsort \ + [glob -nocomplain $srcdir/$subdir/*.d ] ] "" $DEFAULT_DFLAGS + +# All done. +dg-finish + +if [info exists save_default_dflags] { + set DEFAULT_DFLAGS $save_default_dflags +} else { + unset DEFAULT_DFLAGS +} diff --git a/gcc/testsuite/gdc.dg/analyzer/pr111537.d b/gcc/testsuite/gdc.dg/analyzer/pr111537.d new file mode 100644 index 00000000000..e50b05a3f79 --- /dev/null +++ b/gcc/testsuite/gdc.dg/analyzer/pr111537.d @@ -0,0 +1,7 @@ +// { dg-do compile } +import core.stdc.string; +void main() +{ + char[5] arr; + strcpy(arr.ptr, "hello world"); // { dg-warning "stack-based buffer overflow" } +}