Skip to content

Commit dd610ad

Browse files
committed
fix(dirent): LCUI_ReadDirW() UNINITIALIZED READ
1 parent c9dc276 commit dd610ad

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/util/dirent.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,20 @@
3434
#include <LCUI/LCUI.h>
3535
#include <LCUI/font/charset.h>
3636

37-
int LCUI_OpenDirA(const char *filepath, LCUI_Dir *dir)
37+
int LCUI_OpenDirA(const char *path, LCUI_Dir *dir)
3838
{
3939
#if defined(LCUI_BUILD_IN_WIN32) || (_WIN32)
40-
int len;
40+
size_t len;
4141
char *newpath;
42+
char name[] = "\\*";
4243

43-
len = strlen(filepath) + 5;
44-
newpath = malloc(len * sizeof(char));
44+
len = strlen(path) + 1;
45+
newpath = malloc(len * sizeof(char) + sizeof(name));
4546
if (newpath == NULL) {
4647
return -ENOMEM;
4748
}
48-
/* 需要加上通配符 */
49-
sprintf_s(newpath, len, "%s\\*", filepath);
49+
strcpy(newpath, path);
50+
strcpy(newpath + len - 1, name);
5051
dir->handle = FindFirstFileA(newpath, &dir->entry.dataA);
5152
free(newpath);
5253
if (dir->handle == INVALID_HANDLE_VALUE) {
@@ -68,15 +69,18 @@ int LCUI_OpenDirA(const char *filepath, LCUI_Dir *dir)
6869
int LCUI_OpenDirW(const wchar_t *path, LCUI_Dir *dir)
6970
{
7071
#if defined(LCUI_BUILD_IN_WIN32) || (_WIN32)
71-
int len;
72+
size_t len;
7273
wchar_t *newpath;
74+
wchar_t name[] = L"\\*";
75+
7376

74-
len = wcslen(path) + 5;
75-
newpath = malloc(len * sizeof(wchar_t));
77+
len = wcslen(path) + 1;
78+
newpath = malloc(len * sizeof(wchar_t) + sizeof(name));
7679
if (!newpath) {
7780
return -ENOMEM;
7881
}
79-
swprintf(newpath, len, L"%s\\*", path);
82+
wcscpy(newpath, path);
83+
wcscpy(newpath + len - 1, name);
8084
dir->handle = FindFirstFileW(newpath, &dir->entry.dataW);
8185
free(newpath);
8286
if (dir->handle == INVALID_HANDLE_VALUE) {

0 commit comments

Comments
 (0)