commit 9b3c202d23893aa532494044fdd1713c9a8351d9
parent e11f71e041b29a0999f5815f7ab3c7e74d2533c3
Author: Frederic Cambus <fred@statdns.com>
Date: Tue, 22 Jan 2019 12:17:47 +0100
Use the correct idiom for realloc, to avoid leaking memory if allocation fails.
Diffstat:
2 files changed, 10 insertions(+), 8 deletions(-)
diff --git a/src/loaders/ansi.c b/src/loaders/ansi.c
@@ -116,7 +116,7 @@ ansilove_ansi(struct ansilove_ctx *ctx, struct ansilove_options *options)
/* ANSi buffer structure array definition */
uint32_t structIndex = 0;
- struct ansiChar *ansi_buffer;
+ struct ansiChar *ptr, *ansi_buffer;
size_t ansi_buffer_size = ANSI_BUFFER_SIZE;
@@ -420,13 +420,14 @@ ansilove_ansi(struct ansilove_ctx *ctx, struct ansilove_options *options)
if (structIndex == ansi_buffer_size) {
ansi_buffer_size += ANSI_BUFFER_SIZE;
- ansi_buffer = realloc(ansi_buffer, ansi_buffer_size * sizeof(struct ansiChar));
+ ptr = realloc(ansi_buffer, ansi_buffer_size * sizeof(struct ansiChar));
- if (ansi_buffer == NULL) {
+ if (ptr == NULL) {
ctx->error = ANSILOVE_MEMORY_ERROR;
free(ansi_buffer);
- ansi_buffer = NULL;
return -1;
+ } else {
+ ansi_buffer = ptr;
}
}
diff --git a/src/loaders/pcboard.c b/src/loaders/pcboard.c
@@ -57,7 +57,7 @@ ansilove_pcboard(struct ansilove_ctx *ctx, struct ansilove_options *options)
uint32_t column = 0, row = 0, columnMax = 0, rowMax = 0;
/* PCB buffer structure array definition */
- struct pcbChar *pcboard_buffer;
+ struct pcbChar *ptr, *pcboard_buffer;
/* PCB buffer dynamic memory allocation */
pcboard_buffer = malloc(sizeof (struct pcbChar));
@@ -133,12 +133,13 @@ ansilove_pcboard(struct ansilove_ctx *ctx, struct ansilove_options *options)
rowMax = row;
/* reallocate structure array memory */
- pcboard_buffer = realloc(pcboard_buffer, (structIndex + 1) * sizeof (struct pcbChar));
- if (pcboard_buffer == NULL) {
+ ptr = realloc(pcboard_buffer, (structIndex + 1) * sizeof (struct pcbChar));
+ if (ptr == NULL) {
ctx->error = ANSILOVE_MEMORY_ERROR;
free(pcboard_buffer);
- pcboard_buffer = NULL;
return -1;
+ } else {
+ pcboard_buffer = ptr;
}
/* write current character in pcbChar structure */