diff mbox

uefi: uefidump: free original string on failed realloc

Message ID 20170413100834.7458-1-colin.king@canonical.com
State Accepted
Headers show

Commit Message

Colin Ian King April 13, 2017, 10:08 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/uefi/uefidump/uefidump.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

Comments

Alex Hung April 17, 2017, 5:26 p.m. UTC | #1
On 2017-04-13 03:08 AM, 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/uefi/uefidump/uefidump.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/src/uefi/uefidump/uefidump.c b/src/uefi/uefidump/uefidump.c
> index 90556204..305d2d6a 100644
> --- a/src/uefi/uefidump/uefidump.c
> +++ b/src/uefi/uefidump/uefidump.c
> @@ -83,9 +83,13 @@ static char *uefidump_vprintf(char *str, const char *fmt, ...)
>  	if (str == NULL)
>  		str = strdup(buffer);
>  	else {
> -		str = realloc(str, strlen(str) + strlen(buffer) + 1);
> -		if (str == NULL)
> +		char *tmp;
> +		tmp = realloc(str, strlen(str) + strlen(buffer) + 1);
> +		if (!tmp) {
> +			free(str);
>  			return NULL;
> +		}
> +		str = tmp;
>  		strcat(str, buffer);
>  	}
>
>


Acked-by: Alex Hung <alex.hung@canonical.com>
Ivan Hu April 21, 2017, 2:40 a.m. UTC | #2
On 04/13/2017 06:08 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/uefi/uefidump/uefidump.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/src/uefi/uefidump/uefidump.c b/src/uefi/uefidump/uefidump.c
> index 90556204..305d2d6a 100644
> --- a/src/uefi/uefidump/uefidump.c
> +++ b/src/uefi/uefidump/uefidump.c
> @@ -83,9 +83,13 @@ static char *uefidump_vprintf(char *str, const char *fmt, ...)
>  	if (str == NULL)
>  		str = strdup(buffer);
>  	else {
> -		str = realloc(str, strlen(str) + strlen(buffer) + 1);
> -		if (str == NULL)
> +		char *tmp;
> +		tmp = realloc(str, strlen(str) + strlen(buffer) + 1);
> +		if (!tmp) {
> +			free(str);
>  			return NULL;
> +		}
> +		str = tmp;
>  		strcat(str, buffer);
>  	}
>
>

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

Patch

diff --git a/src/uefi/uefidump/uefidump.c b/src/uefi/uefidump/uefidump.c
index 90556204..305d2d6a 100644
--- a/src/uefi/uefidump/uefidump.c
+++ b/src/uefi/uefidump/uefidump.c
@@ -83,9 +83,13 @@  static char *uefidump_vprintf(char *str, const char *fmt, ...)
 	if (str == NULL)
 		str = strdup(buffer);
 	else {
-		str = realloc(str, strlen(str) + strlen(buffer) + 1);
-		if (str == NULL)
+		char *tmp;
+		tmp = realloc(str, strlen(str) + strlen(buffer) + 1);
+		if (!tmp) {
+			free(str);
 			return NULL;
+		}
+		str = tmp;
 		strcat(str, buffer);
 	}