From 1ce5cf13d27344461309581226c54b56ab99b0c9 Mon Sep 17 00:00:00 2001 From: Chad Attermann Date: Fri, 12 Jul 2024 12:42:53 -0600 Subject: [PATCH 1/2] Fix memory leak on failure to open Fixes leak of memory caused by failure to free file and directory structures when open fails. --- libraries/Adafruit_LittleFS/src/Adafruit_LittleFS_File.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS_File.cpp b/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS_File.cpp index 4f8d48567..3091f477e 100644 --- a/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS_File.cpp +++ b/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS_File.cpp @@ -66,6 +66,8 @@ bool File::_open_file (char const *filepath, uint8_t mode) { // failed to open PRINT_LFS_ERR(rc); + // free memory + rtos_free(_file); return false; } @@ -89,6 +91,8 @@ bool File::_open_dir (char const *filepath) { // failed to open PRINT_LFS_ERR(rc); + // free memory + rtos_free(_dir); return false; } From 236e434296ba5f5c3fa740d1d97af4385d817bc9 Mon Sep 17 00:00:00 2001 From: Chad Attermann Date: Fri, 12 Jul 2024 13:18:40 -0600 Subject: [PATCH 2/2] Additionally clearing pointers just in case Clearing `_file` and `_dir` pointers after freeing on failure to open just in case close() is later called. To elaborate, the `_file` and `_dir` pointers are normally freed and cleared by _close(), but when a failure occurs upon opening, these pointers were being left orphaned since calling close() after failure to open doesn't make much sense, and indeed caused serious issues within lfs. --- libraries/Adafruit_LittleFS/src/Adafruit_LittleFS_File.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS_File.cpp b/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS_File.cpp index 3091f477e..43ed2d962 100644 --- a/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS_File.cpp +++ b/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS_File.cpp @@ -68,6 +68,7 @@ bool File::_open_file (char const *filepath, uint8_t mode) PRINT_LFS_ERR(rc); // free memory rtos_free(_file); + _file = NULL; return false; } @@ -93,6 +94,7 @@ bool File::_open_dir (char const *filepath) PRINT_LFS_ERR(rc); // free memory rtos_free(_dir); + _dir = NULL; return false; }