diff mbox

lib: fwts_stringextras: free original string on failed realloc

Message ID 20170413094811.27354-1-colin.king@canonical.com
State Superseded
Headers show

Commit Message

Colin Ian King April 13, 2017, 9:48 a.m. UTC
From: Colin Ian King <colin.king@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@canonical.com>
---
 src/lib/src/fwts_stringextras.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

Comments

Colin Ian King April 13, 2017, 9:59 a.m. UTC | #1
On 13/04/17 10:48, Colin King wrote:
> From: Colin Ian King <colin.king@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@canonical.com>
> ---
>  src/lib/src/fwts_stringextras.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/src/lib/src/fwts_stringextras.c b/src/lib/src/fwts_stringextras.c
> index f63434b7..bbf35d62 100644
> --- a/src/lib/src/fwts_stringextras.c
> +++ b/src/lib/src/fwts_stringextras.c
> @@ -58,8 +58,13 @@ 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;
> +		}

Forgot to assign orig.

>  		strcat(orig, newstr);
>  	} else {
>  		if ((orig = malloc(newlen + 1)) == NULL)
>
Ivan Hu April 21, 2017, 2:25 a.m. UTC | #2
On 04/13/2017 05:48 PM, Colin King wrote:
> From: Colin Ian King <colin.king@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@canonical.com>
> ---
>  src/lib/src/fwts_stringextras.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/src/lib/src/fwts_stringextras.c b/src/lib/src/fwts_stringextras.c
> index f63434b7..bbf35d62 100644
> --- a/src/lib/src/fwts_stringextras.c
> +++ b/src/lib/src/fwts_stringextras.c
> @@ -58,8 +58,13 @@ 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;
> +		}
>  		strcat(orig, newstr);
>  	} else {
>  		if ((orig = malloc(newlen + 1)) == NULL)
>

Acked-by: Ivan Hu <ivan.hu@canonical.com>
Ivan Hu April 21, 2017, 2:30 a.m. UTC | #3
On 04/13/2017 05:48 PM, Colin King wrote:
> From: Colin Ian King <colin.king@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@canonical.com>
> ---
>  src/lib/src/fwts_stringextras.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/src/lib/src/fwts_stringextras.c b/src/lib/src/fwts_stringextras.c
> index f63434b7..bbf35d62 100644
> --- a/src/lib/src/fwts_stringextras.c
> +++ b/src/lib/src/fwts_stringextras.c
> @@ -58,8 +58,13 @@ 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;
> +		}
>  		strcat(orig, newstr);
>  	} else {
>  		if ((orig = malloc(newlen + 1)) == NULL)
>

Acked-by: Ivan Hu <ivan.hu@canonical.com>
diff mbox

Patch

diff --git a/src/lib/src/fwts_stringextras.c b/src/lib/src/fwts_stringextras.c
index f63434b7..bbf35d62 100644
--- a/src/lib/src/fwts_stringextras.c
+++ b/src/lib/src/fwts_stringextras.c
@@ -58,8 +58,13 @@  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;
+		}
 		strcat(orig, newstr);
 	} else {
 		if ((orig = malloc(newlen + 1)) == NULL)