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.
pull/5260/head
Ivan Grokhotkov 2020-05-06 13:21:56 +08:00
rodzic d82b60905f
commit 30a6a8f932
1 zmienionych plików z 14 dodań i 2 usunięć

Wyświetl plik

@ -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;
}