From patchwork Thu Oct 1 17:43:35 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 145125 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 21EE44BB24F9 for ; Thu, 1 Oct 2026 17:46:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 21EE44BB24F9 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=LVAcMf20 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 ESMTP id 27F7B4BB24DD for ; Thu, 1 Oct 2026 17:46:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 27F7B4BB24DD Authentication-Results: sourceware.org; dmarc=pass (p=quarantine 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 27F7B4BB24DD Authentication-Results: sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790876764; cv=none; b=l/af9nMigxiFsOP0nflypn7tApMSrzO3aJaLos/tj7Xix1OnuilafHokTiy0l6y1Yz+nl0QTDafR0NpFJMRtpSm/cBhOW88J9mw7HgF8BnTDsnIJaTzKTPrlK6Nh1Lfd20V7PoSqCsNqOvjWaDD9pnH7cdrRrxfA0QHkQetg8VY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790876764; c=relaxed/simple; bh=hoDZ5d6gFVjZM4Pwrc/fG5rKC+6NuWxbc976Hi0avTw=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ex5zmIBDIanBK0vTRejL+Tkb+9Iv1D/VuU9LsdgEIX7TFg7oOCMEK+xVxKrz9bVBS9nyH/y5nIOWa7gsf5/huDo4KcsvkwZVB87KUvgyHOYRWzX7yXMTzzVaXXzhp7F7s25Iz/AePGDSwbfqYO3yfEDLvbqeqbMtxIQhgU2bW3I= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=LVAcMf20 DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 27F7B4BB24DD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1790876763; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=bOyp4mabtl5XTNTB3B9jFoOhgLooPfY5V7yXP0cSYlk=; b=LVAcMf20xpbyUffinV14b4KPgE95EC5B+dZsFIMdb9QQ3+g+d1yDKWjbeZJTpLIxkQyWQY gSttP67BO6GdelCM3Ny7EAn7pr4Zja9R3eqPzkMBGZQmpHrcGcTTf0Q7GLjwPDyw0PIoiI uQQuu8IXxsXfrQ2naV5A0jqPXq6wFT0= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-445-wuHMx4CCM02jANe5PzxLBQ-1; Thu, 01 Oct 2026 13:46:02 -0400 X-MC-Unique: wuHMx4CCM02jANe5PzxLBQ-1 X-Mimecast-MFC-AGG-ID: wuHMx4CCM02jANe5PzxLBQ_1790876761 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 421DE18011C5; Thu, 1 Oct 2026 17:46:01 +0000 (UTC) Received: from zen.kayari.org (headnet05.pony-001.prod.iad2.dc.redhat.com [10.2.32.117]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 5A51E30001B9; Thu, 1 Oct 2026 17:46:00 +0000 (UTC) From: Jonathan Wakely To: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org Subject: [PATCH] libstdc++: Fix std::from_chars to not read past end of input [PR127666] Date: Thu, 1 Oct 2026 18:43:35 +0100 Message-ID: <20261001174559.3035550-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: oSHjEdZpO75k2ZQuBvpjwotdP30DDxjAoA4K5O_3OTo_1790876761 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.3 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_PASS, SPF_NONE, TXREP, URI_HEX shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on 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~patchwork=sourceware.org@gcc.gnu.org The bug report points out one out-of-bounds read and the second was found by an LLM. The testcase was generated by an LLM. The code fixes are artisanal handwritten code. Assisted-by: LLM libstdc++-v3/ChangeLog: PR libstdc++/127666 * src/c++17/floating_from_chars.cc (pattern): Avoid reading past the end of the input. Remove unused variable. * testsuite/20_util/from_chars/127666.cc: New test. --- Tested x86_64-linux. I'm not sure whether the mmap and sysconf effective targets are enough to ensure the new test works everywhere. If we get new FAILs for it we can adjust the test to not run on more targets. libstdc++-v3/src/c++17/floating_from_chars.cc | 9 ++- .../testsuite/20_util/from_chars/127666.cc | 70 +++++++++++++++++++ 2 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 libstdc++-v3/testsuite/20_util/from_chars/127666.cc diff --git a/libstdc++-v3/src/c++17/floating_from_chars.cc b/libstdc++-v3/src/c++17/floating_from_chars.cc index ed434b395288..c28fc9a459ef 100644 --- a/libstdc++-v3/src/c++17/floating_from_chars.cc +++ b/libstdc++-v3/src/c++17/floating_from_chars.cc @@ -451,13 +451,16 @@ namespace // fmt has the value of one of the enumerators of chars_format. __glibcxx_assert(valid_fmt(fmt)); - string_view res; - + // Fail if the input is empty or starts with '+' if (first == last || *first == '+') [[unlikely]] return nullptr; const int neg = (*first == '-'); + // Fail if the input consists of a lone '-' + if (first + neg == last) + return nullptr; + if (std::memchr("iInN", (unsigned char)first[neg], 4)) { ptrdiff_t len = last - first; @@ -489,7 +492,7 @@ namespace else // Only need 4 chars for "-NAN" len = neg + 3; - buf.assign(first, 0, len); + buf.assign(first, len); // prevent make_result correcting for "0x" fmt = chars_format::general; return buf.c_str(); diff --git a/libstdc++-v3/testsuite/20_util/from_chars/127666.cc b/libstdc++-v3/testsuite/20_util/from_chars/127666.cc new file mode 100644 index 000000000000..25989f2cfee8 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/from_chars/127666.cc @@ -0,0 +1,70 @@ +// { dg-do run { target c++17 } } +// { dg-require-effective-target mmap } +// { dg-require-effective-target sysconf } +// { dg-add-options ieee } +// { dg-additional-options "-DSKIP_LONG_DOUBLE" { target aarch64-*-rtems* aarch64-*-vxworks* x86_64-*-vxworks* } } + +// Bug 127666 +// std::from_chars(first, last, long double&) reads past 'last' for "inf"/"nan" + +#include +#include +#include +#include +#include +#include + +using namespace std::string_view_literals; + +// One past the last writable byte; the guard page starts here, so reading +// at or beyond this address faults. +static char* buf_end; + +template +void +check(std::string_view input, std::errc ec, std::size_t len) +{ + // Position the input against the guard page so an over-read faults. + char* const first = buf_end - input.size(); + std::memcpy(first, input.data(), input.size()); + T value = 1; + auto res = std::from_chars(first, buf_end, value); + VERIFY( res.ec == ec ); + VERIFY( res.ptr == first + len ); +} + +template +void +test_type() +{ + // The inf/nan forms used to read past the end via strlen. + check("inf"sv, std::errc{}, 3); + check("infinity"sv, std::errc{}, 8); + check("nan"sv, std::errc{}, 3); + check("nan(1)"sv, std::errc{}, 6); + check("-nan(ab)"sv, std::errc{}, 8); + + // A lone sign used to read one byte past the end. + check("-"sv, std::errc::invalid_argument, 0); + check("+"sv, std::errc::invalid_argument, 0); +} + +int +main() +{ + const long pg = ::sysconf(_SC_PAGESIZE); + VERIFY( pg > 0 ); + char* const base = (char*) ::mmap(nullptr, 2 * pg, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + VERIFY( base != MAP_FAILED ); + VERIFY( ::mprotect(base + pg, pg, PROT_NONE) == 0 ); + buf_end = base + pg; + + test_type(); + test_type(); +#ifndef SKIP_LONG_DOUBLE + test_type(); +#endif + + ::munmap(base, 2 * pg); +}