From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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-Google-Smtp-Source: AGHT+IEvYPSjkWMH26/f5fQNHldxSP6N/tGw8akocCan0G68trn/r4DRtUS9xagC7gS30sk0qSfISg== 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 Content-Transfer-Encoding: 8bit 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 List-Id: 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) \ -- 2.34.1