Stop leaking memory
authorNHOrus <jy6x2b32pie9@yahoo.com>
Fri, 30 Jun 2017 23:13:30 +0000 (02:13 +0300)
committerEric S. Raymond <esr@thyrsus.com>
Sat, 1 Jul 2017 03:26:18 +0000 (23:26 -0400)
advent.h
main.c
misc.c

index 2437c2301acf8156c0f2dddf7132a96ea234e468..35dd8e0feb9f885c6e82a3d4b2f4d03b959ac3c7 100644 (file)
--- 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 916ff0ac6931decc20f4c1298ca8020fd0588bec..61a2267a59b183f6879f132c9001493938888111 100644 (file)
--- 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 b10a76857b6d845376049a4c47b0e7e0d13dc043..f6342b38e8578feaa9bd8fdb4848710fafc88914 100644 (file)
--- 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.