From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 333B1385AF9A for ; Fri, 7 Jul 2023 17:53:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 333B1385AF9A Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1688752434; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=iSwGuhZ3nL9WeXcB8p1NJOlc/nR6d0PSMMPtlJWWF5o=; b=BfeYolgi5dCy53t5k+elGVwknT69fVRCZtIhVgdMMjaFMUi13FLQT0EtxaJPxdAq5LTcuy zglCiRRBtLe0UOqYi0sMFRgudFsaYlZTojErwHAzRNDguFu9Diwp9rTIMzNz1KAnb7MunE eoHf6M5d4S6X1OJAe55cRTAF3yaAC6Q= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-391-DkuOU7NcNR2czWe8-4iUrQ-1; Fri, 07 Jul 2023 13:53:54 -0400 X-MC-Unique: DkuOU7NcNR2czWe8-4iUrQ-1 Received: by mail-qv1-f71.google.com with SMTP id 6a1803df08f44-635984f84a9so23789366d6.0 for ; Fri, 07 Jul 2023 10:53:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688752433; x=1691344433; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=iSwGuhZ3nL9WeXcB8p1NJOlc/nR6d0PSMMPtlJWWF5o=; b=Js4o3iPXa+fWPM6HwuKGvgBYSgDd1+dEObxB0j9C/IfX1+LzLzzapWKb4jMQYLPaBw O4qYVpScBHmcD4D+nuTrKM+liMA9RISZt8BPaqhgUyy1DcgRuE+6np/hQSC0T5Lpw0Xa zTD/e34SNaJVJCgQzNgQ7EYlcvYGq92F2zMKBAZiwuc3tu0js1Mm8tMDMPqxKkg6rhdT urL1o+bQ4SOrhePdDBWp2nc+tqpcqA7Poehr6BzZXgf/6aE8E0rNL6WnSU9tk13LY7ap D6oekfTwIMkP7/aepHWep+EXJ7ZWtPsVazglxSTgDGuGPR+YkzPtwrlr2irUfh3OpEJR vQIg== X-Gm-Message-State: ABy/qLYCzJNMtSgF41OOudWFm4emDswXpzKX/LUosRthS0O8ummWC6nX 1WX5dxkMYEJFmyt05KRtM6grmwc3NMXfUX4VLZ6HJqH4031s3yv4YYdNKZZFrtQCFLfzhUMJ4Yx uQBmmZrUgTKMcb9eJnvQsvPs7GJQeBa5I26eqZoO1CzjXDjdil1NBpsYFmSFl/nV03E9x3BXN4g efIJgP X-Received: by 2002:a0c:8f11:0:b0:626:3a98:95a with SMTP id z17-20020a0c8f11000000b006263a98095amr5132870qvd.22.1688752433180; Fri, 07 Jul 2023 10:53:53 -0700 (PDT) X-Google-Smtp-Source: APBJJlH5+pc2vyn/AR/VoLbj992y0W/4RAYLNhRbt+2KLmtk4f8PMt/FJFo7YhBCgEnpOeKNRPkftQ== X-Received: by 2002:a0c:8f11:0:b0:626:3a98:95a with SMTP id z17-20020a0c8f11000000b006263a98095amr5132859qvd.22.1688752432912; Fri, 07 Jul 2023 10:53:52 -0700 (PDT) Received: from oak.redhat.com (c-71-206-142-238.hsd1.va.comcast.net. [71.206.142.238]) by smtp.gmail.com with ESMTPSA id g6-20020a0ce4c6000000b0063642bcc5e4sm2318584qvm.9.2023.07.07.10.53.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jul 2023 10:53:52 -0700 (PDT) From: Joe Simmons-Talbott To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH] fxprintf: Get rid of alloca Date: Fri, 7 Jul 2023 13:53:49 -0400 Message-Id: <20230707175349.2096131-1-josimmon@redhat.com> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,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 List-Id: Use a scratch_buffer rather than alloca/malloc to avoid potential stack overflow. --- stdio-common/fxprintf.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/stdio-common/fxprintf.c b/stdio-common/fxprintf.c index f0ac9654ab..88501ab61f 100644 --- a/stdio-common/fxprintf.c +++ b/stdio-common/fxprintf.c @@ -15,6 +15,7 @@ License along with the GNU C Library; if not, see . */ +#include #include #include #include @@ -34,20 +35,18 @@ locked_vfxprintf (FILE *fp, const char *fmt, va_list ap, wchar_t *wfmt; mbstate_t mbstate; int res; - int used_malloc = 0; size_t len = strlen (fmt) + 1; + struct scratch_buffer buf; + scratch_buffer_init (&buf); if (__glibc_unlikely (len > SIZE_MAX / sizeof (wchar_t))) { __set_errno (EOVERFLOW); return -1; } - if (__libc_use_alloca (len * sizeof (wchar_t))) - wfmt = alloca (len * sizeof (wchar_t)); - else if ((wfmt = malloc (len * sizeof (wchar_t))) == NULL) + if (!scratch_buffer_set_array_size (&buf, sizeof (wchar_t), len)) return -1; - else - used_malloc = 1; + wfmt = buf.data; memset (&mbstate, 0, sizeof mbstate); res = __mbsrtowcs (wfmt, &fmt, len, &mbstate); @@ -55,8 +54,7 @@ locked_vfxprintf (FILE *fp, const char *fmt, va_list ap, if (res != -1) res = __vfwprintf_internal (fp, wfmt, ap, mode_flags); - if (used_malloc) - free (wfmt); + scratch_buffer_free (&buf); return res; } -- 2.39.2