native_posix: Fix realloc potential leak

Fix a possible leak if `realloc` fails here; check the result of the
`realloc` call before updating the pointer, so it can be freed in the
failure case.

Signed-off-by: Noah Pendleton <noah.pendleton@gmail.com>
This commit is contained in:
Noah Pendleton 2019-10-04 08:08:28 -04:00 committed by Alberto Escolar
commit d0abd9a104

View file

@ -54,13 +54,15 @@ void native_add_command_line_opts(struct args_struct_t *args)
growby = ARGS_ALLOC_CHUNK_SIZE; growby = ARGS_ALLOC_CHUNK_SIZE;
} }
args_struct = realloc(args_struct, struct args_struct_t *new_args_struct = realloc(args_struct,
(args_aval + growby)* (args_aval + growby)*
sizeof(struct args_struct_t)); sizeof(struct args_struct_t));
args_aval += growby; args_aval += growby;
/* LCOV_EXCL_START */ /* LCOV_EXCL_START */
if (args_struct == NULL) { if (new_args_struct == NULL) {
posix_print_error_and_exit("Could not allocate memory"); posix_print_error_and_exit("Could not allocate memory");
} else {
args_struct = new_args_struct;
} }
/* LCOV_EXCL_STOP */ /* LCOV_EXCL_STOP */
} }