[PATCH 1/2] telemetry: use malloc instead of variable length array

Stephen Hemminger stephen at networkplumber.org
Mon Apr 3 22:19:12 CEST 2023


On Mon,  3 Apr 2023 09:30:23 -0700
Tyler Retzlaff <roretzla at linux.microsoft.com> wrote:

>  __json_snprintf(char *buf, const int len, const char *format, ...)
>  {
> -	char tmp[len];
> +	char *tmp = malloc(len);
>  	va_list ap;
> -	int ret;
> +	int ret = 0;
> +
> +	if (tmp == NULL)
> +		return ret;
>  
>  	va_start(ap, format);
>  	ret = vsnprintf(tmp, sizeof(tmp), format, ap);
>  	va_end(ap);
>  	if (ret > 0 && ret < (int)sizeof(tmp) && ret < len) {
>  		strcpy(buf, tmp);
> -		return ret;
>  	}
> -	return 0; /* nothing written or modified */
> +
> +	free(tmp);
> +
> +	return ret;
>  }

Not sure why it needs a tmp buffer anyway?


More information about the dev mailing list