]> git.lizzy.rs Git - linenoise.git/commitdiff
linenoiseHistorySetMaxLen() was broken and never tested. Fixed.
authorantirez <antirez@gmail.com>
Fri, 8 Feb 2013 11:18:42 +0000 (12:18 +0100)
committerSteve Bennett <steveb@workware.net.au>
Tue, 26 Feb 2013 04:00:02 +0000 (14:00 +1000)
linenoise.c

index c06d88e3782450f7c99eedf7e0dcd46522c62caa..3a9606cee645da3a70b45ed1a417f96c14ff40d3 100644 (file)
@@ -1475,8 +1475,16 @@ int linenoiseHistorySetMaxLen(int len) {
 
         newHistory = (char **)malloc(sizeof(char*)*len);
         if (newHistory == NULL) return 0;
-        if (len < tocopy) tocopy = len;
-        memcpy(newHistory,history+(history_max_len-tocopy), sizeof(char*)*tocopy);
+
+        /* If we can't copy everything, free the elements we'll not use. */
+        if (len < tocopy) {
+            int j;
+
+            for (j = 0; j < tocopy-len; j++) free(history[j]);
+            tocopy = len;
+        }
+        memset(newHistory,0,sizeof(char*)*len);
+        memcpy(newHistory,history+(history_len-tocopy), sizeof(char*)*tocopy);
         free(history);
         history = newHistory;
     }