Skip to content

Commit 30a6a8f

Browse files
committed
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.
1 parent d82b609 commit 30a6a8f

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

components/console/linenoise/linenoise.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,9 @@ static void sanitize(char* src) {
979979
char *linenoise(const char *prompt) {
980980
char *buf = calloc(1, LINENOISE_MAX_LINE);
981981
int count = 0;
982+
if (buf == NULL) {
983+
return NULL;
984+
}
982985
if (!dumbmode) {
983986
count = linenoiseRaw(buf, LINENOISE_MAX_LINE, prompt);
984987
} else {
@@ -1105,9 +1108,15 @@ int linenoiseHistorySave(const char *filename) {
11051108
* on error -1 is returned. */
11061109
int linenoiseHistoryLoad(const char *filename) {
11071110
FILE *fp = fopen(filename,"r");
1108-
char buf[LINENOISE_MAX_LINE];
1111+
if (fp == NULL) {
1112+
return -1;
1113+
}
11091114

1110-
if (fp == NULL) return -1;
1115+
char *buf = calloc(1, LINENOISE_MAX_LINE);
1116+
if (buf == NULL) {
1117+
fclose(fp);
1118+
return -1;
1119+
}
11111120

11121121
while (fgets(buf,LINENOISE_MAX_LINE,fp) != NULL) {
11131122
char *p;
@@ -1117,6 +1126,9 @@ int linenoiseHistoryLoad(const char *filename) {
11171126
if (p) *p = '\0';
11181127
linenoiseHistoryAdd(buf);
11191128
}
1129+
1130+
free(buf);
11201131
fclose(fp);
1132+
11211133
return 0;
11221134
}

0 commit comments

Comments
 (0)