[Mesa-dev] [PATCH 06/12] ralloc: Use strnlen() inside of strncat()

Ian Romanick idr at freedesktop.org
Wed Jan 18 23:37:51 UTC 2017


On 01/07/2017 11:02 AM, Vladislav Egorov wrote:
> If the str is long or isn't null-terminated, strlen() could take a lot
> of time or even crash. I don't know why was it used in the first place,
> maybe for platforms without strnlen(), but strnlen() is already used
> inside of ralloc_strndup(), so this change should not additionally
> break anything.

I don't know why, but it took a long time for me to convince myself that
the value passed to cat() was ultimately the same.  I really wish the
original code had just been

   n = MIN2(n, strlen(str));

This patch is

Reviewed-by: Ian Romanick <ian.d.romanick at intel.com>

> ---
>  src/util/ralloc.c | 7 +------
>  1 file changed, 1 insertion(+), 6 deletions(-)
> 
> diff --git a/src/util/ralloc.c b/src/util/ralloc.c
> index df2b1d8..08cb989 100644
> --- a/src/util/ralloc.c
> +++ b/src/util/ralloc.c
> @@ -410,12 +410,7 @@ ralloc_strcat(char **dest, const char *str)
>  bool
>  ralloc_strncat(char **dest, const char *str, size_t n)
>  {
> -   /* Clamp n to the string length */
> -   size_t str_length = strlen(str);
> -   if (str_length < n)
> -      n = str_length;
> -
> -   return cat(dest, str, n);
> +   return cat(dest, str, strnlen(str, n));
>  }
>  
>  char *
> 



More information about the mesa-dev mailing list