Hello,

Gianluca Cannata, le sam. 24 janv. 2026 15:26:53 +0100, a ecrit:
> -error_t lookup_host (const char *host, struct sockaddr_storage *dest, 
> socklen_t *dest_len) 
> +error_t lookup_host (const char *host, struct sockaddr_storage *dest, 
> socklen_t *dest_len, int *socktype, int *protocol) 
>  {
>         struct addrinfo hints;
>         struct addrinfo *res;
>         int s;
> -
> -       /* Buffer that holds a clean host, that is a host without square 
> brackets in case of a IPv6 address */
> -       char clean_host[256];
> -       const char *host_ptr = host;
> +       char *allocated_host = NULL;
>  
>         if (!host || !dest || !dest_len)
>                 return EINVAL;
> @@ -52,18 +49,25 @@ error_t lookup_host (const char *host, s
>          * because getaddrinfo does not accept square brackets, but URLs do.
>          */
>         if (host[0] == '[') {
> -               const char *end = strchr (host, ']');
> +               size_t host_len = strlen (host);
> +
> +               /* If it starts with a [, it must also ends with a ] */
> +               if (host[host_len - 1] != ']')
> +                       return EINVAL;
> +
> +               /* We allocate only the necessary memory with a VLA (C99) */
> +               size_t clean_len = host_len - 2;
> +               char clean_host[clean_len + 1];

This is unused, since you just strdup below, which is fine:

> +
> +               /* Copy the content between square brackets */
> +               allocated_host = strndup (host + 1, host_len - 2);
> +               if (!allocated_host)
> +                       return ENOMEM;
> +
> +               host = allocated_host;
>         }

Please send a patch with the complete changes, so I can apply it in one
go.

Samuel

Reply via email to