From patchwork Thu Mar 14 17:00:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1912187 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=XgdDg9Js; 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 4TwYbN48wWz1yWn for ; Fri, 15 Mar 2024 04:02:40 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7FF42386F038 for ; Thu, 14 Mar 2024 17:02:38 +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 ESMTPS id EE294385C413 for ; Thu, 14 Mar 2024 17:01:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EE294385C413 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 EE294385C413 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=1710435675; cv=none; b=fTffVxXF8VHjG8KT7Hk54qxtUr8cUm1AL76bTqZ1zTvrSI0HI3VwCusZuiVVqDC1hHgtIgzdvwo1n+TZAxNnqv4mnt/sOGugqpZB8fqd3k3EMb5Kw8A1ngX8ncchdWQ95uxthPGOrvIywVc003UfQarYjF3e5BkARamFJg6D0oo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710435675; c=relaxed/simple; bh=HS/uUKshAf93Ar4LHnMcR3lt0RMOU1eGrocq9BIZEWo=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=bRTyCO9sqAU00v0FPJJMwxfC4+Hq/IHuFoIDR/3rH5dHx9BDXNuWszhHgaDbfIUnkJ9CYhqH+Sw3+8wjOHOyAB/9ZAB+GXJNc8l9j4nHu7N9G+fR2aYqp9kq6kcZYGQluluAAFWwqrZSfhsN/T0c5VopxgpCOasHPLpEibgJ5Fs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1710435672; 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=v/ARItBFaLNwmrXHqtzzT0jaXDERlLWrnOGxRvKcla8=; b=XgdDg9Js+513A/PppyUKHcEpYILK2o9Om/jrRI/nAac3iN1y4+CtSZP+yv7jC5HIzSNCVP cp84erzka7YUy2I4jNlZUihzRMpvBuQAhRDqLuoR62YxYbX5Lei2WjEOYVxp3f4+KW58us fXBcPW3JxIZDl4JqsrMRROGwCSnE8FU= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-192-9oFJqnY7P9m16qy1h3Bl3w-1; Thu, 14 Mar 2024 13:01:10 -0400 X-MC-Unique: 9oFJqnY7P9m16qy1h3Bl3w-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id E2A7E1869C61; Thu, 14 Mar 2024 17:01:09 +0000 (UTC) Received: from localhost (unknown [10.42.28.8]) by smtp.corp.redhat.com (Postfix) with ESMTP id AFED6492BC6; Thu, 14 Mar 2024 17:01:09 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix std::format("{}", negative_integer) [PR114325] Date: Thu, 14 Mar 2024 17:00:52 +0000 Message-ID: <20240314170109.2835409-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 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_H4, 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: 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 Tested aarch64-linux. Pushed to trunk. -- >8 -- The fast path for "{}" format strings has a bug for negative integers where the length passed to std::to_chars is too long. libstdc++-v3/ChangeLog: PR libstdc++/114325 * include/std/format (_Scanner::_M_scan): Pass correct length to __to_chars_10_impl. * testsuite/std/format/functions/format.cc: Check negative integers with empty format-spec. --- libstdc++-v3/include/std/format | 7 ++++--- libstdc++-v3/testsuite/std/format/functions/format.cc | 5 +++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format index 1e839e88db4..613016d1a10 100644 --- a/libstdc++-v3/include/std/format +++ b/libstdc++-v3/include/std/format @@ -4091,6 +4091,7 @@ namespace __format __sink_out = __sink.out(); if constexpr (is_same_v<_CharT, char>) + // Fast path for "{}" format strings and simple format arg types. if (__fmt.size() == 2 && __fmt[0] == '{' && __fmt[1] == '}') { bool __done = false; @@ -4124,14 +4125,14 @@ namespace __format __uval = make_unsigned_t<_Tp>(~__arg) + 1u; else __uval = __arg; - const auto __n = __detail::__to_chars_len(__uval) + __neg; - if (auto __res = __sink_out._M_reserve(__n)) + const auto __n = __detail::__to_chars_len(__uval); + if (auto __res = __sink_out._M_reserve(__n + __neg)) { auto __ptr = __res.get(); *__ptr = '-'; __detail::__to_chars_10_impl(__ptr + (int)__neg, __n, __uval); - __res._M_bump(__n); + __res._M_bump(__n + __neg); __done = true; } } diff --git a/libstdc++-v3/testsuite/std/format/functions/format.cc b/libstdc++-v3/testsuite/std/format/functions/format.cc index a27fbe74631..4499397aaf9 100644 --- a/libstdc++-v3/testsuite/std/format/functions/format.cc +++ b/libstdc++-v3/testsuite/std/format/functions/format.cc @@ -157,6 +157,11 @@ test_std_examples() // Restore std::locale::global(std::locale::classic()); + + string s5 = format("{}", -100); // PR libstdc++/114325 + VERIFY(s5 == "-100"); + string s6 = format("{:d} {:d}", -123, 999); + VERIFY(s6 == "-123 999"); } }