[PATCH][V2] fwts_stringextras: free original string on failed realloc

Alex Hung alex.hung at canonical.com
Mon Apr 17 17:23:02 UTC 2017


On 2017-04-13 03:01 AM, Colin King wrote:
> From: Colin Ian King <colin.king at canonical.com>
>
> A common bug is where realloc fails to allocate and we assume that
> the memory being realloc'd was freed. This is not the case, the
> NULL return means we need to free the original string to avoid
> a memory leak.
>
> Signed-off-by: Colin Ian King <colin.king at canonical.com>
> ---
>  src/lib/src/fwts_stringextras.c | 8 +++++++-
>  1 file changed, 7 insertions(+), 1 deletion(-)
>
> diff --git a/src/lib/src/fwts_stringextras.c b/src/lib/src/fwts_stringextras.c
> index f63434b7..f461d030 100644
> --- a/src/lib/src/fwts_stringextras.c
> +++ b/src/lib/src/fwts_stringextras.c
> @@ -58,8 +58,14 @@ char *fwts_realloc_strcat(char *orig, const char *newstr)
>  	size_t newlen = strlen(newstr);
>
>  	if (orig) {
> -		if ((orig = realloc(orig, strlen(orig) + newlen + 1)) == NULL)
> +		char *tmp;
> +
> +		tmp = realloc(orig, strlen(orig) + newlen + 1);
> +		if (!tmp) {
> +			free(orig);
>  			return NULL;
> +		}
> +		orig = tmp;
>  		strcat(orig, newstr);
>  	} else {
>  		if ((orig = malloc(newlen + 1)) == NULL)
>

the "lib:" is missing in the subject, but this can be fixed when 
applying the patch.

Acked-by: Alex Hung <alex.hung at canonical.com>



More information about the fwts-devel mailing list