From patchwork Tue Aug 20 21:53:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joseph Myers X-Patchwork-Id: 96201 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 096D03870860 for ; Tue, 20 Aug 2024 21:53:59 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id A86C83858433 for ; Tue, 20 Aug 2024 21:53:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A86C83858433 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 A86C83858433 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724190820; cv=none; b=kku6T77k41fe9e572IrMV0M/RPMFaL21LsWl8ZejrGQnrHDdu9/pZUWCko0BsST4saFy8Hi8nMaJgBBetN+XmzPl2JHnTC9+LzLgE3mRNitK3ay/82euVWAHzytVZukM2Fr7ajgp8Vllo9ThiTIwi9Wu+Z774D1ZLOnoLQbtPN8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724190820; c=relaxed/simple; bh=ybyZy/Qz8b3Yc96SWhxi1JIoDhnCgqgMdXa7TbvvJTM=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=kVPvgHfDLBWdJm7sQsEakrqZcJA3afLevkV09aOhsDzYeqzwHuJgxXP+7ZuIVCDAWVhKW1j5ZlBWSEPEM0moMnHkw+tcF4tljvfYmEda2GTpBcYlZ5joYkNM7DZXtGa2nMLZ5MdvHLfzwh0rpY7XzcTP9ROvOqpwK+ConONSmL4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724190816; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=wqIlKmW2i9d8B43xpf8J0lzEJFFEoasF/lpQCzHsY3A=; b=E6yZqyUlE07BTfsApri131gSc2cOexTBYR7Sjb6Ip0ZvaeDBnJt+tRUFjo6QgVmSWA6Wxw 6w7e5VXz8BzhpWbuilOlpEJvaJE1lnT1JJNBoHhBvBVJj2ZoaQm+SRcAgMGnAybS1Kbstc uZrd3fWn/RvqH8p8d3MioLgHFHgF0wQ= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-683-n3miRrOwNAWSWsufTa-nBQ-1; Tue, 20 Aug 2024 17:53:35 -0400 X-MC-Unique: n3miRrOwNAWSWsufTa-nBQ-1 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-428e48612acso64231215e9.3 for ; Tue, 20 Aug 2024 14:53:35 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724190814; x=1724795614; h=mime-version:message-id:subject:to:from:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=wqIlKmW2i9d8B43xpf8J0lzEJFFEoasF/lpQCzHsY3A=; b=C3ReWZDYMnsUWax3xLBW8t6zio/iSJm0FOBCooRc4u+caubwXAcXlrnkOryJBQuE7I LQsie/uzrJ7XZQEtertykdUgOQqVV6h/WiOhYHmKb3NHQMqAhrsCnLN+8IhzJ/EV8Wg2 LugEJYAPhFuNnwbwydJdB1c7CpP/xgBNAy/xSbCzGsCuaN7x+J9ISTmh4X8wqXCKuPDP uXOEIvEUCgnIyuXBQSv6WoSquBZBsnBWCjsbT8d47gSNmJHXLcmbTjRAkh/DjApus2pj 90pPNebxhpecw2XjiodQ+llgvB2X91iR6euruXPKgD6JV4pLseFhcGI9y1yVyah5gxc4 2nKA== X-Gm-Message-State: AOJu0YxkPBtYMimbPXuR3Y2WkW3tMDrJaL4S+it5S2OjMBvjNo0lLGp7 +8zXwhQQDyRT0EzcK4qiqW4vp2/oevSYPg/p2lJaADoxPzdu14KUtFrmVA5KAMVflUMgs9vPhDf P5l4+vV+R+5X01neIKo6oV4IzrkIVTbmLygJm7eu85b9hqYHlhzRGyudqEAer7RnOPFA2U5EUND tWCAfa/iXCeVCACh3/Sg37bi+ggDLRkVEj7qi1ZE9tpA== X-Received: by 2002:a5d:584d:0:b0:371:890c:891a with SMTP id ffacd0b85a97d-372fd57f47dmr251825f8f.8.1724190813890; Tue, 20 Aug 2024 14:53:33 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHj2V4s0mFDG84s1tsqnm+piX3OjcCpl8WNoptrvALlPaG1OgHsiJzSxAS7t3UAZ21KmC+51g== X-Received: by 2002:a5d:584d:0:b0:371:890c:891a with SMTP id ffacd0b85a97d-372fd57f47dmr251804f8f.8.1724190813005; Tue, 20 Aug 2024 14:53:33 -0700 (PDT) Received: from digraph.polyomino.org.uk (digraph.polyomino.org.uk. [2001:8b0:bf73:93f7::51bb:e332]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-371898aa393sm13979980f8f.90.2024.08.20.14.53.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Aug 2024 14:53:32 -0700 (PDT) Received: from jsm28 (helo=localhost) by digraph.polyomino.org.uk with local-esmtp (Exim 4.95) (envelope-from ) id 1sgWmv-00CPY6-9N for libc-alpha@sourceware.org; Tue, 20 Aug 2024 21:53:13 +0000 Date: Tue, 20 Aug 2024 21:53:13 +0000 (UTC) From: Joseph Myers To: libc-alpha@sourceware.org Subject: Do not set errno for overflowing NaN payload in strtod/nan (bug 32045) Message-ID: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-9.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, 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: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org As reported in bug 32045, it's incorrect for strtod/nan functions to set errno based on overflowing payload (strtod should only set errno for overflow / underflow of its actual result, and potentially if nothing in the string can be parsed as a number at all; nan should be a pure function that never sets it). Save and restore errno around the internal strtoull call and add associated test coverage. Tested for x86_64. --- This patch is relative to a tree with (pending review) applied. diff --git a/math/Makefile b/math/Makefile index f06d370383..b64c3eedd5 100644 --- a/math/Makefile +++ b/math/Makefile @@ -1077,6 +1077,7 @@ CFLAGS-test-flt-eval-method.c += -fexcess-precision=standard CFLAGS-test-fe-snans-always-signal.c += $(config-cflags-signaling-nans) CFLAGS-test-nan-const.c += -fno-builtin +CFLAGS-test-nan-payload.c += -fno-builtin CFLAGS-test-ceil-except-2.c += -fno-builtin CFLAGS-test-floor-except-2.c += -fno-builtin diff --git a/math/test-nan-payload.c b/math/test-nan-payload.c index 55c13de14e..413791e09f 100644 --- a/math/test-nan-payload.c +++ b/math/test-nan-payload.c @@ -18,6 +18,7 @@ #define _LIBC_TEST 1 #define __STDC_WANT_IEC_60559_TYPES_EXT__ +#include #include #include #include @@ -82,6 +83,26 @@ } \ while (0) +#define CLEAR_ERRNO \ + do \ + { \ + errno = 12345; \ + } \ + while (0) + +#define CHECK_ERRNO(TYPE, A) \ + do \ + { \ + if (errno == 12345) \ + puts ("PASS: " #TYPE " " #A " errno"); \ + else \ + { \ + puts ("FAIL: " #TYPE " " #A " errno"); \ + result = 1; \ + } \ + } \ + while (0) + /* Cannot test payloads by memcmp for formats where NaNs have padding bits. */ #define CAN_TEST_EQ(MANT_DIG) ((MANT_DIG) != 64 && (MANT_DIG) != 106) @@ -89,26 +110,58 @@ #define RUN_TESTS(TYPE, SFUNC, FUNC, PLFUNC, MANT_DIG) \ do \ { \ + CLEAR_ERRNO; \ TYPE n123 = WRAP_NAN (FUNC, "123"); \ + CHECK_ERRNO (TYPE, n123); \ CHECK_IS_NAN (TYPE, n123); \ + CLEAR_ERRNO; \ TYPE s123 = WRAP_STRTO (SFUNC, "NAN(123)"); \ + CHECK_ERRNO (TYPE, s123); \ CHECK_IS_NAN (TYPE, s123); \ + CLEAR_ERRNO; \ TYPE n456 = WRAP_NAN (FUNC, "456"); \ + CHECK_ERRNO (TYPE, n456); \ CHECK_IS_NAN (TYPE, n456); \ + CLEAR_ERRNO; \ TYPE s456 = WRAP_STRTO (SFUNC, "NAN(456)"); \ + CHECK_ERRNO (TYPE, s456); \ CHECK_IS_NAN (TYPE, s456); \ + CLEAR_ERRNO; \ TYPE nh123 = WRAP_NAN (FUNC, "0x123"); \ + CHECK_ERRNO (TYPE, nh123); \ CHECK_IS_NAN (TYPE, nh123); \ + CLEAR_ERRNO; \ TYPE sh123 = WRAP_STRTO (SFUNC, "NAN(0x123)"); \ + CHECK_ERRNO (TYPE, sh123); \ CHECK_IS_NAN (TYPE, sh123); \ + CLEAR_ERRNO; \ TYPE n123x = WRAP_NAN (FUNC, "123)"); \ + CHECK_ERRNO (TYPE, n123x); \ CHECK_IS_NAN (TYPE, n123x); \ + CLEAR_ERRNO; \ TYPE nemp = WRAP_NAN (FUNC, ""); \ + CHECK_ERRNO (TYPE, nemp); \ CHECK_IS_NAN (TYPE, nemp); \ + CLEAR_ERRNO; \ TYPE semp = WRAP_STRTO (SFUNC, "NAN()"); \ + CHECK_ERRNO (TYPE, semp); \ CHECK_IS_NAN (TYPE, semp); \ + CLEAR_ERRNO; \ TYPE sx = WRAP_STRTO (SFUNC, "NAN"); \ + CHECK_ERRNO (TYPE, sx); \ CHECK_IS_NAN (TYPE, sx); \ + CLEAR_ERRNO; \ + TYPE novf = WRAP_NAN (FUNC, "9999999999" \ + "99999999999999999999" \ + "9999999999"); \ + CHECK_ERRNO (TYPE, novf); \ + CHECK_IS_NAN (TYPE, novf); \ + CLEAR_ERRNO; \ + TYPE sovf = WRAP_STRTO (SFUNC, "NAN(9999999999" \ + "99999999999999999999" \ + "9999999999)"); \ + CHECK_ERRNO (TYPE, sovf); \ + CHECK_IS_NAN (TYPE, sovf); \ if (CAN_TEST_EQ (MANT_DIG)) \ CHECK_SAME_NAN (TYPE, n123, s123); \ CHECK_PAYLOAD (TYPE, PLFUNC, n123, 123); \ diff --git a/stdlib/strtod_nan_main.c b/stdlib/strtod_nan_main.c index 4cb286d2b3..39fb7e9f75 100644 --- a/stdlib/strtod_nan_main.c +++ b/stdlib/strtod_nan_main.c @@ -16,6 +16,7 @@ License along with the GNU C Library; if not, see . */ +#include #include #include #include @@ -50,7 +51,9 @@ STRTOD_NAN (const STRING_TYPE *str, STRING_TYPE **endptr, STRING_TYPE endc) STRING_TYPE *endp; unsigned long long int mant; + int save_errno = errno; mant = STRTOULL (str, &endp, 0); + __set_errno (save_errno); if (endp == cp) SET_NAN_PAYLOAD (retval, mant);