From patchwork Tue Nov 28 00:12:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lewis Hyatt X-Patchwork-Id: 1869053 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=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=hBP0V+pT; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; 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 [IPv6:2620:52:3:1:0:246e:9693:128c]) (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 4SfNG90Wmlz1yRW for ; Tue, 28 Nov 2023 11:13:18 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 88BF33860757 for ; Tue, 28 Nov 2023 00:13:15 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-qk1-x736.google.com (mail-qk1-x736.google.com [IPv6:2607:f8b0:4864:20::736]) by sourceware.org (Postfix) with ESMTPS id F0FAC3858C53 for ; Tue, 28 Nov 2023 00:13:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F0FAC3858C53 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org F0FAC3858C53 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::736 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701130384; cv=none; b=CC5DIWDdcPhaEheN/VAyS4MYMSWQseHiNdrciZgrdBpHAhAeHCa7SmWLsdPMOIZ6VBJG+WVd41zn4tLjAgROkp3WsSvOMg9W00ALwVTkFi3x4EyeEZjrCU57UOe1GT5koKd1BJhVFIAngG9nCUrP1n8tF5UCBK7KmFO1VklGDHE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701130384; c=relaxed/simple; bh=NqdAMr4QGw5FiCxzdP6kPep0wNBkVfGIGmCHe06Wa00=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=i8WkiPt1kLd3xgqHqxR1KaTSyp8fvkXljvtaLdgNHGGG0Av5Sr1TpxdfGPJ+8/GA1ugio4Tr1eoUK6WrubzS0OMGsVa9gXG2Vig8Y8/bZOm8mQNELyWN0iPKIQn2DoatgLh60EMAIRdOMudAJTeLZsj7Vt7tQF8Xo/335zul95Q= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-qk1-x736.google.com with SMTP id af79cd13be357-77897c4ac1fso284927085a.3 for ; Mon, 27 Nov 2023 16:13:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701130382; x=1701735182; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=HFnfEdjnyfP8uRXPSXlYWANy5Kr92Y46YhEd+JeCMTU=; b=hBP0V+pTMPRaW10wMAAevyxn1wIs0kovEh98S0xj/54KwHAn/9TDj5cLIblHb58GRv ixpiXYl3zbPhVZ9uDHRAZI1yj2xUK2SH69v50oYir960ziDG/Ogy1emJ2/QxcEujIZzO jPSEDTj1inLRxR9IIJAUgcHgmvGoeLczTZPT4Z2p8f1sSRb9LvtKmNGKWxKmQdgIT16U 7sgJBU9b8bvsQ9OTlpy0W1aZCYrkeognyXLRjhJUzqvQyDIMaKqCQbkIL2mF6HrPZVuj kAOsMb0fdhwqK4Rk+D+sbAhcXQEvthOwrbusWeNTOueLCAWFjR0ihN8ZHhRVfhKZgsi9 6SMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701130382; x=1701735182; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=HFnfEdjnyfP8uRXPSXlYWANy5Kr92Y46YhEd+JeCMTU=; b=XlukqYjQT5kbjQsuWSa1eks3os4Z5Jcq50vWbXwYGB1XAZylK6hMo2CwvVvyDiY76h A6VIOSlpjT15qLbFr0fnyUslCF+VPTmbrF5MnMU73lFx7jEIf4O2YAt+ZN3uJ0zVgmR+ T/uxMDYEbR1BzeCZYl4/9UWMv8SQuv2W6QYVL5RegmIN/XrHbfXO+BHQJG7nzF9ofdpv wgtP9CGoXjKGjmpPWkJAycKuX2MnjDhv5wSZq61Mq5se6+EhM4GCN+uIwS6cvI1BX4/w l6kYBhd9wQeLVmdqfNKBVmPiFMlUHcz5i+HYdkpXekRLEYz1RIHcaAZd3KVrHty/kuoB dQiA== X-Gm-Message-State: AOJu0YzN7cnUaUBYSV/SPT7y70ETID5L/u9tOpc5adzjO7Uw0WmXAQ10 +HGctEB4LTDt3hkBk9Jp3OUL89MkRyE= X-Google-Smtp-Source: AGHT+IH+vzqhHBA80L4OLnebprHcJ45MYu1qmcwJ9x6gkbNj3unlfltZAuZ+zgTRp8pOfu4XIHUJrg== X-Received: by 2002:a05:620a:8bc4:b0:778:8676:fd4 with SMTP id qy4-20020a05620a8bc400b0077886760fd4mr11838559qkn.73.1701130382167; Mon, 27 Nov 2023 16:13:02 -0800 (PST) Received: from localhost.localdomain (96-67-140-173-static.hfc.comcastbusiness.net. [96.67.140.173]) by smtp.gmail.com with ESMTPSA id v2-20020ae9e302000000b0077d5e1e4edesm4045793qkf.57.2023.11.27.16.13.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 16:13:01 -0800 (PST) From: Lewis Hyatt To: gcc-patches@gcc.gnu.org Cc: Lewis Hyatt Subject: [PATCH] libcpp: Fix unsigned promotion for unevaluated divide by zero [PR112701] Date: Mon, 27 Nov 2023 19:12:56 -0500 Message-Id: <20231128001256.40923-1-lhyatt@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-3038.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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.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 Hello- https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112701 Here is a one-line fix to an edge case in libcpp's expression evaluator noted in the PR. Bootstrap + regtest all languages on x86-64 Linux. Is it OK please? Thanks! -Lewis -- >8 -- When libcpp encounters a divide by zero while processing a constant expression "x/y", it returns "x" as a fallback. The value of the fallback is not normally important, since an error will be generated anyway, but if the expression appears in an unevaluated context, such as "0 ? 0/0u : -1", then there will be no error, and the fallback value will be meaningful to the extent that it may cause promotion from signed to unsigned of an operand encountered later. As the PR notes, libcpp does not do the unsigned promotion correctly in this case; fix it by making the fallback return value unsigned as necessary. libcpp/ChangeLog: PR preprocessor/112701 * expr.cc (num_div_op): Set unsignedp appropriately when returning a stub value for divide by 0. gcc/testsuite/ChangeLog: PR preprocessor/112701 * gcc.dg/cpp/expr.c: Add additional tests to cover divide by 0 in an unevaluated context, where the unsignedness still matters. --- libcpp/expr.cc | 1 + gcc/testsuite/gcc.dg/cpp/expr.c | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/libcpp/expr.cc b/libcpp/expr.cc index 825d2c2369d..4f4a9722ac7 100644 --- a/libcpp/expr.cc +++ b/libcpp/expr.cc @@ -2216,6 +2216,7 @@ num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op, if (!pfile->state.skip_eval) cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0, "division by zero in #if"); + lhs.unsignedp = unsignedp; return lhs; } diff --git a/gcc/testsuite/gcc.dg/cpp/expr.c b/gcc/testsuite/gcc.dg/cpp/expr.c index 532bd681237..055e17ae753 100644 --- a/gcc/testsuite/gcc.dg/cpp/expr.c +++ b/gcc/testsuite/gcc.dg/cpp/expr.c @@ -1,6 +1,7 @@ /* Copyright (C) 2000, 2001 Free Software Foundation, Inc. */ /* { dg-do preprocess } */ +/* { dg-additional-options "-Wall" } */ /* Test we get signedness of ?: operator correct. We would skip evaluation of one argument, and might therefore not transfer its @@ -8,10 +9,27 @@ /* Neil Booth, 19 Jul 2002. */ -#if (1 ? -2: 0 + 1U) < 0 +#if (1 ? -2: 0 + 1U) < 0 /* { dg-warning {the left operand of ":" changes sign} } */ #error /* { dg-bogus "error" } */ #endif -#if (0 ? 0 + 1U: -2) < 0 +#if (0 ? 0 + 1U: -2) < 0 /* { dg-warning {the right operand of ":" changes sign} } */ #error /* { dg-bogus "error" } */ #endif + +/* PR preprocessor/112701 */ +#if (0 ? 0/0u : -1) < 0 /* { dg-warning {the right operand of ":" changes sign} } */ +#error /* { dg-bogus "error" } */ +#endif + +#if (0 ? 0u/0 : -1) < 0 /* { dg-warning {the right operand of ":" changes sign} } */ +#error /* { dg-bogus "error" } */ +#endif + +#if (1 ? -1 : 0/0u) < 0 /* { dg-warning {the left operand of ":" changes sign} } */ +#error /* { dg-bogus "error" } */ +#endif + +#if (1 ? -1 : 0u/0) < 0 /* { dg-warning {the left operand of ":" changes sign} } */ +#error /* { dg-bogus "error" } */ +#endif