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" #include "dungeon.h"
#define LINESIZE 1024
#define NDWARVES 6 // number of dwarves #define NDWARVES 6 // number of dwarves
#define PIRATE NDWARVES // must be NDWARVES-1 when zero-origin #define PIRATE NDWARVES // must be NDWARVES-1 when zero-origin
#define DALTLC LOC_NUGGET // alternate dwarf location #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 */ /* This is where we get a new command from the user */
char* input; char* input;
char inputbuf[LINESIZE];
for (;;) { for (;;) {
input = get_input(); input = get_input();
if (input == NULL) if (input == NULL)
@ -1040,8 +1042,12 @@ L2600:
if (strcmp(input, "") != 0) if (strcmp(input, "") != 0)
break; break;
} }
strncpy(inputbuf, input, LINESIZE - 1);
linenoiseFree(input);
long tokens[4]; long tokens[4];
tokenize(input, tokens); tokenize(inputbuf, tokens);
command.wd1 = tokens[0]; command.wd1 = tokens[0];
command.wd1x = tokens[1]; command.wd1x = tokens[1];
command.wd2 = tokens[2]; command.wd2 = tokens[2];
@ -1118,7 +1124,7 @@ Lookup:
defn = get_vocab_id(word1); defn = get_vocab_id(word1);
if (defn == -1) { if (defn == -1) {
/* Gee, I don't understand. */ /* Gee, I don't understand. */
if (fallback_handler(input)) if (fallback_handler(inputbuf))
continue; continue;
rspeak(DONT_KNOW, command.wd1, command.wd1x); rspeak(DONT_KNOW, command.wd1, command.wd1x);
goto L2600; goto L2600;

10
misc.c
View file

@ -217,13 +217,13 @@ void vspeak(const char* msg, va_list ap)
} }
} }
/* Version specifier */ /* Version specifier */
if (msg[i] == 'V') { if (msg[i] == 'V') {
strcpy(renderp, VERSION); strcpy(renderp, VERSION);
size_t len = strlen(VERSION); size_t len = strlen(VERSION);
renderp += len; renderp += len;
size -= len; size -= len;
} }
// All-lowercase specifier. // All-lowercase specifier.
if (msg[i] == 'L' || msg[i] == 'C') { if (msg[i] == 'L' || msg[i] == 'C') {
@ -345,8 +345,10 @@ char* get_input()
printf("%s", input_prompt); printf("%s", input_prompt);
// LCOV_EXCL_STOP // LCOV_EXCL_STOP
ssize_t numread = getline(&input, &n, stdin); 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); return (NULL);
}
} }
if (input == NULL) // Got EOF; return with it. if (input == NULL) // Got EOF; return with it.