Skip to content

Commit 4408997

Browse files
committed
Free allocated buffers before calling return
Calling return before freeing makes them un-reachable, hence why I decided to do this.
1 parent 0a5cbbc commit 4408997

6 files changed

Lines changed: 85 additions & 35 deletions

File tree

source/file/dirlist.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,11 @@ Result updateList(int clearindex)
161161
fileCount++; // Increment file count
162162
}
163163
}
164-
else
164+
else
165+
{
166+
free(entries);
165167
return ret;
168+
}
166169

167170
free(entries);
168171

source/menus/menu_music.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,16 @@ enum file_types getMusicFileType(const char * file)
5050

5151
/* Failure opening file */
5252
if (R_FAILED(fsOpen(&handle, fsArchive, file, FS_OPEN_READ)))
53+
{
54+
FSFILE_Close(handle);
5355
return -1;
56+
}
5457

5558
if (R_FAILED(FSFILE_Read(handle, &bytesRead, offset, &fileSig, 4)))
56-
goto err;
59+
{
60+
FSFILE_Close(handle);
61+
return -2;
62+
}
5763

5864
offset += bytesRead;
5965

@@ -97,7 +103,6 @@ enum file_types getMusicFileType(const char * file)
97103
}
98104
}
99105

100-
err:
101106
FSFILE_Close(handle);
102107
return file_type;
103108
}

source/minizip/archive.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Result unzExtractCurrentFile(unzFile * unzHandle, int * path)
2424

2525
void * buf = (void *)malloc(bufsize);
2626
if (!buf)
27-
return 0;
27+
return -2;
2828

2929
char * filenameWithoutPath = basename(filename);
3030

@@ -45,6 +45,7 @@ Result unzExtractCurrentFile(unzFile * unzHandle, int * path)
4545
if ((res = unzOpenCurrentFile(unzHandle)) != UNZ_OK)
4646
{
4747
unzClose(unzHandle);
48+
free(buf);
4849
return res;
4950
}
5051

@@ -75,10 +76,10 @@ Result unzExtractCurrentFile(unzFile * unzHandle, int * path)
7576

7677
res = unzCloseCurrentFile(unzHandle);
7778
}
78-
79+
7980
if (buf)
8081
free(buf);
81-
82+
8283
return res;
8384
}
8485

source/screenshot.c

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,42 @@
99

1010
static int num = 0;
1111

12-
static int generateScreenshot(const char * path)
12+
static Result generateScreenshot(const char * path)
1313
{
1414
int x = 0, y = 0;
1515
Handle handle;
1616
u32 bytesWritten = 0;
1717
u64 offset = 0;
1818
size_t size = 0x36;
19+
Result ret = 0;
1920

2021
// Get top/bottom framebuffers
2122
u8 * gfxBottom = gfxGetFramebuffer(GFX_BOTTOM, GFX_BOTTOM, NULL, NULL);
2223
u8 * gfxTopLeft = gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL);
2324

2425
// Open file for writing screenshot
25-
fsOpen(&handle, fsArchive, path, (FS_OPEN_CREATE | FS_OPEN_WRITE));
26+
if (R_FAILED(ret = fsOpen(&handle, fsArchive, path, (FS_OPEN_CREATE | FS_OPEN_WRITE))))
27+
return ret;
2628

2729
// Some
28-
u8 * buffer = (u8*)malloc(size + 576000);
29-
memset(buffer, 0, size + 576000);
30-
buffer[size + 576000] = 0;
30+
u8 * buf = (u8*)malloc(size + 576000);
31+
memset(buf, 0, size + 576000);
32+
buf[size + 576000] = 0;
3133

32-
FSFILE_SetSize(handle, (u16)(size + 576000));
34+
if (R_FAILED(ret = FSFILE_SetSize(handle, (u16)(size + 576000))))
35+
{
36+
free(buf);
37+
return ret;
38+
}
3339

34-
*(u16*)&buffer[0x0] = 0x4D42;
35-
*(u32*)&buffer[0x2] = size + 576000;
36-
*(u32*)&buffer[0xA] = size;
37-
*(u32*)&buffer[0xE] = 0x28;
38-
*(u32*)&buffer[0x12] = 400;
39-
*(u32*)&buffer[0x16] = 480;
40-
*(u32*)&buffer[0x1A] = 0x00180001;
41-
*(u32*)&buffer[0x22] = 576000;
40+
*(u16*)&buf[0x0] = 0x4D42;
41+
*(u32*)&buf[0x2] = size + 576000;
42+
*(u32*)&buf[0xA] = size;
43+
*(u32*)&buf[0xE] = 0x28;
44+
*(u32*)&buf[0x12] = 400;
45+
*(u32*)&buf[0x16] = 480;
46+
*(u32*)&buf[0x1A] = 0x00180001;
47+
*(u32*)&buf[0x22] = 576000;
4248

4349
// Generate top left
4450
u8* framebuf = gfxTopLeft;
@@ -49,9 +55,9 @@ static int generateScreenshot(const char * path)
4955
{
5056
int si = ((239 - y) + (x * 240)) * 3;
5157
int di = size + (x + ((479 - y) * 400)) * 3;
52-
buffer[di++] = framebuf[si++];
53-
buffer[di++] = framebuf[si++];
54-
buffer[di++] = framebuf[si++];
58+
buf[di++] = framebuf[si++];
59+
buf[di++] = framebuf[si++];
60+
buf[di++] = framebuf[si++];
5561
}
5662
}
5763

