From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7932) id 20F10385843A; Wed, 6 Sep 2023 13:33:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 20F10385843A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1694007230; bh=m18pNVAxviu0+tKA/Ooea67B5W7TsUcJLtmGzFdpx3Y=; h=From:To:Subject:Date:From; b=Y0AJspnPfSacGCX5K3C0OKzjWtkDCyBeoDQ5npU5iJjUJUaB7xXSSU6U3HoBc7Hoy Ji8HUAzlsrsHTP+oJXFFDcwKicZHkrax3hZLFlkP56SDBdQSeH7egYauy2uXJWfQxZ Yfqqa9T0oQpvOOseAd6yOY1eqYZ4qCMt+n3BxqfU= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Joe Simmons-Talbott To: glibc-cvs@sourceware.org Subject: [glibc] getaddrinfo: Get rid of alloca X-Act-Checkin: glibc X-Git-Author: Joe Simmons-Talbott X-Git-Refname: refs/heads/master X-Git-Oldrev: 3d6fcf1bd7f462d333c36a14efc0e03f2fdd3f9e X-Git-Newrev: 955a47a4bf517ac17d24829547bafd2a79e584e1 Message-Id: <20230906133350.20F10385843A@sourceware.org> Date: Wed, 6 Sep 2023 13:33:50 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=955a47a4bf517ac17d24829547bafd2a79e584e1 commit 955a47a4bf517ac17d24829547bafd2a79e584e1 Author: Joe Simmons-Talbott Date: Wed Sep 6 13:32:46 2023 +0000 getaddrinfo: Get rid of alloca Use a scratch_buffer rather than alloca to avoid potential stack overflow. Diff: --- sysdeps/posix/getaddrinfo.c | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/sysdeps/posix/getaddrinfo.c b/sysdeps/posix/getaddrinfo.c index d17b97d79a..6ae6744fe4 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, nresults, + sizeof (*results) + sizeof (size_t))) { - 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);