From patchwork Wed Jan 1 22:52:40 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?TWlrbMOzcyBNw6F0w6k=?= X-Patchwork-Id: 103852 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 6FB853857BA3 for ; Wed, 1 Jan 2025 22:53:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6FB853857BA3 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=dXVuVhaZ X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by sourceware.org (Postfix) with ESMTPS id 1D8F23857C5F for ; Wed, 1 Jan 2025 22:52:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1D8F23857C5F 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 1D8F23857C5F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::333 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1735771966; cv=none; b=hPSbHNEgHImXZ4qIOi49JRGFWYgEzHASsMTKyvVFBG0X4iN5Gi3Q2RoAUnXyFtb27D4wxiBaZEontF1KJ8j4AnpOX2d4CpAonwunVGNAA/VL48hNFeBCaocclKmzL0qS6yzwgBhqvlCBibcwX4fZ5buH7IxN3atpzpxegDHwUMM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1735771966; c=relaxed/simple; bh=3VSxcl3BmttwbPSjBnohQeXH43T5E7cuh/VxsONqM9w=; h=DKIM-Signature:Message-ID:Date:MIME-Version:To:From:Subject; b=hbXAoehFzPyvVvf3ECbsSOugU2jLq7Jrek0w0u7Osd+Oz19ai/jidanpOcEUrRUEDDO6IesXUdQIaK81PTDBiFdpdQVbffYFEgVKQag+fbFVEyQen9KsSt7v/RABQ6WAKh9yohxuQpKCl1BJvl1p45UCxx1RPulOVTi2Nbzd6js= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1D8F23857C5F Received: by mail-wm1-x333.google.com with SMTP id 5b1f17b1804b1-436249df846so75279875e9.3 for ; Wed, 01 Jan 2025 14:52:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735771964; x=1736376764; darn=sourceware.org; h=subject:from:to:content-language:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=3VSxcl3BmttwbPSjBnohQeXH43T5E7cuh/VxsONqM9w=; b=dXVuVhaZjXM961FyTsjL5zVQ1gOfR0CImmxznN/xbAhznCAorxTyP2h4LKDgUe2NrR r+m3xFGuMa1XxP8Qcs+H0XdZnlI1c03Lu306RnR7P5BBsrKowwy9vF1NPdzzf/78ONkC k+yNCNQ5ruSzW9IftAJrqnPrWlznmJq7HFotBsJHyohvHbn2To03Go8XLwXjj0uQR70q 9jEWnrWikUoEfLhcB+VMTZN3navFpSSa5QXzybJp4oGPSoYDWOuhvDHciwf630ByPHB8 Vr+FK9B3DE432WsdqBdyckIQiJx3JH34aIJHS+re4mBTBT0SfQ5AELihIJZVnB652N90 Dzvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735771964; x=1736376764; h=subject:from:to:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=3VSxcl3BmttwbPSjBnohQeXH43T5E7cuh/VxsONqM9w=; b=PPCylWUqeloIsIkfozr2YbQYURrhKnohxMtIa77+5C4Bst/897j0h9AOXV5/xjUON3 I6zEu2yiyBjQgZ9SSLNhXumybJW18k9UFZbjVzrGt6LoeEGMkxe+mWZPc4QjXwXXIxVf BBY2G2f/RNKNy7+wz0/7/drynl3Q0oimJADY2dVCf7S9Lx8ad2oTftHM6Msm1eji4HHq Cvh10qDVRI+g54TZNx8kjY+cg7dUFoU3jElEHO+NR1oxDg0x/2Fj18Bd/GigobwGXzsy e6suietn3j5ueOt1f7ziMKWSVFNEU8QgZG68Jk+s7t1EG8PIs9bvHZhYXULa0qx6b4e7 apfg== X-Gm-Message-State: AOJu0YwaAEefhh+9YBUXtdbQPB6xVvRwK3yqebb2czGaTZzNDsCSIHW/ 6umgqgUrdVMHN+Jb4r6AHfoV/jsZTv5y4wiv8c30+ofiB3sXCYRic7EW6Q== X-Gm-Gg: ASbGncujY86TwRXVaSORWQpVydxcax9wCGKKfFzyxRdjU/uBjvgcfq5ikBydAct5MqR qTolU3/L1Ld00uFdxy/Gk7fxf4hNMdF5Psq7kVhsogyPnNlJCvvaQy8VicexXQTdCozCunR5FtG 8vIkGsXP6vv5mqVjrMBmSYDybtrrVRwr5sxBZ6McnTFsQiwbzJQO+4ZC2DcF+vBsDc5O9AUdQx7 mu1mdrGPFE9gYqHhD5uclklVlM+31333kBKVDkiYs+iQyjQeAdWUSYJrlnh10noyiYgn6vgUriE oYy87A== X-Google-Smtp-Source: AGHT+IEmyib5OOc/b9IS2xXQttm/saf7DB4GBd8d7MvNyliOkzKMPvnQf0KyUst3lmnSYf617he9pQ== X-Received: by 2002:a05:6000:156f:b0:386:2aba:a7f6 with SMTP id ffacd0b85a97d-38a224071e3mr35134404f8f.49.1735771964456; Wed, 01 Jan 2025 14:52:44 -0800 (PST) Received: from [10.68.1.209] (84-236-6-34.pool.digikabel.hu. [84.236.6.34]) by smtp.googlemail.com with ESMTPSA id ffacd0b85a97d-38a1c8ac97fsm35987353f8f.92.2025.01.01.14.52.41 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 01 Jan 2025 14:52:42 -0800 (PST) Message-ID: Date: Wed, 1 Jan 2025 23:52:40 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US, hu-HU To: libc-alpha@sourceware.org From: =?utf-8?b?TWlrbMOzcyBNw6F0w6k=?= Subject: [PATCH] stdio: make scanf(%f) eat the same number of characters as strtod() would X-Spam-Status: No, score=-8.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, HK_RANDOM_ENVFROM, HK_RANDOM_FROM, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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: 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 According to the spec the %f conversion specifier (and its synonyms) must behave exactly as strtod(). This behavior matches FreeBSD libc. On MSVC (with Godbolt) the "5e-" test fails miserably. Fixed the relevant unit tests. BTW the unit tests in this directory are a complete mess. From 97906587c0c51304bf6737c93441609255ab6d2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikl=C3=B3s=20M=C3=A1t=C3=A9?= Date: Wed, 1 Jan 2025 23:50:13 +0100 Subject: [PATCH] stdio: make scanf(%f) eat the same number of characters as strtod() would According to the spec the %f conversion specifier (and its synonyms) must behave exactly as strtod(). This behavior matches FreeBSD libc. On MSVC (with Godbolt) the "5e-" test fails miserably. Fixed the relevant unit tests. BTW the unit tests in this directory are a complete mess. --- stdio-common/bug26.c | 75 ++++++++++++++++++++++++++++----- stdio-common/tstscanf.c | 2 +- stdio-common/vfscanf-internal.c | 11 +++++ 3 files changed, 77 insertions(+), 11 deletions(-) diff --git a/stdio-common/bug26.c b/stdio-common/bug26.c index 74a00d04d0..5229cfd9e5 100644 --- a/stdio-common/bug26.c +++ b/stdio-common/bug26.c @@ -17,22 +17,77 @@ #include #include +#include int main (void) { FILE *f; - int lost = 0; - int c; + int errors = 0; + int c, n; double d; - char s[] = "+.e"; + char *e; + char s1[] = "+.e"; /* Contains no number, + parse should leave everything in the input buffer. */ + char s2[] = "5e-"; /* Contains 5, + parse should leave "e-" in the input buffer. */ - f = fmemopen (s, strlen (s), "r"); - /* This should fail to parse a floating-point number, and leave 'e' in the - input. */ - lost |= (fscanf (f, "%lf", &d) != 0); + d = strtod (s1, &e); + if (d != 0.0 || e != s1) + { + errors++; + puts ("strtod (s1) failed!"); + } + + f = fmemopen (s1, strlen (s1), "r"); + c = fscanf (f, "%lf", &d); + if (c != 0) + { + errors++; + puts ("fscanf (s1) had a valid conversion??"); + } + c = fgetc (f); + if (c != '+') + { + errors++; + printf ("fscanf (s1) ate too many characters (c = '%c', pos %lu)\n", + c, ftell (f)); + } + fclose (f); + + d = strtod (s2, &e); + if (d != 5.0 || e != s2+1) + { + errors++; + puts ("strtod (s2) failed!"); + } + f = fmemopen (s2, strlen (s2), "r"); + d = 0.0; + c = fscanf (f, "%lf%n", &d, &n); + if (c != 1) + { + errors++; + printf ("fscanf (s2) had wrong conversion count %d\n", c); + } + if (d != 5.0) + { + errors++; + printf ("fscanf (s2) had wrong conversion result %f\n", d); + } + if (n != 1) + { + errors++; + printf ("fscanf (s2) reported wrong character count %d\n", n); + } c = fgetc (f); - lost |= c != 'e'; - puts (lost ? "Test FAILED!" : "Test succeeded."); - return lost; + if (c != 'e') + { + errors++; + printf ("fscanf (s2) ate too many characters (c = '%c', pos %lu)\n", + c, ftell (f)); + } + fclose (f); + + puts (errors ? "Test FAILED!" : "Test succeeded."); + return errors != 0; } diff --git a/stdio-common/tstscanf.c b/stdio-common/tstscanf.c index bb3e55dbb7..3562ef64a0 100644 --- a/stdio-common/tstscanf.c +++ b/stdio-common/tstscanf.c @@ -151,7 +151,7 @@ main (int argc, char **argv) { 2, -12.8F, "degrees", "" }, { 0, 0.0F, "", "" }, { 3, 10.0F, "LBS", "fertilizer" }, - { 3, 100.0F, "rgs", "energy" }, + { 3, 100.0F, "ergs", "energy" }, { -1, 0.0F, "", "" }}; size_t rounds = 0; float quant; diff --git a/stdio-common/vfscanf-internal.c b/stdio-common/vfscanf-internal.c index 87f23b5845..c3d4e81476 100644 --- a/stdio-common/vfscanf-internal.c +++ b/stdio-common/vfscanf-internal.c @@ -2582,6 +2582,17 @@ digits_extended_fail: *ARG (float *) = d; } + if (__glibc_unlikely (char_buffer_size (&charbuf) > tw - char_buffer_start (&charbuf))) + { + --charbuf.current; /* We have the read chars plus a '\0'. */ + int n = char_buffer_size (&charbuf) - (tw - char_buffer_start (&charbuf)); + for (int i = 0; i < n; ++i) + { + c = *(--charbuf.current); + ungetc_not_eof (c, s); + } + } + if (__glibc_unlikely (tw == char_buffer_start (&charbuf))) conv_error (); -- 2.45.2