From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by sourceware.org (Postfix) with ESMTPS id 435903858C66 for ; Wed, 10 May 2023 10:40:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 435903858C66 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wr1-x430.google.com with SMTP id ffacd0b85a97d-3078cc99232so4043609f8f.3 for ; Wed, 10 May 2023 03:40:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1683715254; x=1686307254; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :sender:from:to:cc:subject:date:message-id:reply-to; bh=EOKK3i+5wKwNHwn89Tt9NCwgOOQbojrNchTjaqwKUDM=; b=YJccYDq4rUWkvZZ9OVd0rUteZTZNW465hn1Teb7PGzEq7z1cSPj7ZqgtnLAO61nfdj U/EasU0FtjJqGwTaNd2cRb+ZnAXOwR8RlPuZHmGDyiPpLQKXKt4SziXX9Dyx5FYcDTjA Byx1MSoJlBZqMV59U4G/bkYbQG+/uwlpJWdFv89aPYIpzbjesXJ9PZGs4J7wwxYIeLAg 7LoJpUmPS/qTXONs+gevkNaQNS6IGLM91oOo4D4mzCm1Q0FqzshEqgUQmPuyHg/W+4hy 5C0NDQrzGPu+ujWv52eztIeyC68Qtkj1Lhf/n9+ciCbOQirSvfIdDmmnKAaC6ooVbnFc MWlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683715254; x=1686307254; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :sender:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=EOKK3i+5wKwNHwn89Tt9NCwgOOQbojrNchTjaqwKUDM=; b=ZNmPdH7riUcgu7qUkrcCFNP4K7HV5aWhfYvoKiFAAIwz35LA2qLbLk4R4rBm1Cjhgm 9yKpmyzkAN8drSiRNSgXqAxhPOlf151DKlWutkb/cMpUMWuEaVdI/Q01Dn0Dmb/I2boF tZlu93AtRlOqmkZvIaB2c01wUa8ef5/E8pE99yb6vRvXnkzdTcKBb22uuqsA61mzw2WR mTYlUDmp4M1agPNh48glkRbE8W4tFkSA6qq36oj3TCL3Jj55Y0rvyD+eXd2VLHxlO419 esiEe8tfYWqrouziV8KWcqxSekJL863sx1jdzboasrffXocJs8TXeGlAu6cWYVh/laJm ONkA== X-Gm-Message-State: AC+VfDx5Oytd68lIOCNO0+ZMnAlbc2DmNTLrw3KT2FfvBzLx1mab6X/O P7w/QrZugL1JdA+NHGuLByYeETa4fdk= X-Google-Smtp-Source: ACHHUZ6BRNZiGN6I5LCO5ge5tCNYICOOLOiAJMwFJr7RW4ah9JVa4U8nWvt5tQnij1iSwqC3FeXc1w== X-Received: by 2002:adf:fb0b:0:b0:2cd:bc79:5444 with SMTP id c11-20020adffb0b000000b002cdbc795444mr12460653wrr.2.1683715253992; Wed, 10 May 2023 03:40:53 -0700 (PDT) Received: from [192.168.0.160] ([170.253.51.134]) by smtp.gmail.com with ESMTPSA id n23-20020a1c7217000000b003f4ad703aecsm1156321wmc.15.2023.05.10.03.40.53 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 10 May 2023 03:40:53 -0700 (PDT) Sender: Alejandro Colomar Message-ID: <51e9922f-4b0d-7ae9-ce74-9a3b1dc5c08a@gmail.com> Date: Wed, 10 May 2023 12:40:52 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.10.0 Subject: Re: [PATCH] vfprintf-internal: Replace alloca with malloc. Content-Language: en-US To: Joe Simmons-Talbott , libc-alpha@sourceware.org References: <20230509174329.1959380-1-josimmon@redhat.com> From: Alejandro Colomar In-Reply-To: <20230509174329.1959380-1-josimmon@redhat.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,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: Hi Joe, On 5/9/23 19:43, Joe Simmons-Talbott via Libc-alpha wrote: > Avoid potential stack overflow from unbounded alloca. > --- > stdio-common/vfprintf-internal.c | 13 ++++++++++++- > 1 file changed, 12 insertions(+), 1 deletion(-) > > diff --git a/stdio-common/vfprintf-internal.c b/stdio-common/vfprintf-internal.c > index c76c06e49b..93f327c6b4 100644 > --- a/stdio-common/vfprintf-internal.c > +++ b/stdio-common/vfprintf-internal.c > @@ -1001,6 +1001,7 @@ printf_positional (struct Xprintf_buffer * buf, const CHAR_T *format, > scratch_buffer_init (&specsbuf); > struct printf_spec *specs = specsbuf.data; > size_t specs_limit = specsbuf.length / sizeof (specs[0]); > + bool malloced_pa_user = false; > > /* Used as a backing store for args_value, args_size, args_type > below. */ > @@ -1171,7 +1172,10 @@ printf_positional (struct Xprintf_buffer * buf, const CHAR_T *format, > else if (__glibc_unlikely (__printf_va_arg_table != NULL) > && __printf_va_arg_table[args_type[cnt] - PA_LAST] != NULL) > { > - args_value[cnt].pa_user = alloca (args_size[cnt]); > + args_value[cnt].pa_user = malloc (args_size[cnt]); > + if (args_value[cnt].pa_user == NULL) > + break; > + malloced_pa_user = true; > (*__printf_va_arg_table[args_type[cnt] - PA_LAST]) > (args_value[cnt].pa_user, ap_savep); > } > @@ -1334,6 +1338,13 @@ printf_positional (struct Xprintf_buffer * buf, const CHAR_T *format, > (specs[nspecs_done].next_fmt > - specs[nspecs_done].end_of_fmt)); > } > + if (malloced_pa_user) > + for (cnt = 0; cnt < nargs; ++cnt) > + { > + if (args_value[cnt].pa_user != NULL) > + free (args_value[cnt].pa_user); free(NULL) is a no-op since at least C89. The test is redundant. Cheers, Alex > + } > + > all_done: > scratch_buffer_free (&argsbuf); > scratch_buffer_free (&specsbuf);