Skip to content

Commit a97cd64

Browse files
committed
Merge branch 'bugfix/linenoise_stack_alloc' into 'master'
console: linenoise: move allocations from stack to heap, fix calloc result checking (Github PR) Merges espressif/esp-idf#5161 Closes IDFGH-3144 See merge request espressif/esp-idf!8612
2 parents 62c36a0 + 30a6a8f commit a97cd64

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)