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.133.124]) by sourceware.org (Postfix) with ESMTPS id 5A3A03856089 for ; Fri, 9 Jun 2023 14:59:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5A3A03856089 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=1686322775; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Fo8Ch2Uizo60TvDhWQktAHSSrcOOo/fMIMcIrLdKvDk=; b=exnL9RAgGZ5Jd9p+IyibfFsP5uqk/CIsV9NZzxF0U94IZY3iLgOKD5VqXQxiXGAtNQXnzK nsD6+tTi4xX9SqrAgPooa5soacMvAJWBTTVtdXHsctEr82mjFIBhIyNV2AtbcCD9zr6grq jLDF4mO/V8fYIKKSYqwaMa7bUmQHB6A= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-460-RKLBTfJSOBOcbw9n2N3llQ-1; Fri, 09 Jun 2023 10:59:33 -0400 X-MC-Unique: RKLBTfJSOBOcbw9n2N3llQ-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A181E3C14AAB for ; Fri, 9 Jun 2023 14:59:33 +0000 (UTC) Received: from oak (unknown [10.22.10.124]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8BCCB20268C6 for ; Fri, 9 Jun 2023 14:59:33 +0000 (UTC) Date: Fri, 9 Jun 2023 10:59:32 -0400 From: Joe Simmons-Talbott To: libc-alpha@sourceware.org Subject: Re: [PATCH v3] vfprintf-internal: Get rid of alloca. Message-ID: <20230609145932.GX176347@oak> References: <20230522170627.239536-1-josimmon@redhat.com> <20230601133428.GR176347@oak> MIME-Version: 1.0 In-Reply-To: <20230601133428.GR176347@oak> X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H5,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: On Thu, Jun 01, 2023 at 09:34:28AM -0400, Joe Simmons-Talbott via Libc-alpha wrote: > On Mon, May 22, 2023 at 01:06:27PM -0400, Joe Simmons-Talbott wrote: > > Avoid potential stack overflow from unbounded alloca. Use the existing > > scratch_buffer instead. > > Ping. x2 Thanks, Joe > > Thanks, > Joe > > --- > > Changes to v2: > > * Don't assume the first scratch buffer resize adds enough space. > > > > stdio-common/vfprintf-internal.c | 23 ++++++++++++++++++++++- > > 1 file changed, 22 insertions(+), 1 deletion(-) > > > > diff --git a/stdio-common/vfprintf-internal.c b/stdio-common/vfprintf-internal.c > > index c76c06e49b..20fd3c3043 100644 > > --- a/stdio-common/vfprintf-internal.c > > +++ b/stdio-common/vfprintf-internal.c > > @@ -1066,6 +1066,8 @@ printf_positional (struct Xprintf_buffer * buf, const CHAR_T *format, > > union printf_arg *args_value; > > int *args_size; > > int *args_type; > > + int *args_pa_user; > > + size_t args_pa_user_offset; > > { > > /* Calculate total size needed to represent a single argument > > across all three argument-related arrays. */ > > @@ -1082,6 +1084,7 @@ printf_positional (struct Xprintf_buffer * buf, const CHAR_T *format, > > now. */ > > args_size = &args_value[nargs].pa_int; > > args_type = &args_size[nargs]; > > + args_pa_user = &args_type[nargs]; > > memset (args_type, (mode_flags & PRINTF_FORTIFY) != 0 ? '\xff' : '\0', > > nargs * sizeof (*args_type)); > > } > > @@ -1171,7 +1174,25 @@ 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]); > > + while (args_pa_user + args_size[cnt] > > > + (int *) &argsbuf + argsbuf.length) > > + { > > + args_pa_user_offset = args_pa_user - &args_type[nargs]; > > + if (!scratch_buffer_grow_preserve (&argsbuf)) > > + { > > + Xprintf_buffer_mark_failed (buf); > > + goto all_done; > > + } > > + args_value = argsbuf.data; > > + /* Set up the remaining two arrays to each point past the end of > > + the prior array, since space for all three has been allocated > > + now. */ > > + args_size = &args_value[nargs].pa_int; > > + args_type = &args_size[nargs]; > > + args_pa_user = &args_type[nargs] + args_pa_user_offset; > > + } > > + args_value[cnt].pa_user = args_pa_user; > > + args_pa_user += args_size[cnt]; > > (*__printf_va_arg_table[args_type[cnt] - PA_LAST]) > > (args_value[cnt].pa_user, ap_savep); > > } > > -- > > 2.39.2 > > >