[Mesa-dev] [PATCH 1/2] util: add util_vasprintf() for Windows

Nicolai Hähnle nhaehnle at gmail.com
Sat Sep 9 11:46:50 UTC 2017


On 09.09.2017 00:55, Brian Paul wrote:
> We don't have vasprintf() on Windows so we need to implement it ourselves.
> Since we don't know the length of the final string, take a guess at 10000
> chars.
> ---
>   src/util/u_string.h | 11 +++++++++++
>   1 file changed, 11 insertions(+)
> 
> diff --git a/src/util/u_string.h b/src/util/u_string.h
> index e88e13f..d787cd3 100644
> --- a/src/util/u_string.h
> +++ b/src/util/u_string.h
> @@ -110,6 +110,16 @@ util_sprintf(char *str, const char *format, ...)
>      va_end(ap);
>   }
>   
> +static inline int
> +util_vasprintf(char **ret, const char *format, va_list ap)
> +{
> +   /* Just allocate 10000 bytes as a guess */
 > +   *ret = (char *) malloc(10000);

I'd suggest something like the following instead (untested):

    va_list ap_copy;
    va_copy(ap_copy, ap);
    int r = util_vsnprintf(NULL, 0, format, ap);
    va_end(ap_copy);

    if (r < 0)
       return -1;

    *ret = (char *) malloc(r + 1);
    if (!ret)
       return -1;

    return util_vsnprintf(*ret, r + 1, format, ap);

Cheers,
Nicolai

> +   if (!ret)
> +      return -1;
> +   return util_vsnprintf(*ret, 10000, format, ap);
> +}
> +
>   static inline char *
>   util_strchr(const char *s, char c)
>   {
> @@ -186,6 +196,7 @@ util_strstr(const char *haystack, const char *needle)
>   #define util_vsnprintf vsnprintf
>   #define util_snprintf snprintf
>   #define util_vsprintf vsprintf
> +#define util_vasprintf vasprintf
>   #define util_sprintf sprintf
>   #define util_strchr strchr
>   #define util_strcmp strcmp
> 


-- 
Lerne, wie die Welt wirklich ist,
Aber vergiss niemals, wie sie sein sollte.


More information about the mesa-dev mailing list