@@ -64,33 +70,42 @@ static int generateScreenshot(const char * path)
6470
{
6571
int si = ((239 - y) + (x * 240)) * 3;
6672
int di = size + ((x+40) + ((239 - y) * 400)) * 3;
67-
buffer[di++] = framebuf[si++];
68-
buffer[di++] = framebuf[si++];
69-
buffer[di++] = framebuf[si++];
73+
buf[di++] = framebuf[si++];
74+
buf[di++] = framebuf[si++];
75+
buf[di++] = framebuf[si++];
7076
}
7177

7278
// Make adjustments for the smaller width
7379
for (x = 0; x < 40; x++)
7480
{
7581
int di = size + (x + ((239 - y) * 400)) * 3;
76-
buffer[di++] = 0;
77-
buffer[di++] = 0;
78-
buffer[di++] = 0;
82+
buf[di++] = 0;
83+
buf[di++] = 0;
84+
buf[di++] = 0;
7985
}
8086

8187
for (x = 360; x < 400; x++)
8288
{
8389
int di = size + (x + ((239 - y) * 400)) * 3;
84-
buffer[di++] = 0;
85-
buffer[di++] = 0;
86-
buffer[di++] = 0;
90+
buf[di++] = 0;
91+
buf[di++] = 0;
92+
buf[di++] = 0;
8793
}
8894
}
8995

90-
FSFILE_Write(handle, &bytesWritten, offset, (u32 *)buffer, size + 576000, 0x10001);
91-
FSFILE_Close(handle);
92-
free(buffer);
96+
if (R_FAILED(FSFILE_Write(handle, &bytesWritten, offset, (u32 *)buf, size + 576000, 0x10001)))
97+
{
98+
free(buf);
99+
return ret;
100+
}
101+
102+
if (R_FAILED(FSFILE_Close(handle)))
103+
{
104+
free(buf);
105+
return ret;
106+
}
93107

108+
free(buf);
94109
return 0;
95110
}
96111

source/theme.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ Result saveThemeConfig(char * themePath, char * coloursPath)
5151
snprintf(buf, 512, themeConfig, themePath, coloursPath);
5252

5353
if (R_FAILED(ret = fsWrite(fsArchive, "/3ds/3DShell/theme.cfg", buf)))
54+
{
55+
free(buf);
5456
return ret;
57+
}
5558

5659
free(buf);
5760
return 0;
@@ -73,7 +76,10 @@ static Result loadThemeConfig(void)
7376
char * buf = (char *)malloc(size + 1);
7477

7578
if (R_FAILED(ret = fsRead(fsArchive, "/3ds/3DShell/theme.cfg", size, buf)))
79+
{
80+
free(buf);
7681
return ret;
82+
}
7783

7884
buf[size] = '\0';
7985

@@ -104,7 +110,10 @@ static Result createFontColours(void)
104110
120, 120, 120);
105111

106112
if (R_FAILED(ret = fsWrite(fsArchive, "/3ds/3DShell/themes/default/colours.cfg", buf)))
113+
{
114+
free(buf);
107115
return ret;
116+
}
108117

109118
free(buf);
110119
return 0;
@@ -129,7 +138,10 @@ static Result loadFontColours(void)
129138
char * buf = (char *)malloc(size + 1);
130139

131140
if (R_FAILED(ret = fsRead(fsArchive, colour_dir, size, buf)))
141+
{
142+
free(buf);
132143
return ret;
144+
}
133145

134146
buf[size] = '\0';
135147

source/utils.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ Result saveConfig(int sortBy, bool recycleBin, bool protection, bool hidden)
2020
snprintf(buf, 256, configFile, sortBy, recycleBin, protection, hidden);
2121

2222
if (R_FAILED(ret = fsWrite(fsArchive, "/3ds/3DShell/config.cfg", buf)))
23+
{
24+
free(buf);
2325
return ret;
26+
}
2427

2528
free(buf);
2629
return 0;
@@ -49,7 +52,10 @@ Result loadConfig(void)
4952
char * buf = (char *)malloc(size + 1);
5053

5154
if (R_FAILED(ret = fsRead(fsArchive, "/3ds/3DShell/config.cfg", size, buf)))
55+
{
56+
free(buf);
5257
return ret;
58+
}
5359

5460
buf[size] = '\0';
5561

@@ -79,7 +85,10 @@ Result getLastDirectory(void)
7985
char * buf = (char *)malloc(size + 1);
8086

8187
if (R_FAILED(ret = fsRead(fsArchive, "/3ds/3DShell/lastdir.txt", size, buf)))
88+
{
89+
free(buf);
8290
return ret;
91+
}
8392

8493
buf[size] = '\0';
8594

@@ -216,6 +225,11 @@ const char * getUsername(void)
216225
for (int i = 0; i < 0x13; i++)
217226
whcar_username[i] = (wchar_t)((u16 *)data)[i];
218227
}
228+
else
229+
{
230+
free(username);
231+
return NULL;
232+
}
219233

220234
wcstombs(username, whcar_username, 0x1C);
221235

0 commit comments

Comments
 (0)