Skip to content

Commit 9555d66

Browse files
committed
Use C standard malloc/calloc/free instead of platform-specific APIs
See #26 - Replace `LocalAlloc(LMEM_FIXED, size)` with `malloc(size)` - Replace `LocalAlloc(LPTR, size)` with `calloc(1, size)` - Replace `LocalFree` calls with `free` - Simplify null checks from `ptr == NULL` to `!ptr` - Remove `GetLastError()` from allocation error messages - Cast the result of each allocation to the intended pointer type immediately
1 parent d37f9c7 commit 9555d66

File tree

4 files changed

+84
-87
lines changed

4 files changed

+84
-87
lines changed

src/filesystem_utils.c

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ char *JoinPath(const char *p1, const char *p2)
2323
if (*p2_start == PATH_SEPARATOR) { p2_start++; p2_len--; }
2424

2525
size_t joined_len = p1_len + 1 + p2_len;
26-
char *joined_path = (char *)LocalAlloc(LPTR, joined_len + 1);
27-
if (joined_path == NULL) {
28-
APP_ERROR("Failed to allocate buffer for join path (%lu)", GetLastError());
26+
char *joined_path = (char *)calloc(1, joined_len + 1);
27+
if (!joined_path) {
28+
APP_ERROR("Failed to allocate buffer for join path");
2929
return NULL;
3030
}
3131
memcpy(joined_path, p1, p1_len);
@@ -55,9 +55,9 @@ bool CreateDirectoriesRecursively(const char *dir)
5555
}
5656

5757
size_t dir_len = strlen(dir);
58-
char *path = (char *)LocalAlloc(LPTR, dir_len + 1);
59-
if (path == NULL) {
60-
APP_ERROR("LocalAlloc failed (%lu)", GetLastError());
58+
char *path = (char *)calloc(1, dir_len + 1);
59+
if (!path) {
60+
APP_ERROR("Failed to allocate memory");
6161
return false;
6262
}
6363
strcpy(path, dir);
@@ -73,7 +73,7 @@ bool CreateDirectoriesRecursively(const char *dir)
7373
break;
7474
} else {
7575
APP_ERROR("Directory name conflicts with a file(%s)", path);
76-
LocalFree(path);
76+
free(path);
7777
return false;
7878
}
7979
} else {
@@ -82,7 +82,7 @@ bool CreateDirectoriesRecursively(const char *dir)
8282
continue;
8383
} else {
8484
APP_ERROR("Cannot access the directory (%lu)", GetLastError());
85-
LocalFree(path);
85+
free(path);
8686
return false;
8787
}
8888
}
@@ -97,13 +97,13 @@ bool CreateDirectoriesRecursively(const char *dir)
9797

9898
if (!CreateDirectory(path, NULL) && GetLastError() != ERROR_ALREADY_EXISTS) {
9999
APP_ERROR("Failed to create directory (%lu)", GetLastError());
100-
LocalFree(path);
100+
free(path);
101101
return false;
102102
}
103103
}
104104
}
105105

106-
LocalFree(path);
106+
free(path);
107107
return true;
108108
}
109109

@@ -119,17 +119,17 @@ bool CreateParentDirectories(const char *file)
119119
for (; i > 0; i--) { if (file[i] == PATH_SEPARATOR) break; }
120120
if (i == 0) { return true; }
121121

122-
char *dir = (char *)LocalAlloc(LPTR, i + 1);
123-
if (dir == NULL) {
124-
APP_ERROR("LocalAlloc failed (%lu)", GetLastError());
122+
char *dir = (char *)calloc(1, i + 1);
123+
if (!dir) {
124+
APP_ERROR("Failed to allocate memory");
125125
return false;
126126
}
127127

128128
strncpy(dir, file, i);
129129
dir[i] = '\0';
130130
bool result = CreateDirectoriesRecursively(dir);
131131

132-
LocalFree(dir);
132+
free(dir);
133133
return result;
134134
}
135135

