Skip to content

Commit

Permalink
fix possible memory leaking
Browse files Browse the repository at this point in the history
  • Loading branch information
dixyes committed May 29, 2024
1 parent a4fb9e9 commit 9d84d14
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion php_micro_fileinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,35 @@ const wchar_t *micro_get_filename_w() {
}

const char *micro_get_filename(void) {
return php_win32_cp_w_to_utf8(micro_get_filename_w());
static char *self_filename = NULL;
if (NULL != self_filename) {
return self_filename;
}
int self_filename_len;
LPWCH self_filename_w = micro_get_filename_w();
if (NULL == self_filename_w) {
return NULL;
}

self_filename_len = WideCharToMultiByte(CP_UTF8, 0, self_filename_w, -1, NULL, 0, NULL, NULL);
if (0 == self_filename_len) {
// null string? wtf?
return NULL;
}

self_filename = malloc(self_filename_len);
if (NULL == self_filename) {
// impossible
return NULL;
}

if (0 == WideCharToMultiByte(CP_UTF8, 0, self_filename_w, -1, self_filename, self_filename_len, NULL, NULL)) {
// impossible
free(self_filename);
return NULL;
}

return self_filename;
}

#elif defined(__linux) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
Expand Down

0 comments on commit 9d84d14

Please sign in to comment.