[PATCH] check_native: Get rid of alloca
Joe Simmons-Talbott
josimmon@redhat.com
Wed May 31 20:35:11 GMT 2023
On Wed, May 31, 2023 at 09:01:22AM -0400, Joe Simmons-Talbott wrote:
> Use malloc rather than alloca to avoid potential stack overflow.
This is failing on 32bit builds[1] and I'm not sure how to fix it or why
it's failing here but didn't with the same construct in my ifaddrs patch
[2]
Thanks,
Joe
[1] https://www.delorie.com/trybots/32bit/20795/make.tail.txt
[2] https://sourceware.org/pipermail/libc-alpha/2023-May/148681.html
> ---
> sysdeps/unix/sysv/linux/check_native.c | 27 +++++++++-----------------
> 1 file changed, 9 insertions(+), 18 deletions(-)
>
> diff --git a/sysdeps/unix/sysv/linux/check_native.c b/sysdeps/unix/sysv/linux/check_native.c
> index 34876ca624..45b328f240 100644
> --- a/sysdeps/unix/sysv/linux/check_native.c
> +++ b/sysdeps/unix/sysv/linux/check_native.c
> @@ -37,6 +37,12 @@
>
> #include "netlinkaccess.h"
>
> +static void
> +ifree (char **ptr)
> +{
> + free (*ptr);
> +}
> +
> void
> __check_native (uint32_t a1_index, int *a1_native,
> uint32_t a2_index, int *a2_native)
> @@ -48,7 +54,6 @@ __check_native (uint32_t a1_index, int *a1_native,
> nladdr.nl_family = AF_NETLINK;
>
> socklen_t addr_len = sizeof (nladdr);
> - bool use_malloc = false;
>
> if (fd < 0)
> return;
> @@ -82,24 +87,13 @@ __check_native (uint32_t a1_index, int *a1_native,
> nladdr.nl_family = AF_NETLINK;
>
> #ifdef PAGE_SIZE
> - /* Help the compiler optimize out the malloc call if PAGE_SIZE
> - is constant and smaller or equal to PTHREAD_STACK_MIN/4. */
> const size_t buf_size = PAGE_SIZE;
> #else
> const size_t buf_size = __getpagesize ();
> #endif
> - char *buf;
> -
> - if (__libc_use_alloca (buf_size))
> - buf = alloca (buf_size);
> - else
> - {
> - buf = malloc (buf_size);
> - if (buf != NULL)
> - use_malloc = true;
> - else
> - goto out;
> - }
> + char *buf __attribute__ ((__cleanup__ (ifree))) = malloc (buf_size);
> + if (buf == NULL)
> + goto out;
>
> struct iovec iov = { buf, buf_size };
>
> @@ -170,7 +164,4 @@ __check_native (uint32_t a1_index, int *a1_native,
>
> out:
> __close_nocancel_nostatus (fd);
> -
> - if (use_malloc)
> - free (buf);
> }
> --
> 2.39.2
>
More information about the Libc-alpha
mailing list