From patchwork Fri Oct 4 10:03:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1992672 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=RrR06kfr; 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 4XKkf25SZmz1xtH for ; Fri, 4 Oct 2024 20:03:54 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C1E443846056 for ; Fri, 4 Oct 2024 10:03:52 +0000 (GMT) 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 ESMTP id F364F385DDD7 for ; Fri, 4 Oct 2024 10:03:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F364F385DDD7 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org F364F385DDD7 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1728036212; cv=none; b=NfYqumUpQ8PONl2MY0zjJq+1ubusz2Dqe0Yz7sByGcx4Q/C55gF71dhjs2E8PE+K32QJ4lncbqKgBhDOc1cIHIUAFxpSmYRJ/TEC9bZkrI1Qudsn6bDgOKjPb229/+RPq7VT1UPhHydF3/E9LNNZJw1buK3fIZjla3oxmGLG88k= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1728036212; c=relaxed/simple; bh=GcG/D+ZQqWG65OCS5JosKjTalZ/ekH4KkHingiY0UVc=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=ncn/ONYnoNhAyHIOdd3/T9/e6eWnzEtvFXIOUyiQzJjhiuMx+G9gNqMhyr1QrGobDzmmQPP7SpyBQly2jrKJXvfIrgp5GzB1NiT5KBkXi6XIwq2huVbNCd6cL4fYrKAzWzBLb7jnuuqajo6VPRyx1b8XffLR7s36JJuvHwdqwl8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1728036210; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=b2qp8zjf4TPxi+v/p+KjHU9EMY7DIZ0d14FlHZlWT/E=; b=RrR06kfrTPv0VTd4wZpP5noa9nuwWrpHe5CC59e0yj0bvqjyOWw6E+vzjMZGbI86Mkg7hY 9uG+rJiLnWy3vuV1iuOQUitgPlU0GYJnc25GBirDbXsBEsNji7qUIg3ulPr4M0/B9jvi+b kftD5bsPQJ8AQ75menG63y9wxkPzoHQ= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-522-4l3am2ukMCGruoq0Z5Ukgw-1; Fri, 04 Oct 2024 06:03:29 -0400 X-MC-Unique: 4l3am2ukMCGruoq0Z5Ukgw-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 46A701955D91 for ; Fri, 4 Oct 2024 10:03:28 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.61]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 9ABEF19560A2; Fri, 4 Oct 2024 10:03:27 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 494A3OIt2242035 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 4 Oct 2024 12:03:24 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 494A3OiH2242034; Fri, 4 Oct 2024 12:03:24 +0200 Date: Fri, 4 Oct 2024 12:03:24 +0200 From: Jakub Jelinek To: David Malcolm Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] diagnostic, pch: Fix up the new diagnostic PCH methods for ubsan checking [PR116936] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org Hi! The PR notes that the new pch_save/pch_restore methods I've added recently invoke UB if either m_classification_history.address () or m_push_list.address () is NULL (which can happen if those vectors are empty (and in the pch_save case nothing has been pushed into them before either). While the corresponding length is necessarily 0, fwrite (NULL, something, 0, f) or fread (NULL, something, 0, f) still invoke UB. The following patch fixes that by not calling fwrite/fread if the corresponding length is 0. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-10-04 Jakub Jelinek PR pch/116936 * diagnostic.cc (diagnostic_option_classifier::pch_save): Only call fwrite if corresponding length is non-zero. (diagnostic_option_classifier::pch_restore): Only call fread if corresponding length is non-zero. Jakub --- gcc/diagnostic.cc.jj 2024-10-01 09:38:58.014961851 +0200 +++ gcc/diagnostic.cc 2024-10-02 20:33:37.922953272 +0200 @@ -165,11 +165,13 @@ diagnostic_option_classifier::pch_save ( unsigned int lengths[2] = { m_classification_history.length (), m_push_list.length () }; if (fwrite (lengths, sizeof (lengths), 1, f) != 1 - || fwrite (m_classification_history.address (), - sizeof (diagnostic_classification_change_t), - lengths[0], f) != lengths[0] - || fwrite (m_push_list.address (), sizeof (int), - lengths[1], f) != lengths[1]) + || (lengths[0] + && fwrite (m_classification_history.address (), + sizeof (diagnostic_classification_change_t), + lengths[0], f) != lengths[0]) + || (lengths[1] + && fwrite (m_push_list.address (), sizeof (int), + lengths[1], f) != lengths[1])) return -1; return 0; } @@ -187,11 +189,13 @@ diagnostic_option_classifier::pch_restor gcc_checking_assert (m_push_list.is_empty ()); m_classification_history.safe_grow (lengths[0]); m_push_list.safe_grow (lengths[1]); - if (fread (m_classification_history.address (), - sizeof (diagnostic_classification_change_t), - lengths[0], f) != lengths[0] - || fread (m_push_list.address (), sizeof (int), - lengths[1], f) != lengths[1]) + if ((lengths[0] + && fread (m_classification_history.address (), + sizeof (diagnostic_classification_change_t), + lengths[0], f) != lengths[0]) + || (lengths[1] + && fread (m_push_list.address (), sizeof (int), + lengths[1], f) != lengths[1])) return -1; return 0; }