Commit cbf172f79dbc86264fd35b2b8a0b308896ae00e1

antirez 2014-03-12T13:10:44

Handle malloc error in linenoiseAddCompletion(). We just don't add the entry on out of memory to avoid to break the API with previous versions of Linenoise.

diff --git a/linenoise.c b/linenoise.c
index 514ed89..186b25a 100644
--- a/linenoise.c
+++ b/linenoise.c
@@ -310,9 +310,17 @@ void linenoiseSetCompletionCallback(linenoiseCompletionCallback *fn) {
  * understand example. */
 void linenoiseAddCompletion(linenoiseCompletions *lc, char *str) {
     size_t len = strlen(str);
-    char *copy = malloc(len+1);
+    char *copy, **cvec;
+
+    copy = malloc(len+1);
+    if (copy == NULL) return;
     memcpy(copy,str,len+1);
-    lc->cvec = realloc(lc->cvec,sizeof(char*)*(lc->len+1));
+    cvec = realloc(lc->cvec,sizeof(char*)*(lc->len+1));
+    if (cvec == NULL) {
+        free(copy);
+        return;
+    }
+    lc->cvec = cvec;
     lc->cvec[lc->len++] = copy;
 }