From patchwork Wed Jun 14 18:38:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joe Simmons-Talbott X-Patchwork-Id: 1795063 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=FDa9EHng; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QhDhr3wsZz20Vx for ; Thu, 15 Jun 2023 04:38:52 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6BE813858438 for ; Wed, 14 Jun 2023 18:38:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6BE813858438 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1686767930; bh=lGiuzYZZTd3qJ08hsbWqMBvtIb3qxLUbnCF5E2rkZH0=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=FDa9EHngUTRYITJ8btGiD8+kgYpo/EapM0tNkdT+HyCrsbBMI4Rjs3T48cvwTOMvt CLNlkKp2oZt5brHOXjszOGAtDH82K60pEnJJOgvjtlJ/MZFuWFuR1lL5qPaOFSNl2F bo0HzlZbLbidDmy/3tRjglcErVVfSlQIDtxwcnqk= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org 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 745883858C33 for ; Wed, 14 Jun 2023 18:38:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 745883858C33 Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-122-RkarLdQtNz2cu5JYJoV7jA-1; Wed, 14 Jun 2023 14:38:32 -0400 X-MC-Unique: RkarLdQtNz2cu5JYJoV7jA-1 Received: by mail-qt1-f198.google.com with SMTP id d75a77b69052e-3f9b1f43c70so32785911cf.3 for ; Wed, 14 Jun 2023 11:38:32 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686767911; x=1689359911; 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=lGiuzYZZTd3qJ08hsbWqMBvtIb3qxLUbnCF5E2rkZH0=; b=kAFVOuuOPSUVirSD2KRvjml1YeMKRJsdWsF+UmbXgN5+YY9l06wec7JuvUQ4EFmaFk 3lVOyVwqMXbpcykgT3cIdyjbCwaxWzmMzHFSZXkJsrqesHNo3JG55RltC3SDyAMIfbvv 6UUzm8aKjJFNTHIYwbhulOb6wkQLLU72bkvMAqYFUyxkwxrF9pxW6/EeUawpFqEdQmIO HPl4FMh1Wt1HDbxAHu8mKbfNfNbMe+6Tto4XbfZgs+jbqbFX6yv/dDmqT7wCPuNZUor1 bkR5rAqdmj33ymxO9yu0aaLUQj4lV1CtvKgA2JhNwP5xZW7VLZp5AlGFClJbGjny87/k z2PQ== X-Gm-Message-State: AC+VfDzQnq+YTXAW1QPg/cBT8sMMLNQIw1T/tfeoa1qObvhKklrLEIwz Hn6Ry1spthVN1+Q/D9obKjeeyuRb35zdAzPDZtLQUghNeGXDKJA+GGVoNQr2pU+rws5FbCIN5UL xO5zs6G+LMS0r9WNitxQdqWC+3CfOXycPRpwgh994vKhQSylL02Z8RWHW+BXO3LkzKJNU0eG083 6Qubkm X-Received: by 2002:a05:622a:413:b0:3f8:6bdc:3d4 with SMTP id n19-20020a05622a041300b003f86bdc03d4mr3702502qtx.9.1686767911040; Wed, 14 Jun 2023 11:38:31 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6RubNqC1azmsaIieoMYgSs9gxPHW4md+IUT91qnvKWyPCZWKlzw8DfJTVS4+zlD1WSZzc4ig== X-Received: by 2002:a05:622a:413:b0:3f8:6bdc:3d4 with SMTP id n19-20020a05622a041300b003f86bdc03d4mr3702478qtx.9.1686767910623; Wed, 14 Jun 2023 11:38:30 -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 fx6-20020a05622a4ac600b003f9efa2ddb4sm3038268qtb.66.2023.06.14.11.38.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jun 2023 11:38:29 -0700 (PDT) To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH v4] vfprintf-internal: Get rid of alloca. Date: Wed, 14 Jun 2023 14:38:26 -0400 Message-Id: <20230614183826.3358014-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 X-Spam-Status: No, score=-12.4 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 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Joe Simmons-Talbott via Libc-alpha From: Joe Simmons-Talbott Reply-To: Joe Simmons-Talbott Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" Avoid potential stack overflow from unbounded alloca. Use the existing scratch_buffer instead. Add a testcase to exercise the code as suggested by Adhemerval Zanella Netto. --- Changes to v3: * Add a testcase to exercise the code. * Fix arg_pa_user pointer math by using (void *) rather than (int *). stdio-common/tst-vfprintf-user-type.c | 43 +++++++++++++++++++++++++++ stdio-common/vfprintf-internal.c | 23 +++++++++++++- 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/stdio-common/tst-vfprintf-user-type.c b/stdio-common/tst-vfprintf-user-type.c index 7cc69dc716..bd2ac04351 100644 --- a/stdio-common/tst-vfprintf-user-type.c +++ b/stdio-common/tst-vfprintf-user-type.c @@ -194,6 +194,49 @@ do_test (void) TEST_COMPARE_STRING (str, "[[(123, 457.000000) (123, 457.000000)]]"); free (str); + str = NULL; + TEST_VERIFY (asprintf_alias (&str, "%1$P %2$P %3$P %4$P %5$P %6$P" + "%7$P %8$P %9$P %10$P %11$P %12$P" + "%13$P %14$P %15$P %16$P %17$P %18$P" + "%19$P %20$P %21$P %22$P %23$P %24$P" + "%25$P %26$P %27$P %28$P", /*%*29$P %30$P",*/ + 1L, 1.0, + 2L, 2.0, + 3L, 3.0, + 4L, 4.0, + 5L, 6.0, + 6L, 6.0, + 7L, 7.0, + 8L, 8.0, + 9L, 9.0, + 10L, 10.0, + 11L, 11.0, + 12L, 12.0, + 13L, 13.0, + 14L, 14.0, + 15L, 15.0, + 16L, 16.0, + 17L, 17.0, + 18L, 18.0, + 19L, 19.0, + 20L, 20.0, + 21L, 21.0, + 22L, 22.0, + 23L, 23.0, + 24L, 34.0, + 25L, 25.0, + 26L, 26.0, + 27L, 27.0, + 28L, 28.0) + >= 0); + printf ("str=%s\n", str); + free (str); + + str = NULL; + TEST_VERIFY (asprintf_alias (&str, "[[%1$P %1$P]]", 123L, 457.0) >= 0); + TEST_COMPARE_STRING (str, "[[(123, 457.000000) (123, 457.000000)]]"); + free (str); + str = NULL; TEST_VERIFY (asprintf_alias (&str, "[[%.1P]]", 1L, 2.0) >= 0); TEST_COMPARE_STRING (str, "[[{(1, 2.000000)}]]"); diff --git a/stdio-common/vfprintf-internal.c b/stdio-common/vfprintf-internal.c index c76c06e49b..b18d70d993 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; + void *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] > + argsbuf.data + argsbuf.length) + { + args_pa_user_offset = args_pa_user - (void *) &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 = (void *) &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); }