Skip to content

Commit cf22b14

Browse files
authored
Fix FileStream memory leak (#2160)
1 parent bf2fb00 commit cf22b14

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

targets/ESP32/_nanoCLR/System.IO.FileSystem/nf_sys_io_filesystem_System_IO_FileStream.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,28 @@ void CombinePathAndName(char *outpath, const char *path1, const char *path2)
2424
// Converts from windows type path "c:\folder\folder\file.ext"
2525
// to linux type path used in ESP32 VFS "/c/folder/folder/file.exe
2626
// where /c is the mount point
27+
////////////////////////////////////////////
28+
// MAKE SURE TO FREE THE RETURNED POINTER //
29+
////////////////////////////////////////////
2730
//
2831
char *ConvertToVfsPath(const char *filepath)
2932
{
3033
char *startPath = NULL;
3134
char *path = NULL;
3235

3336
int pathlen = hal_strlen_s(filepath);
37+
38+
/////////////////////////////////
39+
// MAKE SURE TO FREE THIS POINTER
3440
startPath = (char *)platform_malloc(pathlen + 1);
41+
42+
// sanity check for successfull malloc
43+
if (startPath == NULL)
44+
{
45+
// failed to allocate memory
46+
return NULL;
47+
}
48+
3549
path = startPath;
3650
hal_strcpy_s(path, pathlen + 1, filepath);
3751

@@ -75,10 +89,9 @@ HRESULT Library_nf_sys_io_filesystem_System_IO_FileStream::OpenFileNative___VOID
7589

7690
// setup file path
7791
filePath = (char *)platform_malloc(2 * FF_LFN_BUF + 1);
78-
vfsPath = (char *)platform_malloc(2 * FF_LFN_BUF + 1);
7992

8093
// sanity check for successfull malloc
81-
if (filePath == NULL || vfsPath == NULL)
94+
if (filePath == NULL)
8295
{
8396
// failed to allocate memory
8497
NANOCLR_SET_AND_LEAVE(CLR_E_OUT_OF_MEMORY);
@@ -212,10 +225,9 @@ HRESULT Library_nf_sys_io_filesystem_System_IO_FileStream::ReadNative___I4__STRI
212225

213226
// setup file path
214227
filePath = (char *)platform_malloc(2 * FF_LFN_BUF + 1);
215-
vfsPath = (char *)platform_malloc(2 * FF_LFN_BUF + 1);
216228

217229
// sanity check for successfull malloc
218-
if (filePath == NULL || vfsPath == NULL)
230+
if (filePath == NULL)
219231
{
220232
// failed to allocate memory
221233
NANOCLR_SET_AND_LEAVE(CLR_E_OUT_OF_MEMORY);
@@ -305,10 +317,9 @@ HRESULT Library_nf_sys_io_filesystem_System_IO_FileStream::WriteNative___VOID__S
305317

306318
// setup file path
307319
filePath = (char *)platform_malloc(2 * FF_LFN_BUF + 1);
308-
vfsPath = (char *)platform_malloc(2 * FF_LFN_BUF + 1);
309320

310321
// sanity check for successfull mallocs
311-
if (filePath == NULL || vfsPath == NULL)
322+
if (filePath == NULL)
312323
{
313324
// failed to allocate memory
314325
NANOCLR_SET_AND_LEAVE(CLR_E_OUT_OF_MEMORY);

0 commit comments

Comments
 (0)