From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oi1-x235.google.com (mail-oi1-x235.google.com [IPv6:2607:f8b0:4864:20::235]) by sourceware.org (Postfix) with ESMTPS id 112333864877 for ; Fri, 30 Jun 2023 13:15:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 112333864877 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-oi1-x235.google.com with SMTP id 5614622812f47-3a1d9b64837so1384448b6e.0 for ; Fri, 30 Jun 2023 06:15:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1688130951; x=1690722951; h=content-transfer-encoding:in-reply-to:organization:from:references :to:content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=EBQkUQFwOhp1Hbi6LkZ3k+DY50Y04gLb/mVJoGZ+8gA=; b=e1zY/s3pxSGEQYoy8OMMR9An6XNAEVrTHo0NNtp83fvYXxQbcVOf4b/TD6FmQUGdfd H+4wPS0Yfw+RqKv+E1mazcSYbcjyjoBBOt9N1XQPaJaDMnu1gZ4u6Dz0tZsH67uEv307 OyCL878m3vFyi7Yy6Qboa9/IO+4iNBw2DjD+Ai2EGADZlN8WhLz7US9s+9e1P6KLTJ+F utfq0vPTUJMWJPvs7QcVOsjE0KdHah27XcjaZEJJm9XQ8QLnfJ/YJRz/H89D2rbPTHiN Yp4zDUFO7+v9SLxGyd8O7TRiB45CMDxGdyakHajslwWv5q+B61tiK3+bCWKLOWfFL+hj xIpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688130951; x=1690722951; h=content-transfer-encoding:in-reply-to:organization:from:references :to:content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=EBQkUQFwOhp1Hbi6LkZ3k+DY50Y04gLb/mVJoGZ+8gA=; b=l2F3wEBIvmoEtud9m3pz029/JFEH6XIe1Wdvg2e7K63qtxfAhK5Df+MMdOL7gpJvi/ Jo5c1j6aN1HSGr/Qkk8REamZnORz+DeHX7sZA4YCxUbiy9BfNr484lFb0Er09gMUCQR9 Wzx/7IiYXV98KL5sbwihTit6ehQeKgxRyKknFuxmVAzthdpQZleYKXgOVl1jvBbzE8Ut J6f0qmaan1isk8AZoE9IyqDmXMK1mE4TbI8T/3s1vEf0gUAV5aCS3EXU2EDJzPXK7/ry sw3kXaVizHPmklKt5e0XI2R3jXl7Xz5+Sjs+R9MZbPNINuf2AWLuzuRYBzTiyBtc+Q6S qM8Q== X-Gm-Message-State: AC+VfDzdzxpR8bPMEZRMzrCD9IUjfPTIU0Ufa+pFbsFBO3sU8u3KRKQd CDpd44etiPDETIrpXfjKVgg3N5gKyD8+7WntqiHpqw== X-Google-Smtp-Source: ACHHUZ4AWHrv5cmKesz0JCDmW9j9irBRVQhGGG9Ql36H+B0rFymfncDfszhJ+4SICuKR31UECRShww== X-Received: by 2002:a05:6808:30a2:b0:3a1:ec14:cd7e with SMTP id bl34-20020a05680830a200b003a1ec14cd7emr2861283oib.36.1688130951366; Fri, 30 Jun 2023 06:15:51 -0700 (PDT) Received: from ?IPV6:2804:1b3:a7c3:665c:f449:9fb3:d8c6:9093? ([2804:1b3:a7c3:665c:f449:9fb3:d8c6:9093]) by smtp.gmail.com with ESMTPSA id bl26-20020a056808309a00b003a1e38362b7sm4778106oib.25.2023.06.30.06.15.49 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 30 Jun 2023 06:15:50 -0700 (PDT) Message-ID: Date: Fri, 30 Jun 2023 10:15:48 -0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.12.0 Subject: Re: [PATCH] getaddrinfo: Get rid of alloca Content-Language: en-US To: Joe Simmons-Talbott , libc-alpha@sourceware.org References: <20230620214031.1241057-1-josimmon@redhat.com> From: Adhemerval Zanella Netto Organization: Linaro In-Reply-To: <20230620214031.1241057-1-josimmon@redhat.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-12.9 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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: On 20/06/23 18:40, Joe Simmons-Talbott via Libc-alpha wrote: > Use a scratch_buffer rather than alloca to avoid potential stack > overflow. > --- > sysdeps/posix/getaddrinfo.c | 22 ++++++++-------------- > 1 file changed, 8 insertions(+), 14 deletions(-) > > diff --git a/sysdeps/posix/getaddrinfo.c b/sysdeps/posix/getaddrinfo.c > index 0356b622be..442475d621 100644 > --- a/sysdeps/posix/getaddrinfo.c > +++ b/sysdeps/posix/getaddrinfo.c > @@ -2404,22 +2404,17 @@ getaddrinfo (const char *name, const char *service, > struct addrinfo *q; > struct addrinfo *last = NULL; > char *canonname = NULL; > - bool malloc_results; > size_t alloc_size = nresults * (sizeof (*results) + sizeof (size_t)); > + struct scratch_buffer buf; > + scratch_buffer_init (&buf); > > - malloc_results > - = !__libc_use_alloca (alloc_size); > - if (malloc_results) > + if (!scratch_buffer_set_array_size (&buf, 1, alloc_size)) I think it would be better to use: if (!scratch_buffer_set_array_size (&buf, nresults, sizeof (*results) + sizeof (size_t))) [...] To use the overflow checks done by scratch_buffer_set_array_size ( sizeof (*results) + sizeof (size_t) is always safe so I think there is no need to add extra checks). > { > - results = malloc (alloc_size); > - if (results == NULL) > - { > - __free_in6ai (in6ai); > - return EAI_MEMORY; > - } > + __free_in6ai (in6ai); > + return EAI_MEMORY; > } > - else > - results = alloca (alloc_size); > + results = buf.data; > + > order = (size_t *) (results + nresults); > > /* Now we definitely need the interface information. */ > @@ -2590,8 +2585,7 @@ getaddrinfo (const char *name, const char *service, > /* Fill in the canonical name into the new first entry. */ > p->ai_canonname = canonname; > > - if (malloc_results) > - free (results); > + scratch_buffer_free (&buf); > } > > __free_in6ai (in6ai);