Skip to content

Commit d0abd9a

Browse files
noahpaescolar
authored andcommitted
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 <[email protected]>
1 parent 9bb7da0 commit d0abd9a

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

boards/posix/native_posix/cmdline.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@ void native_add_command_line_opts(struct args_struct_t *args)
5454
growby = ARGS_ALLOC_CHUNK_SIZE;
5555
}
5656

57-
args_struct = realloc(args_struct,
57+
struct args_struct_t *new_args_struct = realloc(args_struct,
5858
(args_aval + growby)*
5959
sizeof(struct args_struct_t));
6060
args_aval += growby;
6161
/* LCOV_EXCL_START */
62-
if (args_struct == NULL) {
62+
if (new_args_struct == NULL) {
6363
posix_print_error_and_exit("Could not allocate memory");
64+
} else {
65+
args_struct = new_args_struct;
6466
}
6567
/* LCOV_EXCL_STOP */
6668
}

0 commit comments

Comments
 (0)