@@ -170,11 +170,11 @@ bool DeleteRecursively(const char *path)
170170
}
171171
}
172172

173-
LocalFree(subPath);
173+
free(subPath);
174174
} while (FindNextFile(handle, &findData));
175175
FindClose(handle);
176176
}
177-
LocalFree(findPath);
177+
free(findPath);
178178

179179
if (!RemoveDirectory(path)) {
180180
APP_ERROR("Failed to delete directory (%lu)", GetLastError());
@@ -190,9 +190,9 @@ char *GenerateUniqueName(const char *prefix)
190190
size_t prefix_len = 0;
191191
if (prefix != NULL) { prefix_len = strlen(prefix); }
192192

193-
char *name = (char *)LocalAlloc(LPTR, prefix_len + UID_LENGTH + 1);
194-
if (name == NULL) {
195-
APP_ERROR("Failed to allocate memory for unique name (%lu)", GetLastError());
193+
char *name = (char *)calloc(1, prefix_len + UID_LENGTH + 1);
194+
if (!name) {
195+
APP_ERROR("Failed to allocate memory for unique name");
196196
return NULL;
197197
}
198198

@@ -229,7 +229,7 @@ char *CreateUniqueDirectory(const char *base_path, const char *prefix)
229229
}
230230

231231
char *full_path = JoinPath(base_path, temp_name);
232-
LocalFree(temp_name);
232+
free(temp_name);
233233
if (full_path == NULL) {
234234
APP_ERROR("Failed to construct a unique directory path");
235235
return NULL;
@@ -239,10 +239,10 @@ char *CreateUniqueDirectory(const char *base_path, const char *prefix)
239239
return full_path;
240240
} else if (GetLastError() != ERROR_ALREADY_EXISTS) {
241241
APP_ERROR("Failed to create a unique directory (%lu)", GetLastError());
242-
LocalFree(full_path);
242+
free(full_path);
243243
return NULL;
244244
} else {
245-
LocalFree(full_path);
245+
free(full_path);
246246
}
247247

248248
Sleep(10); // To avoid sequential generation and prevent name duplication.
@@ -262,41 +262,41 @@ char *GetImagePath(void)
262262
https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation
263263
*/
264264
DWORD buffer_size = 32767;
265-
wchar_t *image_path_w = (wchar_t *)LocalAlloc(LPTR, buffer_size * sizeof(wchar_t));
266-
if (image_path_w == NULL) {
267-
APP_ERROR("Failed to allocate buffer for image path (%lu)", GetLastError());
265+
wchar_t *image_path_w = (wchar_t *)calloc(1, buffer_size * sizeof(wchar_t));
266+
if (!image_path_w) {
267+
APP_ERROR("Failed to allocate buffer for image path");
268268
return NULL;
269269
}
270270

271271
DWORD copied = GetModuleFileNameW(NULL, image_path_w, buffer_size);
272272
if (copied == 0 || GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
273273
APP_ERROR("Failed to get image path (%lu)", GetLastError());
274-
LocalFree(image_path_w);
274+
free(image_path_w);
275275
return NULL;
276276
}
277277

278278
int utf8_size = WideCharToMultiByte(CP_UTF8, 0, image_path_w, -1, NULL, 0, NULL, NULL);
279279
if (utf8_size == 0) {
280280
APP_ERROR("Failed to calculate buffer size for UTF-8 conversion (%lu)", GetLastError());
281-
LocalFree(image_path_w);
281+
free(image_path_w);
282282
return NULL;
283283
}
284284

285-
char *image_path_utf8 = (char *)LocalAlloc(LPTR, utf8_size);
286-
if (image_path_utf8 == NULL) {
287-
APP_ERROR("Failed to allocate buffer for UTF-8 image path (%lu)", GetLastError());
288-
LocalFree(image_path_w);
285+
char *image_path_utf8 = (char *)calloc(1, utf8_size);
286+
if (!image_path_utf8) {
287+
APP_ERROR("Failed to allocate buffer for UTF-8 image path");
288+
free(image_path_w);
289289
return NULL;
290290
}
291291

292292
if (WideCharToMultiByte(CP_UTF8, 0, image_path_w, -1, image_path_utf8, utf8_size, NULL, NULL) == 0) {
293293
APP_ERROR("Failed to convert image path to UTF-8 (%lu)", GetLastError());
294-
LocalFree(image_path_w);
295-
LocalFree(image_path_utf8);
294+
free(image_path_w);
295+
free(image_path_utf8);
296296
return NULL;
297297
}
298298

299-
LocalFree(image_path_w);
299+
free(image_path_w);
300300
return image_path_utf8;
301301
}
302302

@@ -318,7 +318,7 @@ char *GetImageDirectoryPath(void) {
318318

319319
if (i == 0) {
320320
APP_ERROR("Executable path does not contain a directory");
321-
LocalFree(image_path);
321+
free(image_path);
322322
return NULL;
323323
}
324324

@@ -328,16 +328,15 @@ char *GetImageDirectoryPath(void) {
328328
// Retrieves the path to the temporary directory for the current user.
329329
char *GetTempDirectoryPath(void)
330330
{
331-
char *temp_dir = (char *)LocalAlloc(LPTR, MAX_PATH);
332-
333-
if (temp_dir == NULL) {
334-
APP_ERROR("Failed to memory allocate for get temp directory (%lu)", GetLastError());
331+
char *temp_dir = (char *)calloc(1, MAX_PATH);
332+
if (!temp_dir) {
333+
APP_ERROR("Failed to memory allocate for get temp directory");
335334
return NULL;
336335
}
337336

338337
if (!GetTempPath(MAX_PATH, temp_dir)) {
339338
APP_ERROR("Failed to get temp path (%lu)", GetLastError());
340-
LocalFree(temp_dir);
339+
free(temp_dir);
341340
return NULL;
342341
}
343342

@@ -370,14 +369,14 @@ bool ChangeDirectoryToSafeDirectory(void)
370369
{
371370
char *working_dir = GetTempDirectoryPath();
372371
bool changed = working_dir && SetCurrentDirectory(working_dir);
373-
LocalFree(working_dir);
372+
free(working_dir);
374373

375374
if (changed) return true;
376375

377376
DEBUG("Failed to change to temporary directory. Trying executable's directory");
378377
working_dir = GetImageDirectoryPath();
379378
changed = working_dir && SetCurrentDirectory(working_dir);
380-
LocalFree(working_dir);
379+
free(working_dir);
381380

382381
if (!changed) {
383382
APP_ERROR("Failed to change to executable's directory");
@@ -427,7 +426,7 @@ MappedFile OpenAndMapFile(const char *file_path, unsigned long long *file_size,
427426
return NULL;
428427
}
429428

430-
MappedFileHandle *handle = (MappedFileHandle *)LocalAlloc(LPTR, sizeof(MappedFileHandle));
429+
MappedFileHandle *handle = (MappedFileHandle *)calloc(1, sizeof(MappedFileHandle));
431430
if (handle) {
432431
handle->hFile = hFile;
433432
handle->hMapping = hMapping;
@@ -437,7 +436,7 @@ MappedFile OpenAndMapFile(const char *file_path, unsigned long long *file_size,
437436
}
438437
return (MappedFile)handle;
439438
} else {
440-
APP_ERROR("Failed to allocate memory for handle (%lu)", GetLastError());
439+
APP_ERROR("Failed to allocate memory for handle");
441440
UnmapViewOfFile(lpBaseAddress);
442441
CloseHandle(hMapping);
443442
CloseHandle(hFile);
@@ -472,7 +471,7 @@ bool FreeMappedFile(MappedFile handle) {
472471
}
473472
}
474473

475-
LocalFree(h);
474+
free(h);
476475
}
477476

478477
return success;

src/inst_dir.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const char *CreateDebugExtractInstDir(void)
3434
}
3535

3636
const char *inst_dir = CreateInstDirectory(image_dir);
37-
LocalFree(image_dir);
37+
free(image_dir);
3838
if (inst_dir == NULL) {
3939
APP_ERROR("Failed to create installation directory in the executable's directory");
4040
}
@@ -52,7 +52,7 @@ const char *CreateTemporaryInstDir(void)
5252
}
5353

5454
const char *inst_dir = CreateInstDirectory(temp_dir);
55-
LocalFree(temp_dir);
55+
free(temp_dir);
5656
if (inst_dir == NULL) {
5757
APP_ERROR("Failed to create installation directory in the temporary directory");
5858
}
@@ -63,7 +63,7 @@ const char *CreateTemporaryInstDir(void)
6363
// Frees the allocated memory for the installation directory path.
6464
void FreeInstDir(void)
6565
{
66-
LocalFree((void *)InstDir);
66+
free((void *)InstDir);
6767
InstDir = NULL;
6868
}
6969

@@ -121,9 +121,9 @@ void MarkInstDirForDeletion(void)
121121
size_t inst_dir_len = strlen(InstDir);
122122
size_t suffix_len = strlen(DELETION_MAKER_SUFFIX);
123123
size_t len = inst_dir_len + suffix_len;
124-
char *marker = LocalAlloc(LPTR, len + 1);
125-
if (marker == NULL) {
126-
APP_ERROR("Failed to allocate memory for deletion marker path (%lu)", GetLastError());
124+
char *marker = (char *)calloc(1, len + 1);
125+
if (!marker) {
126+
APP_ERROR("Failed to allocate memory for deletion marker path");
127127
return;
128128
}
129129
memcpy(marker, InstDir, inst_dir_len);
@@ -138,7 +138,7 @@ void MarkInstDirForDeletion(void)
138138

139139
APP_ERROR("Deletion marker path is %s", marker);
140140
CloseHandle(h);
141-
LocalFree(marker);
141+
free(marker);
142142
}
143143

144144
// Replaces placeholders in a string with the installation directory path.
@@ -155,10 +155,9 @@ char *ReplaceInstDirPlaceholder(const char *str)
155155

156156
for (p = str; *p; p++) { if (*p == PLACEHOLDER) c++; }
157157
SIZE_T out_len = strlen(str) - c + InstDirLen * c + 1;
158-
char *out = (char *)LocalAlloc(LPTR, out_len);
159-
160-
if (out == NULL) {
161-
APP_ERROR("LocalAlloc failed (%lu)", GetLastError());
158+
char *out = (char *)calloc(1, out_len);
159+
if (!out) {
160+
APP_ERROR("Failed to allocate memory");
162161
return NULL;
163162
}
164163

@@ -193,7 +192,7 @@ bool ChangeDirectoryToScriptDirectory(void)
193192
if (!changed) {
194193
APP_ERROR("Failed to change CWD (%lu)", GetLastError());
195194
}
196-
LocalFree(script_dir);
195+
free(script_dir);
197196

198197
return changed;
199198
}
@@ -218,7 +217,7 @@ bool CreateDirectoryUnderInstDir(const char *rel_path)
218217
APP_ERROR("Failed to create directory under installation directory (InstDir): '%s'", dir);
219218
}
220219

221-
LocalFree(dir);
220+
free(dir);
222221
return result;
223222
}
224223

@@ -294,7 +293,7 @@ bool ExportFileToInstDir(const char *rel_path, const void *buf, size_t len)
294293

295294
cleanup:
296295
if (path) {
297-
LocalFree(path);
296+
free(path);
298297
}
299298
return result;
300299
}

0 commit comments

Comments
 (0)