Skip to content

Commit 1bcc2fc

Browse files
committed
Avoid early allocation
1 parent 54f920d commit 1bcc2fc

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

win32/readdir.c

+6-7
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,8 @@ DIR *opendir(const char *dir)
3737
return NULL;
3838
}
3939

40-
dp = (DIR *) calloc(1, sizeof(DIR) + (_MAX_FNAME*5+1)*sizeof(char));
41-
if (dp == NULL) {
42-
return NULL;
43-
}
44-
4540
resolvedw = php_win32_ioutil_conv_any_to_w(resolved_path_buff, PHP_WIN32_CP_IGNORE_LEN, &resolvedw_len);
4641
if (!resolvedw) {
47-
free(dp);
4842
return NULL;
4943
}
5044

@@ -56,7 +50,6 @@ DIR *opendir(const char *dir)
5650
}
5751
filespecw = (wchar_t *)malloc((filespecw_len + 1)*sizeof(wchar_t));
5852
if (filespecw == NULL) {
59-
free(dp);
6053
free(resolvedw);
6154
return NULL;
6255
}
@@ -73,6 +66,12 @@ DIR *opendir(const char *dir)
7366
filespecw[index] = L'\0';
7467
wcscat(filespecw, L"\\*");
7568

69+
dp = (DIR *) calloc(1, sizeof(DIR) + (_MAX_FNAME*5+1)*sizeof(char));
70+
if (dp == NULL) {
71+
free(resolvedw);
72+
return NULL;
73+
}
74+
7675
if ((handle = FindFirstFileExW(filespecw, FindExInfoBasic, &(dp->fileinfo), FindExSearchNameMatch, NULL, FIND_FIRST_EX_LARGE_FETCH)) == INVALID_HANDLE_VALUE) {
7776
DWORD err = GetLastError();
7877
if (err == ERROR_NO_MORE_FILES || err == ERROR_FILE_NOT_FOUND) {

0 commit comments

Comments
 (0)