Skip to content

Commit

Permalink
Merge pull request #41 from hkva-fork/fix-swprintf
Browse files Browse the repository at this point in the history
Fix memory corruption caused by swprintf
  • Loading branch information
Wassimulator authored May 16, 2024
2 parents 1c176f6 + 6cb1f88 commit 8beec45
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1323,7 +1323,7 @@ static void load_image_post() {
G->graphics.main_image.aspect_ratio = (float)frac.n1 / frac.n2;

wchar_t title[512];
swprintf(title, L"CactusViewer %hs - %ws", VERSION, G->files[G->current_file_index].file.name);
swprintf(title, array_size(title), L"CactusViewer %hs - %ws", VERSION, G->files[G->current_file_index].file.name);
SetWindowTextW(hwnd, title);

apply_settings();
Expand Down Expand Up @@ -1665,7 +1665,7 @@ static int scan_folder(wchar_t *path) {
BasePath[len] = '\\';
BasePath[len + 1] = 0;
FileName = (wchar_t*)malloc(8 * sizeof(wchar_t));
swprintf(FileName, L"none");
swprintf(FileName, 8, L"none");
result = SCAN_DIR;
} else {
remove_char(path, '/"');
Expand All @@ -1689,18 +1689,21 @@ static int scan_folder(wchar_t *path) {
// FileName will be empty and BasePath will be the image name... swap and set BasePath to current working directory.
free(FileName);
FileName = BasePath;
BasePath = (wchar_t *)malloc((wcslen(CURRENT_FOLDER) + 2) * sizeof(wchar_t));
swprintf(BasePath, L"%ls%lc%lc%", CURRENT_FOLDER, L'\\', L'\0');
const size_t name_len = wcslen(CURRENT_FOLDER) + 2;
BasePath = (wchar_t *)malloc(name_len * sizeof(wchar_t));
swprintf(BasePath, name_len, L"%ls%lc", CURRENT_FOLDER, L'\\');
} else if (PathFileExistsW(BasePath) && PathIsRelativeW(BasePath)) {
// If BasePath is relative, append it to current working directory.
wchar_t *Tmp = BasePath;
BasePath = (wchar_t *)malloc((wcslen(Tmp) + wcslen(CURRENT_FOLDER) + 2) * sizeof(wchar_t));
swprintf(BasePath, L"%ls\\%ls%lc%lc%", CURRENT_FOLDER, Tmp, L'\\', L'\0');
const size_t name_len = wcslen(Tmp) + wcslen(CURRENT_FOLDER) + 2;
BasePath = (wchar_t *)malloc(name_len * sizeof(wchar_t));
swprintf(BasePath, name_len, L"%ls\\%ls%lc", CURRENT_FOLDER, Tmp, L'\\');
free(Tmp);
}

FullPath = (wchar_t *)malloc((wcslen(BasePath) + wcslen(FileName) + 1) * sizeof(wchar_t));
swprintf(FullPath, L"%ls%ls%lc", BasePath, FileName, L'\0');
const size_t name_len = wcslen(BasePath) + wcslen(FileName) + 1;
FullPath = (wchar_t *)malloc(name_len * sizeof(wchar_t));
swprintf(FullPath, name_len, L"%ls%ls%lc", BasePath, FileName, L'\0');
}
if (!is_valid_windows_path(BasePath)) {
G->files.reset_count();
Expand Down

0 comments on commit 8beec45

Please sign in to comment.