From: antirez Date: Fri, 8 Feb 2013 11:18:42 +0000 (+0100) Subject: linenoiseHistorySetMaxLen() was broken and never tested. Fixed. X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=2c25335ded9816b9ace4702025e55e03eab98a7d;p=linenoise.git linenoiseHistorySetMaxLen() was broken and never tested. Fixed. --- diff --git a/linenoise.c b/linenoise.c index c06d88e..3a9606c 100644 --- a/linenoise.c +++ b/linenoise.c @@ -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; }