Bladeren bron

Merge branch 'bugfix/linenoise_stack_alloc' into 'master'

console: linenoise: move allocations from stack to heap, fix calloc result checking (Github PR)

Merges https://github.com/espressif/esp-idf/pull/5161

Closes IDFGH-3144

See merge request espressif/esp-idf!8612
Ivan Grokhotkov 5 jaren geleden
bovenliggende
commit
a97cd645ad
1 gewijzigde bestanden met toevoegingen van 14 en 2 verwijderingen
  1. 14 2
      components/console/linenoise/linenoise.c

+ 14 - 2
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;
 }