From patchwork Thu Dec 1 02:42:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 61295 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 04E26386EC0C for ; Thu, 1 Dec 2022 02:44:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 04E26386EC0C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1669862687; bh=grftwHGIFosY4NUhXGi7ht+WbNepJiRRUvyx5Id0h5Q=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=BPPh/RVSpmaJGWYauorlTQOFB1FgVBFmf0OBuGyrCQje3Wjd3QLHOgavSMKMCUBvR 6wZgKQgS/d/Je47DUIIWQLoBppadiERrxion9ciKiYrSzpl2JdKu/HMX4mTsgt1alp rNGuw7hBuc4CQhxXCei/sNr5ibvhixFSX587glbU= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id 7E6E73858288 for ; Thu, 1 Dec 2022 02:42:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7E6E73858288 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-47-iqoDkCXnPIuCtCkyPv24rQ-1; Wed, 30 Nov 2022 21:42:06 -0500 X-MC-Unique: iqoDkCXnPIuCtCkyPv24rQ-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 83779101A528 for ; Thu, 1 Dec 2022 02:42:06 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.2.16.65]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5F24A2024CC5; Thu, 1 Dec 2022 02:42:06 +0000 (UTC) To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [committed 7/7] analyzer: fix i18n issues in symbolic out-of-bounds [PR106626] Date: Wed, 30 Nov 2022 21:42:00 -0500 Message-Id: <20221201024200.3722982-7-dmalcolm@redhat.com> In-Reply-To: <20221201024200.3722982-1-dmalcolm@redhat.com> References: <20221201024200.3722982-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.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_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" Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Pushed to trunk as r13-4431-geaaf97b6147095. gcc/analyzer/ChangeLog: PR analyzer/106626 * bounds-checking.cc (symbolic_past_the_end::describe_final_event): Delete, moving to symbolic_buffer_overflow::describe_final_event and symbolic_buffer_over_read::describe_final_event, eliminating composition of text strings via "byte_str" and "m_dir_str". (symbolic_past_the_end::m_dir_str): Delete field. (symbolic_buffer_overflow::symbolic_buffer_overflow): Drop m_dir_str. (symbolic_buffer_overflow::describe_final_event): New, as noted above. (symbolic_buffer_over_read::symbolic_buffer_overflow): Drop m_dir_str. (symbolic_buffer_over_read::describe_final_event): New, as noted above. Signed-off-by: David Malcolm --- gcc/analyzer/bounds-checking.cc | 192 +++++++++++++++++++++++--------- 1 file changed, 138 insertions(+), 54 deletions(-) diff --git a/gcc/analyzer/bounds-checking.cc b/gcc/analyzer/bounds-checking.cc index aaf3f22109b..1c44790f86d 100644 --- a/gcc/analyzer/bounds-checking.cc +++ b/gcc/analyzer/bounds-checking.cc @@ -544,62 +544,10 @@ public: return label_text (); } - label_text - describe_final_event (const evdesc::final_event &ev) final override - { - const char *byte_str; - if (pending_diagnostic::same_tree_p (m_num_bytes, integer_one_node)) - byte_str = "byte"; - else - byte_str = "bytes"; - - if (m_offset) - { - if (m_num_bytes && TREE_CODE (m_num_bytes) == INTEGER_CST) - { - if (m_diag_arg) - return ev.formatted_print ("%s of %E %s at offset %qE" - " exceeds %qE", m_dir_str, - m_num_bytes, byte_str, - m_offset, m_diag_arg); - else - return ev.formatted_print ("%s of %E %s at offset %qE" - " exceeds the buffer", m_dir_str, - m_num_bytes, byte_str, m_offset); - } - else if (m_num_bytes) - { - if (m_diag_arg) - return ev.formatted_print ("%s of %qE %s at offset %qE" - " exceeds %qE", m_dir_str, - m_num_bytes, byte_str, - m_offset, m_diag_arg); - else - return ev.formatted_print ("%s of %qE %s at offset %qE" - " exceeds the buffer", m_dir_str, - m_num_bytes, byte_str, m_offset); - } - else - { - if (m_diag_arg) - return ev.formatted_print ("%s at offset %qE exceeds %qE", - m_dir_str, m_offset, m_diag_arg); - else - return ev.formatted_print ("%s at offset %qE exceeds the" - " buffer", m_dir_str, m_offset); - } - } - if (m_diag_arg) - return ev.formatted_print ("out-of-bounds %s on %qE", - m_dir_str, m_diag_arg); - return ev.formatted_print ("out-of-bounds %s", m_dir_str); - } - protected: tree m_offset; tree m_num_bytes; tree m_capacity; - const char *m_dir_str; }; /* Concrete subclass to complain about overflows with symbolic values. */ @@ -611,7 +559,6 @@ public: tree num_bytes, tree capacity) : symbolic_past_the_end (reg, diag_arg, offset, num_bytes, capacity) { - m_dir_str = "write"; } const char *get_kind () const final override @@ -638,6 +585,75 @@ public: "heap-based buffer overflow"); } } + + label_text + describe_final_event (const evdesc::final_event &ev) final override + { + if (m_offset) + { + /* Known offset. */ + if (m_num_bytes) + { + /* Known offset, known size. */ + if (TREE_CODE (m_num_bytes) == INTEGER_CST) + { + /* Known offset, known constant size. */ + if (pending_diagnostic::same_tree_p (m_num_bytes, + integer_one_node)) + { + /* Singular m_num_bytes. */ + if (m_diag_arg) + return ev.formatted_print + ("write of %E byte at offset %qE exceeds %qE", + m_num_bytes, m_offset, m_diag_arg); + else + return ev.formatted_print + ("write of %E byte at offset %qE exceeds the buffer", + m_num_bytes, m_offset); + } + else + { + /* Plural m_num_bytes. */ + if (m_diag_arg) + return ev.formatted_print + ("write of %E bytes at offset %qE exceeds %qE", + m_num_bytes, m_offset, m_diag_arg); + else + return ev.formatted_print + ("write of %E bytes at offset %qE exceeds the buffer", + m_num_bytes, m_offset); + } + } + else + { + /* Known offset, known symbolic size. */ + if (m_diag_arg) + return ev.formatted_print + ("write of %qE bytes at offset %qE exceeds %qE", + m_num_bytes, m_offset, m_diag_arg); + else + return ev.formatted_print + ("write of %qE bytes at offset %qE exceeds the buffer", + m_num_bytes, m_offset); + } + } + else + { + /* Known offset, unknown size. */ + if (m_diag_arg) + return ev.formatted_print ("write at offset %qE exceeds %qE", + m_offset, m_diag_arg); + else + return ev.formatted_print ("write at offset %qE exceeds the" + " buffer", m_offset); + } + } + /* Unknown offset. */ + if (m_diag_arg) + return ev.formatted_print ("out-of-bounds write on %qE", + m_diag_arg); + return ev.formatted_print ("out-of-bounds write"); + } }; /* Concrete subclass to complain about over-reads with symbolic values. */ @@ -649,7 +665,6 @@ public: tree num_bytes, tree capacity) : symbolic_past_the_end (reg, diag_arg, offset, num_bytes, capacity) { - m_dir_str = "read"; } const char *get_kind () const final override @@ -677,6 +692,75 @@ public: "heap-based buffer over-read"); } } + + label_text + describe_final_event (const evdesc::final_event &ev) final override + { + if (m_offset) + { + /* Known offset. */ + if (m_num_bytes) + { + /* Known offset, known size. */ + if (TREE_CODE (m_num_bytes) == INTEGER_CST) + { + /* Known offset, known constant size. */ + if (pending_diagnostic::same_tree_p (m_num_bytes, + integer_one_node)) + { + /* Singular m_num_bytes. */ + if (m_diag_arg) + return ev.formatted_print + ("read of %E byte at offset %qE exceeds %qE", + m_num_bytes, m_offset, m_diag_arg); + else + return ev.formatted_print + ("read of %E byte at offset %qE exceeds the buffer", + m_num_bytes, m_offset); + } + else + { + /* Plural m_num_bytes. */ + if (m_diag_arg) + return ev.formatted_print + ("read of %E bytes at offset %qE exceeds %qE", + m_num_bytes, m_offset, m_diag_arg); + else + return ev.formatted_print + ("read of %E bytes at offset %qE exceeds the buffer", + m_num_bytes, m_offset); + } + } + else + { + /* Known offset, known symbolic size. */ + if (m_diag_arg) + return ev.formatted_print + ("read of %qE bytes at offset %qE exceeds %qE", + m_num_bytes, m_offset, m_diag_arg); + else + return ev.formatted_print + ("read of %qE bytes at offset %qE exceeds the buffer", + m_num_bytes, m_offset); + } + } + else + { + /* Known offset, unknown size. */ + if (m_diag_arg) + return ev.formatted_print ("read at offset %qE exceeds %qE", + m_offset, m_diag_arg); + else + return ev.formatted_print ("read at offset %qE exceeds the" + " buffer", m_offset); + } + } + /* Unknown offset. */ + if (m_diag_arg) + return ev.formatted_print ("out-of-bounds read on %qE", + m_diag_arg); + return ev.formatted_print ("out-of-bounds read"); + } }; /* Check whether an access is past the end of the BASE_REG. */