From patchwork Thu Sep 14 14:06:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 722541 Delivered-To: patch@linaro.org Received: by 2002:ab3:5684:0:b0:23f:8cfb:5ad7 with SMTP id d4csp472192ltc; Thu, 14 Sep 2023 07:07:01 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEzrW7n6EDE6AxGFXJfrsxTvkDHbz5wfVDWr/UYsKT2TIJjBVH7TPxKCiqBwzpqb9NchnkW X-Received: by 2002:a2e:8519:0:b0:2bc:ee89:6976 with SMTP id j25-20020a2e8519000000b002bcee896976mr4645795lji.24.1694700420990; Thu, 14 Sep 2023 07:07:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694700420; cv=none; d=google.com; s=arc-20160816; b=05kENOKeC8r1P5bbENLY6rj7BoopGAMBrKhsR1ab7eT3z4TUDOs6VbeY7ad5wowL8N Ly8CavggQIimgc46pugcAHfLTlrn2nvQ1eVGnucTSnu5N1nZ5SvbPdHkwetv72P1OfMR fa4TLq8D6KnCiLnTx+7Ru/ucf9xRQNA1exBzuvLtr3uUqhgm2E6rPHoaee3zSLhgJXmB 3g9XAB5CxH6cF1nBGamU9LNUygVYwVmKcHyg4ydgQwJ02GNX7Zi2QIjYZpUibvA6WVhU m1Zy3px71aFC3/Ju3iXs2WU5XTJ7NWkpjoGhJ0Oe9b0hwsJQ+lHOuTbNdTtpUcUh/oNf evzg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature:dmarc-filter:delivered-to; bh=ioXOjBCRsRt6YbwiLABc+BgkQN2VttAD9+nXvIe9okM=; fh=Ji/BrrBV5vvGE0PSJoc8P6vA3gQfM/DANnlOBrK0hiY=; b=Bf2z9D+gxnvuwKngZ4tgR2OYCnChyPrsvGUL/T2W6PZBDK90HqLX5lsmuMD11fICUS giHMUhABoJsqj9xW/E+cHNmdoqDkASOelDDUN6TSpaDYmlUWUWk3RYQiS59XeXD5u8KA +TlrBMSefE8BgnDSAGfFu+AAKh0EfcDfqe87xzF2bAQOKFzfcsQ9OL+eiJWzw9BekpXh PG9rvJhgg4dgABtHo+Wan/TPBxKj6K+CPyh/dKI3VfUMzpl3Fno0zFKX8TMkeaaKj1vk 8wzVjD9USqW6FX7sN1PuyZJGIpjiekd1b3p33enhXrlADc5jx3KqnoOV1M530JkirfZI cwpw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=y57sn1Os; spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id s27-20020a170906169b00b009aa146cba1csi1342543ejd.723.2023.09.14.07.07.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 07:07:00 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=y57sn1Os; spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E42743855591 for ; Thu, 14 Sep 2023 14:06:59 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oi1-x22a.google.com (mail-oi1-x22a.google.com [IPv6:2607:f8b0:4864:20::22a]) by sourceware.org (Postfix) with ESMTPS id C76F53857C41 for ; Thu, 14 Sep 2023 14:06:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C76F53857C41 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-oi1-x22a.google.com with SMTP id 5614622812f47-3a9f88b657eso571224b6e.3 for ; Thu, 14 Sep 2023 07:06:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1694700412; x=1695305212; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=ioXOjBCRsRt6YbwiLABc+BgkQN2VttAD9+nXvIe9okM=; b=y57sn1OsfkVMnYhLtNJJoyaNWfsXioRIMH23Q5UUsb9KQqExXjEQS/zjkz7hcDAoWD h7FrK8TzboV9chOqs9RXCco4vOLyUoSe7sLVpkpO3PLir/KfcnU63lUGEBOmD0boSo9C IszelB9eEZGvcS7zUAzSLS609UttkGFSwdGS7MrGhXaakfyS/6ztIu6Mc80D/S+3uVSR qzz35EDYirR5giW09h7U0Gi7iyAJomsSH3AjB3LwXFeQnqKk6f+K0EJacPDtmaV+jZUB 8o0vr/Gl/D/VwxflgWS8uYFdKQXNZjpBiK0jZ2OjnBCn3lTMUwZ7ZTDYhM+0mZ5EIH05 aAiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694700412; x=1695305212; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ioXOjBCRsRt6YbwiLABc+BgkQN2VttAD9+nXvIe9okM=; b=Ss18Jqv5AtOGDoRjDKmzDbdtSQw8I3qpt43YWrzJSsMpW1LhueKDPxeh96Xm1e7Tje cSUwrMqwjV9LciFkrRSBDOBE6axlAvNXdVezR/+nj4IZSnaEcSzKZ6wsiw008hqNn8rE 9xR3ZKbRhWgkkhlkTVGuSNrBUlmcJBQCYGzBcug5P2v8VHqbzTITqukbkaD7uVQm9Mgl lwvo1DtBhR4CDnMWb0B1xpkLL6LTPu0ZBgfUK498R+5xuKDGVuoDz+aHeWP8g0uVhi8L 7jexxRuH988naAPPjWcFssckssx/qqQ0M8IubUsmPMgxnL3Ryn+XgEn9GUlM9kbOGbn/ 3yQw== X-Gm-Message-State: AOJu0YwJlfZPbL1+diYmi8di+BgQMlPnerYT0ytCS2FUcHdbCvswYOnK 8vuC+Z4m9eldEEXQ0gRQjJzvC2/Iwv2VnkWLUHSAfQ== X-Received: by 2002:a05:6808:1a1c:b0:3a9:eb53:3856 with SMTP id bk28-20020a0568081a1c00b003a9eb533856mr7412291oib.39.1694700412467; Thu, 14 Sep 2023 07:06:52 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:91cb:5173:d70:d8d:4d2c]) by smtp.gmail.com with ESMTPSA id s26-20020a056808009a00b003a862e70bcbsm620417oic.13.2023.09.14.07.06.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 07:06:51 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Joe Simmons-Talbott Subject: [PATCH v2 2/2] stdio: Remove __printf_fp_buffer_2 alloca usage Date: Thu, 14 Sep 2023 11:06:39 -0300 Message-Id: <20230914140639.1912983-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230914140639.1912983-1-adhemerval.zanella@linaro.org> References: <20230914140639.1912983-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, 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+patch=linaro.org@sourceware.org And replace with a scratch_buffer. --- Changes from v1: * Fixed stdio-common regressions. --- stdio-common/printf_fp.c | 36 ++++++++++-------------------------- 1 file changed, 10 insertions(+), 26 deletions(-) diff --git a/stdio-common/printf_fp.c b/stdio-common/printf_fp.c index 9ddf61fd6d..7c85edb8c9 100644 --- a/stdio-common/printf_fp.c +++ b/stdio-common/printf_fp.c @@ -17,12 +17,8 @@ License along with the GNU C Library; if not, see . */ -/* The gmp headers need some configuration frobs. */ -#define HAVE_ALLOCA 1 - #include #include -#include #include #include #include @@ -168,11 +164,6 @@ __printf_fp_buffer_2 (struct hack_digit_param *p, struct __printf_buffer *buf, /* General helper (carry limb). */ mp_limb_t cy; - /* Buffer in which we produce the output. */ - char *wbuffer = NULL; - /* Flag whether wbuffer and buffer are malloc'ed or not. */ - int buffer_malloced = 0; - p->expsign = 0; if (p->special) @@ -600,6 +591,9 @@ __printf_fp_buffer_2 (struct hack_digit_param *p, struct __printf_buffer *buf, int dig_max; int significant; char spec = _tolower (info->spec); + struct scratch_buffer wscbuf; + + scratch_buffer_init (&wscbuf); if (spec == 'e') { @@ -662,7 +656,7 @@ __printf_fp_buffer_2 (struct hack_digit_param *p, struct __printf_buffer *buf, /* Allocate buffer for output. We need two more because while rounding it is possible that we need two more characters in front of all the other output. If the amount of memory we have to allocate is too - large use `malloc' instead of `alloca'. */ + large use `malloc' instead of a static buffer. */ if (__glibc_unlikely (chars_needed >= (size_t) -1 - 2 || chars_needed < fracdig_max)) { @@ -671,21 +665,12 @@ __printf_fp_buffer_2 (struct hack_digit_param *p, struct __printf_buffer *buf, __printf_buffer_mark_failed (buf); return; } - size_t wbuffer_to_alloc = 2 + chars_needed; - buffer_malloced = ! __libc_use_alloca (wbuffer_to_alloc); - if (__builtin_expect (buffer_malloced, 0)) + if (!scratch_buffer_set_array_size (&wscbuf, 1, 2 + chars_needed)) { - wbuffer = malloc (wbuffer_to_alloc); - if (wbuffer == NULL) - { - /* Signal an error to the caller. */ - __printf_buffer_mark_failed (buf); - return; - } + __printf_buffer_mark_failed (buf); + return; } - else - wbuffer = alloca (wbuffer_to_alloc); - wcp = wstartp = wbuffer + 2; /* Let room for rounding. */ + wcp = wstartp = wscbuf.data + 2; /* Let room for rounding. */ /* Do the real work: put digits in allocated buffer. */ if (p->expsign == 0 || p->type != 'f') @@ -951,10 +936,9 @@ __printf_fp_buffer_2 (struct hack_digit_param *p, struct __printf_buffer *buf, if (info->left) __printf_buffer_pad (buf, info->pad, width); - } - if (buffer_malloced) - free (wbuffer); + scratch_buffer_free (&wscbuf); + } } #define PRINTF_FP_FETCH(P, FLOAT, SUFFIX, MANT_DIG) \