@@ -621,8 +621,16 @@ VAL idris_readStr(VM* vm, FILE* h) {
621621 {
622622 /* Make the buffer bigger and read more of the line */
623623 line_buf_size += BUFSIZE;
624− line_buf = (char *) realloc (line_buf, line_buf_size);
625−
624+ { /* limit the scope of new_line_buf to where it's needed for errors*/
625+ char *new_line_buf = realloc (line_buf, line_buf_size);
626+ if (new_line_buf == NULL)
627+ {
628+ fprintf(stderr, "Fatal Error: couldn't allocate a larger line buffer.");
629+ exit(EXIT_FAILURE);
630+ } else {
631+ line_buf = new_line_buf;
632+ }
633+ }
626634 /* points to last byte again */
627635 line_ptr = line_buf + line_buf_size - 1;
628636 /* so we can see if fgets put a 0 there */