From 30a6a8f932dd60f36b505b450e34ebbd93b3cf6a Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Wed, 6 May 2020 13:21:56 +0800 Subject: [PATCH] Update linenoise.c: calloc returning NULL is not handled Calloc function tries to allocate 4096 bytes. If such memory is not available, it returns NULL pointer. This exception was not handled in the code, causing a dirty crash. --- components/console/linenoise/linenoise.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/components/console/linenoise/linenoise.c b/components/console/linenoise/linenoise.c index 91dc9e3d2e..04a75b307e 100644 --- a/components/console/linenoise/linenoise.c +++ b/components/console/linenoise/linenoise.c @@ -979,6 +979,9 @@ static void sanitize(char* src) { char *linenoise(const char *prompt) { char *buf = calloc(1, LINENOISE_MAX_LINE); int count = 0; + if (buf == NULL) { + return NULL; + } if (!dumbmode) { count = linenoiseRaw(buf, LINENOISE_MAX_LINE, prompt); } else { @@ -1105,9 +1108,15 @@ int linenoiseHistorySave(const char *filename) { * on error -1 is returned. */ int linenoiseHistoryLoad(const char *filename) { FILE *fp = fopen(filename,"r"); - char buf[LINENOISE_MAX_LINE]; + if (fp == NULL) { + return -1; + } - if (fp == NULL) return -1; + char *buf = calloc(1, LINENOISE_MAX_LINE); + if (buf == NULL) { + fclose(fp); + return -1; + } while (fgets(buf,LINENOISE_MAX_LINE,fp) != NULL) { char *p; @@ -1117,6 +1126,9 @@ int linenoiseHistoryLoad(const char *filename) { if (p) *p = '\0'; linenoiseHistoryAdd(buf); } + + free(buf); fclose(fp); + return 0; }