ACK: [PATCH] uefi: uefidump: free original string on failed realloc
Alex Hung
alex.hung at canonical.com
Mon Apr 17 17:26:39 UTC 2017
On 2017-04-13 03:08 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/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 at canonical.com>
More information about the fwts-devel
mailing list