Message ID | 20170413100834.7458-1-colin.king@canonical.com |
---|---|
State | Accepted |
Headers | show |
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>
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 --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); }