Skip to content

Commit e5913c3

Browse files
mazgchP-R-O-C-H-Y
andauthored
reduce stack size requirement for this library by 4k my moving a buffer to heap. (espressif#6745)
Better allocate the buffer for f_mkfs from the heap otherwise the stack requirement of this library is huge due to a work buffer allocated for f_mkfs in sdcard_mount. The work buffer is only needed if argument format_if_empty is set true (which is by default false). This change is quite important if you plan to use this library in a task. as now it increased the tasks stacks size by 4k, even this memory is never used if users are not aware of the large stack requirement during init this library may have other variables on stack that would have written memory outsides its range which can cause various side effects. Co-authored-by: Jan Procházka <90197375+P-R-O-C-H-Y@users.noreply.github.com>
1 parent 247bca8 commit e5913c3

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

libraries/SD/src/sd_diskio.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -801,8 +801,13 @@ bool sdcard_mount(uint8_t pdrv, const char* path, uint8_t max_files, bool format
801801
if (res != FR_OK) {
802802
log_e("f_mount failed: %s", fferr2str[res]);
803803
if(res == 13 && format_if_empty){
804-
BYTE work[FF_MAX_SS];
804+
BYTE* work = (BYTE*) malloc(sizeof(BYTE) * FF_MAX_SS);
805+
if (!work) {
806+
log_e("alloc for f_mkfs failed");
807+
return false;
808+
}
805809
res = f_mkfs(drv, FM_ANY, 0, work, sizeof(work));
810+
free(work);
806811
if (res != FR_OK) {
807812
log_e("f_mkfs failed: %s", fferr2str[res]);
808813
esp_vfs_fat_unregister_path(path);

0 commit comments

Comments
 (0)