From: NHOrus Date: Fri, 30 Jun 2017 23:13:30 +0000 (+0300) Subject: Stop leaking memory X-Git-Tag: takebird~168 X-Git-Url: https://jxself.org/git/?a=commitdiff_plain;h=c83df10dc62412a57dc4342a0c1d22ffa048246c;p=open-adventure.git Stop leaking memory --- diff --git a/advent.h b/advent.h index 2437c23..35dd8e0 100644 --- a/advent.h +++ b/advent.h @@ -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 diff --git a/main.c b/main.c index 916ff0a..61a2267 100644 --- a/main.c +++ b/main.c @@ -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; diff --git a/misc.c b/misc.c index b10a768..f6342b3 100644 --- a/misc.c +++ b/misc.c @@ -217,14 +217,14 @@ void vspeak(const char* msg, va_list ap) } } - /* Version specifier */ + /* Version specifier */ if (msg[i] == 'V') { - strcpy(renderp, VERSION); + strcpy(renderp, VERSION); size_t len = strlen(VERSION); renderp += len; size -= len; - } - + } + // All-lowercase specifier. if (msg[i] == 'L' || msg[i] == 'C') { packed_to_token(arg, renderp); /* unpack directly to destination */ @@ -345,8 +345,10 @@ 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.