]> granicus.if.org Git - esp-idf/commitdiff
examples: Fixing possible memory leak, not freeing "run_time" prior to returning
authormharizanov <mharizanov@gmail.com>
Tue, 4 Sep 2018 12:52:11 +0000 (15:52 +0300)
committerMahavir Jain <mahavir@espressif.com>
Thu, 8 Nov 2018 10:27:00 +0000 (15:57 +0530)
Merges https://github.com/espressif/esp-idf/pull/2367

examples/storage/nvs_rw_blob/main/nvs_blob_example_main.c

index e5bd6eabe5a7ef5e88f1616e3b16a2580d1d1256..410ced3f0bdddb61d40c4890218a2cbd0e6f039c 100644 (file)
@@ -80,7 +80,10 @@ esp_err_t save_run_time(void)
     uint32_t* run_time = malloc(required_size + sizeof(uint32_t));
     if (required_size > 0) {
         err = nvs_get_blob(my_handle, "run_time", run_time, &required_size);
-        if (err != ESP_OK) return err;
+        if (err != ESP_OK) {
+            free(run_time);
+            return err;
+        }
     }
 
     // Write value including previously saved blob if available
@@ -131,7 +134,10 @@ esp_err_t print_what_saved(void)
     } else {
         uint32_t* run_time = malloc(required_size);
         err = nvs_get_blob(my_handle, "run_time", run_time, &required_size);
-        if (err != ESP_OK) return err;
+        if (err != ESP_OK) {
+            free(run_time);
+            return err;
+        }
         for (int i = 0; i < required_size / sizeof(uint32_t); i++) {
             printf("%d: %d\n", i + 1, run_time[i]);
         }