From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oa1-x2f.google.com (mail-oa1-x2f.google.com [IPv6:2001:4860:4864:20::2f]) by sourceware.org (Postfix) with ESMTPS id 51FD33857725 for ; Mon, 11 Sep 2023 13:50:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 51FD33857725 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-oa1-x2f.google.com with SMTP id 586e51a60fabf-1c8d895a602so2627731fac.2 for ; Mon, 11 Sep 2023 06:50:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1694440215; x=1695045015; 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=sdr42wBcyMdV7kIiTns1svwk/p8zrFOBf3ER77yXvYU=; b=en2yiUjRRGAR1E1pZq2qvg5+d640QFQzb8yhEu4fvL7ZI0iNKwDssEkujnr2F0qEOZ oZWcQCfWqbzZlGVsq0NjPtGMg5UIeHFIAcUYNDiDELboVUD9+hutJxlWlVCZ8xp+dBug sLl+hYwQ+A6GJbAnHo53g5tCGn9DjUiOZOyygJnfekHRV/auddyOijPf/C4ZPheDPEcK SVb3QSiIlfYzI/OArSq+Y0CooQDWtPLgSJtYYW4gZ+H2WMwxCdwJbUUQDJSHdXvDVl2I o7q3RqdBGnc26CFUl5RGLC+E6YfEhsDOKstUjOk7iRmfANe8RhbzJslYpaW4dtP5wFsx qQLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694440215; x=1695045015; 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=sdr42wBcyMdV7kIiTns1svwk/p8zrFOBf3ER77yXvYU=; b=b/QivDi63eushONiH3IY3fCHCjy+0YTH8H2Vd1LC0B/kfFMgwSXSa1qXcxPiSZBmKd u61fNPV8pfSKdm1VgQ+ppfAxab9L6WkFy7O6cMqzhINsdDzkN4THVUwNynK7THK40x9s j2N3ZlfmjvN9cLfwavKG+7zVb1rjS+ZD5YkrWWDLaDuCRIJpF8RnRSEv4IO/LQD2GJBQ QIE9yHcFn1OlOnOFGNQAN2AxkcYzDkk64OLTiWwTrO7XWgXj3t+dqw7+XIJ3ghti5wD9 spNKvoAgLVYyTuktlysPnRMsWEPauMO5ms7yuSZTOSgBBkZSpqJ9O3qT/TXOe26GrkCb 7XHg== X-Gm-Message-State: AOJu0YyiXkrFNjMnm4/2KrBAqjLd0TZgWA7X6pCDm4SvT3bequDv65ub wujquVftet8AuROxnIfncYSK1DXCnkKCWLxkdiY2sQ== X-Google-Smtp-Source: AGHT+IHSmOwkCsgBLXCIO4qFtc62YEFqkIxQN4tsHNt37/AfaXLGbUKfgtqVDa2yvpV+V/0VLu4dog== X-Received: by 2002:a05:6870:239f:b0:1ba:64a5:d2d1 with SMTP id e31-20020a056870239f00b001ba64a5d2d1mr10320295oap.20.1694440215136; Mon, 11 Sep 2023 06:50:15 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:91cb:8dba:c245:9a76:bfb3]) by smtp.gmail.com with ESMTPSA id w3-20020a056870a2c300b001c4d922b4fasm4025505oak.8.2023.09.11.06.50.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Sep 2023 06:50:14 -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: Mon, 11 Sep 2023 10:50:06 -0300 Message-Id: <20230911135006.3390666-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230911135006.3390666-1-adhemerval.zanella@linaro.org> References: <20230911135006.3390666-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.3 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. --- 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..dd387393f0 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') @@ -846,6 +831,8 @@ __printf_fp_buffer_2 (struct hack_digit_param *p, struct __printf_buffer *buf, } } } + + scratch_buffer_free (&wscbuf); } /* Now remove unnecessary '0' at the end of the string. */ @@ -952,9 +939,6 @@ __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); } #define PRINTF_FP_FETCH(P, FLOAT, SUFFIX, MANT_DIG) \ -- 2.34.1