Skip to content

Commit 5803501

Browse files
committed
Make sure we don't crash on slightly-off-standard libc implementations
1 parent cc495c2 commit 5803501

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

Core/gb.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,13 @@ static char *default_input_callback(GB_gameboy_t *gb)
5858

5959
if (getline(&expression, &size, stdin) == -1) {
6060
/* The user doesn't have STDIN or used ^D. We make sure the program keeps running. */
61-
free(expression);
61+
62+
/* Some implementations may allocate expressions even on getline failure,
63+
and other implementations may crash on free(NULL). Free expression if
64+
it was allocated. */
65+
if (expression) {
66+
free(expression);
67+
}
6268
GB_set_async_input_callback(gb, NULL); /* Disable async input */
6369
return strdup("c");
6470
}

0 commit comments

Comments
 (0)