Stop leaking memory

This commit is contained in:
NHOrus 2017-07-01 02:13:30 +03:00 committed by Eric S. Raymond
parent 8bc08773fa
commit c83df10dc6
3 changed files with 16 additions and 7 deletions

View file

@ -5,6 +5,7 @@
#include "dungeon.h"
#define LINESIZE 1024
#define NDWARVES 6 // number of dwarves
#define PIRATE NDWARVES // must be NDWARVES-1 when zero-origin
#define DALTLC LOC_NUGGET // alternate dwarf location

10
main.c
View file

@ -1029,6 +1029,8 @@ L2600:
/* This is where we get a new command from the user */
char* input;
char inputbuf[LINESIZE];
for (;;) {
input = get_input();
if (input == NULL)
@ -1040,8 +1042,12 @@ L2600:
if (strcmp(input, "") != 0)
break;
}
strncpy(inputbuf, input, LINESIZE - 1);
linenoiseFree(input);
long tokens[4];
tokenize(input, tokens);
tokenize(inputbuf, tokens);
command.wd1 = tokens[0];
command.wd1x = tokens[1];
command.wd2 = tokens[2];
@ -1118,7 +1124,7 @@ Lookup:
defn = get_vocab_id(word1);
if (defn == -1) {
/* Gee, I don't understand. */
if (fallback_handler(input))
if (fallback_handler(inputbuf))
continue;
rspeak(DONT_KNOW, command.wd1, command.wd1x);
goto L2600;

4
misc.c
View file

@ -345,9 +345,11 @@ char* get_input()
printf("%s", input_prompt);
// LCOV_EXCL_STOP
ssize_t numread = getline(&input, &n, stdin);
if (numread == -1) // Got EOF; return with it.
if (numread == -1) { // Got EOF; return with it.
free(input);
return (NULL);
}
}
if (input == NULL) // Got EOF; return with it.
return (input);