Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions src/cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,16 @@ void CCacheData::PrematureDeleteByPlugin(CPluginInterfaceAbstract* ownDeletePlug

CCacheDirData::CCacheDirData(const char* path) : Names(100, 50)
{
Path[0] = 0;
PathLength = 0;
int l = (int)strlen(path);
if (l > 0 && path[l - 1] == '\\')
l--;
if (l >= MAX_PATH - 1)
{
TRACE_E("CCacheDirData::CCacheDirData(): path is too long.");
return;
}
memcpy(Path, path, l);
if (l > 0)
Path[l++] = '\\';
Expand All @@ -345,9 +352,11 @@ CCacheDirData::~CCacheDirData()
delete name;
}
if (PathLength > 0)
{
Path[PathLength - 1] = 0; // trimming a backslash
SetFileAttributesUtf8(Path, FILE_ATTRIBUTE_ARCHIVE);
RemoveDirectoryUtf8(Path);
SetFileAttributesUtf8(Path, FILE_ATTRIBUTE_ARCHIVE);
RemoveDirectoryUtf8(Path);
}
}

BOOL CCacheDirData::ContainTmpName(const char* tmpName, const char* rootTmpPath,
Expand All @@ -356,6 +365,8 @@ BOOL CCacheDirData::ContainTmpName(const char* tmpName, const char* rootTmpPath,
CALL_STACK_MESSAGE2("CCacheDirData::ContainTmpName(%s, , ,)", tmpName);

*canContainThisName = FALSE;
if (PathLength <= 0)
return FALSE;
if (rootTmpPathLen < PathLength &&
StrNICmp(Path, rootTmpPath, rootTmpPathLen) == 0)
{
Expand Down Expand Up @@ -470,6 +481,8 @@ void CCacheDirData::RemoveEmptyTmpDirsOnlyFromDisk()
{
CALL_STACK_MESSAGE1("CCacheDirData::RemoveEmptyTmpDirsOnlyFromDisk()");

if (PathLength <= 0)
return;
if (PathLength > 0)
Path[PathLength - 1] = 0; // backslash trimming
CStrP pathW(ConvertAllocUtf8ToWide(Path, -1));
Expand All @@ -486,6 +499,8 @@ BOOL CCacheDirData::GetName(CDiskCache* monitor, const char* name, BOOL* exists,
BOOL canBlock, BOOL onlyAdd, int* errorCode)
{
CALL_STACK_MESSAGE4("CCacheDirData::GetName(, %s, , , %d, %d,)", name, canBlock, onlyAdd);
if (PathLength <= 0)
return FALSE;
int i;
if (errorCode != NULL)
*errorCode = DCGNE_SUCCESS;
Expand All @@ -508,6 +523,14 @@ CCacheDirData::GetName(const char* name, const char* tmpName, BOOL* exists, BOOL
CALL_STACK_MESSAGE4("CCacheDirData::GetName(%s, %s, , %d, ,)", name, tmpName, ownDelete);
if (errorCode != NULL)
*errorCode = DCGNE_SUCCESS;
if (PathLength <= 0)
{
TRACE_E("CCacheDirData::GetName(): invalid tmp-directory path.");
*exists = TRUE;
if (errorCode != NULL)
*errorCode = DCGNE_TOOLONGNAME;
return NULL;
}
char tmpFullName[MAX_PATH];
memcpy(tmpFullName, Path, PathLength);
if (PathLength + strlen(tmpName) + 1 <= MAX_PATH)
Expand Down Expand Up @@ -653,6 +676,8 @@ void CCacheDirData::AddVictimsToArray(TDirectArray<CCacheData*>& victArr)

BOOL CCacheDirData::DetachTmpFile(const char* tmpName)
{
if (PathLength <= 0)
return FALSE;
if (StrNICmp(tmpName, Path, PathLength) == 0) // if there's a chance that this is our tmp-file
{
int i;
Expand Down Expand Up @@ -1182,7 +1207,7 @@ CDiskCache::GetName(const char* name, const char* tmpName, BOOL* exists, BOOL on
if (rootTmpPath != NULL &&
(SalCheckPath(TRUE, sysTmpDir, ERROR_SUCCESS, TRUE, MainWindow->HWindow) != ERROR_SUCCESS ||
!CheckAndCreateDirectory(rootTmpPath, NULL, TRUE)) || // if it's not TEMP, tmp-root must be verified and created, if needed
!SalGetTempFileName(rootTmpPath, "SAL", newDirPath, FALSE))
!SalGetTempFileName(rootTmpPath, "SAL", newDirPath, _countof(newDirPath), FALSE))
{
*exists = TRUE; // fatal error
Leave();
Expand Down
42 changes: 32 additions & 10 deletions src/consts.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,10 @@ class CEnterCriticalSection

// because Windows GetTempFileName doesn't work, we wrote our own clone:
// creates file/directory (based on 'file') at path 'path' (NULL -> Windows TEMP dir),
// with prefix 'prefix', returns name of created file in 'tmpName' (min. size MAX_PATH),
// with prefix 'prefix', returns name of created file in 'tmpName' (size in 'tmpNameLen'),
// returns "success?" (on failure returns Windows error code via SetLastError - for compatibility)
BOOL SalGetTempFileName(const char* path, const char* prefix, char* tmpName, int tmpNameLen, BOOL file);
// legacy compatibility overload ('tmpName' must be at least MAX_PATH)
BOOL SalGetTempFileName(const char* path, const char* prefix, char* tmpName, BOOL file);

// because Windows MoveFile can't rename files with read-only attribute on Novell,
Expand Down Expand Up @@ -153,6 +155,8 @@ BOOL IsNOVELLDrive(const char* path);
// whether file size check is needed after copying); for optimization
// purposes uses 'lastLantasticCheckRoot' (for first call "", then don't change)
// and 'lastIsLantasticPath' (result for 'lastLantasticCheckRoot')
BOOL IsLantasticDrive(const char* path, char* lastLantasticCheckRoot, int lastLantasticCheckRootBufSize, BOOL& lastIsLantasticPath);
// legacy compatibility overload ('lastLantasticCheckRoot' must be at least MAX_PATH)
BOOL IsLantasticDrive(const char* path, char* lastLantasticCheckRoot, BOOL& lastIsLantasticPath);

// returns TRUE for network paths
Expand Down Expand Up @@ -259,8 +263,12 @@ BOOL PathsAreOnTheSameVolume(const char* path1, const char* path2, BOOL* resIsOn
BOOL IsTheSamePath(const char* path1, const char* path2);

// determines if path is plugin FS type, 'path' is the path being checked, 'fsName' is
// buffer of MAX_PATH characters for FS name (or NULL), returns 'userPart' (if != NULL) - pointer
// into 'path' to first character of plugin-defined path (after first ':')
// optional output buffer for FS name (size in 'fsNameBufSize', can be NULL),
// returns 'userPart' (if != NULL) - pointer into 'path' to first character
// of plugin-defined path (after first ':')
BOOL IsPluginFSPath(const char* path, char* fsName, int fsNameBufSize, const char** userPart = NULL);
BOOL IsPluginFSPath(char* path, char* fsName, int fsNameBufSize, char** userPart = NULL);
// legacy compatibility overloads ('fsName' must be at least MAX_PATH if non-NULL)
BOOL IsPluginFSPath(const char* path, char* fsName = NULL, const char** userPart = NULL);
BOOL IsPluginFSPath(char* path, char* fsName = NULL, char** userPart = NULL);

Expand All @@ -272,8 +280,9 @@ BOOL IsFileURLPath(const char* path);
int IsFileLink(const char* fileExtension);

// gets both UNC and normal root path from 'path', returns path in 'root' in format 'C:\' or '\\SERVER\SHARE\',
// returns number of characters in root path (without null-terminator); 'root' is buffer of at least MAX_PATH characters, for longer
// UNC root paths truncates to MAX_PATH-2 characters and adds backslash (either way it's not 100% a root path)
// returns number of characters in root path (without null-terminator)
int GetRootPath(char* root, int rootBufSize, const char* path);
// legacy compatibility overload ('root' must be at least MAX_PATH)
int GetRootPath(char* root, const char* path);

// returns pointer after root (more precisely to backslash right after root) of both UNC and normal path 'path'
Expand Down Expand Up @@ -883,8 +892,11 @@ BOOL ResolveSubsts(char* resPath);

// Performs resolve of subst and reparse point for path 'path', then attempts to get GUID path
// for the mount-point of the path (if missing, for the root of the path). Returns FALSE on failure.
// On success, returns TRUE and sets 'mountPoint' and 'guidPath' (if different from NULL, must
// point to buffers of size at least MAX_PATH; strings will be terminated with backslash).
// On success, returns TRUE and sets 'mountPoint' and 'guidPath' (if non-NULL; sizes are in
// 'mountPointBufSize' and 'guidPathBufSize'; strings are terminated with backslash).
BOOL GetResolvedPathMountPointAndGUID(const char* path, char* mountPoint, int mountPointBufSize,
char* guidPath, int guidPathBufSize);
// legacy compatibility overloads ('mountPoint'/'guidPath' must be at least MAX_PATH if non-NULL)
BOOL GetResolvedPathMountPointAndGUID(const char* path, char* mountPoint, char* guidPath);

// attempt to return correct values (also handles reparse points - complete path is specified instead of root)
Expand Down Expand Up @@ -921,6 +933,8 @@ BOOL GetReparsePointDestination(const char* repPointDir, char* repPointDstBuf, D
// in 'currentReparsePoint' (at least MAX_PATH characters) returns current (last) local
// reparse point, on failure returns classic root; returns FALSE on failure; if
// 'error' is not NULL, TRUE is written to it on error
BOOL GetCurrentLocalReparsePoint(const char* path, char* currentReparsePoint, int currentReparsePointBufSize, BOOL* error = NULL);
// legacy compatibility overload ('currentReparsePoint' must be at least MAX_PATH)
BOOL GetCurrentLocalReparsePoint(const char* path, char* currentReparsePoint, BOOL* error = NULL);

// call only for paths 'path' whose root (after removing subst) is DRIVE_FIXED (elsewhere it makes no sense to search for
Expand Down Expand Up @@ -2369,8 +2383,10 @@ void ShellActionAux6(CFilesWindow* panel);

//******************************************************************************

// returns in 'path' (buffer at least MAX_PATH characters) the path Configuration.IfPathIsInaccessibleGoTo;
// returns in 'path' the path Configuration.IfPathIsInaccessibleGoTo;
// takes into account Configuration.IfPathIsInaccessibleGoToIsMyDocs setting
void GetIfPathIsInaccessibleGoTo(char* path, int pathBufSize, BOOL forceIsMyDocs);
// legacy compatibility overload ('path' must be at least 2 * MAX_PATH)
void GetIfPathIsInaccessibleGoTo(char* path, BOOL forceIsMyDocs = FALSE);

// loads icon overlay handler configuration from registry configuration
Expand Down Expand Up @@ -2399,19 +2415,25 @@ BOOL RestoreNetworkConnection(HWND parent, const char* name, const char* remoteN
LPNETRESOURCE lpNetResource = NULL);

// constructs text for Type column in panel for unassociated file (e.g. "AAA File" or "File")
void GetCommonFileTypeStr(char* buf, int bufSize, int* resLen, const char* ext);
// legacy compatibility overload ('buf' must be at least TRANSFER_BUFFER_MAX)
void GetCommonFileTypeStr(char* buf, int* resLen, const char* ext);

// finds doubled separators and removes unnecessary ones (on Vista I encountered doubled
// separators in context menu on .bar files)
void RemoveUselessSeparatorsFromMenu(HMENU h);

// returns "Open Salamander" directory on path CSIDL_APPDATA in 'buf' (buffer of size MAX_PATH)
// returns "Open Salamander" directory on path CSIDL_APPDATA in 'buf'
BOOL GetOurPathInRoamingAPPDATA(char* buf, int bufSize);
// legacy compatibility overload ('buf' must be at least MAX_PATH)
BOOL GetOurPathInRoamingAPPDATA(char* buf);

// creates "Open Salamander" directory on path CSIDL_APPDATA; returns TRUE if path
// fits in MAX_PATH (its existence is not guaranteed, CreateDirectory result is not checked);
// if 'buf' is not NULL, it is buffer of size MAX_PATH, in which this path is returned
// if 'buf' is not NULL, it is output buffer of size 'bufSize', in which this path is returned
// WARNING: use only Vista+
BOOL CreateOurPathInRoamingAPPDATA(char* buf, int bufSize);
// legacy compatibility overload ('buf' must be at least MAX_PATH if non-NULL)
BOOL CreateOurPathInRoamingAPPDATA(char* buf);

#ifndef _WIN64
Expand Down
2 changes: 1 addition & 1 deletion src/dialogs3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,7 @@ void CDriveInfo::Transfer(CTransferInfo& ti)
char guidPath[MAX_PATH];
mountPoint[0] = 0;
guidPath[0] = 0;
if (GetResolvedPathMountPointAndGUID(VolumePath, mountPoint, guidPath))
if (GetResolvedPathMountPointAndGUID(VolumePath, mountPoint, _countof(mountPoint), guidPath, _countof(guidPath)))
{
SetDlgItemTextUtf8(HWindow, IDT_MOUNTPOINT, mountPoint);
SetDlgItemTextUtf8(HWindow, IDT_GUIDPATH, guidPath);
Expand Down
4 changes: 2 additions & 2 deletions src/dialogs5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1827,7 +1827,7 @@ void CCfgPageDrives::Transfer(CTransferInfo& ti)
char newPath[MAX_PATH];
if (ti.Type == ttDataToWindow)
{
GetIfPathIsInaccessibleGoTo(path);
GetIfPathIsInaccessibleGoTo(path, _countof(path), FALSE);
ti.EditLine(IDE_DRVSPEC_ONERRGOTO, path, MAX_PATH);
IfPathIsInaccessibleGoToChanged = FALSE;
}
Expand All @@ -1836,7 +1836,7 @@ void CCfgPageDrives::Transfer(CTransferInfo& ti)
if (IfPathIsInaccessibleGoToChanged) // change only if the user actually edited the path
{
ti.EditLine(IDE_DRVSPEC_ONERRGOTO, newPath, MAX_PATH);
GetIfPathIsInaccessibleGoTo(path, TRUE);
GetIfPathIsInaccessibleGoTo(path, _countof(path), TRUE);
if (IsTheSamePath(path, newPath)) // user wants to go to My Documents
{
Configuration.IfPathIsInaccessibleGoToIsMyDocs = TRUE;
Expand Down
2 changes: 1 addition & 1 deletion src/dialogs6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2434,7 +2434,7 @@ CDriveSelectErrDlg::DialogProc(UINT uMsg, WPARAM wParam, LPARAM lParam)

case DRIVE_FIXED: // mount-point, determine the root of removable drive
{
if (!GetCurrentLocalReparsePoint(DrvPath, root))
if (!GetCurrentLocalReparsePoint(DrvPath, root, _countof(root)))
setTimer = FALSE; // can't be a mount-point, no periodic tests
break;
}
Expand Down
35 changes: 25 additions & 10 deletions src/fileswn2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ BOOL CFilesWindow::ChangeToRescuePathOrFixedDrive(HWND parent, BOOL* noChange, B
refreshListBox, canForce, tryCloseReason);
BOOL noChangeUsed = FALSE;
char ifPathIsInaccessibleGoTo[MAX_PATH];
GetIfPathIsInaccessibleGoTo(ifPathIsInaccessibleGoTo);
GetIfPathIsInaccessibleGoTo(ifPathIsInaccessibleGoTo, _countof(ifPathIsInaccessibleGoTo), FALSE);
if (ifPathIsInaccessibleGoTo[0] == '\\' && ifPathIsInaccessibleGoTo[1] == '\\' ||
ifPathIsInaccessibleGoTo[0] != 0 && ifPathIsInaccessibleGoTo[1] == ':')
{
Expand Down Expand Up @@ -1704,7 +1704,7 @@ BOOL CFilesWindow::ChangePathToDisk(HWND parent, const char* path, int suggested
BOOL canTryUserRescuePath = FALSE; // allows using Configuration.IfPathIsInaccessibleGoTo right before the fixed-drive path
BOOL openIfPathIsInaccessibleGoToCfg = FALSE;
char ifPathIsInaccessibleGoTo[2 * MAX_PATH];
GetIfPathIsInaccessibleGoTo(ifPathIsInaccessibleGoTo);
GetIfPathIsInaccessibleGoTo(ifPathIsInaccessibleGoTo, _countof(ifPathIsInaccessibleGoTo), FALSE);
if ((ifPathIsInaccessibleGoTo[0] == '\\' && ifPathIsInaccessibleGoTo[1] == '\\' ||
ifPathIsInaccessibleGoTo[0] != 0 && ifPathIsInaccessibleGoTo[1] == ':') &&
!IsTheSamePath(path, ifPathIsInaccessibleGoTo))
Expand Down Expand Up @@ -1822,7 +1822,7 @@ BOOL CFilesWindow::ChangePathToDisk(HWND parent, const char* path, int suggested
canTryUserRescuePath = FALSE; // we won't try it more than once
openIfPathIsInaccessibleGoToCfg = TRUE;
fixedDrive = FALSE; // we'll allow switching to a fixed-drive (perhaps it was tried already but the user path had priority)
GetIfPathIsInaccessibleGoTo(changedPath);
GetIfPathIsInaccessibleGoTo(changedPath, _countof(changedPath), FALSE);
shorterPathWarning = TRUE; // we want to see errors for the "rescue" path
change = TRUE;
}
Expand Down Expand Up @@ -1934,7 +1934,7 @@ BOOL CFilesWindow::ChangePathToDisk(HWND parent, const char* path, int suggested
}
if (drvType != DRIVE_REMOTE)
{
GetCurrentLocalReparsePoint(changedPath, CheckPathRootWithRetryMsgBox);
GetCurrentLocalReparsePoint(changedPath, CheckPathRootWithRetryMsgBox, MAX_PATH);
if (strlen(CheckPathRootWithRetryMsgBox) > 3)
{
lstrcpyn(drive, CheckPathRootWithRetryMsgBox, MAX_PATH);
Expand Down Expand Up @@ -3483,8 +3483,18 @@ void CFilesWindow::GetContextMenuPos(POINT* p)
ClientToScreen(GetListBoxHWND(), p);
}

void GetCommonFileTypeStr(char* buf, int* resLen, const char* ext)
void GetCommonFileTypeStr(char* buf, int bufSize, int* resLen, const char* ext)
{
if (buf == NULL || resLen == NULL || ext == NULL || bufSize <= 0)
{
TRACE_E("GetCommonFileTypeStr(): invalid parameter.");
if (buf != NULL && bufSize > 0)
buf[0] = 0;
if (resLen != NULL)
*resLen = 0;
return;
}

char uppercaseExt[MAX_PATH];
char* d = uppercaseExt;
char* end = uppercaseExt + MAX_PATH - 1;
Expand All @@ -3493,17 +3503,22 @@ void GetCommonFileTypeStr(char* buf, int* resLen, const char* ext)
*d = 0;
if (*ext == 0 && uppercaseExt[0] != 0)
{ // we have the entire extension in uppercase (no spaces and shorter than MAX_PATH) + it is not empty
*resLen = _snprintf_s(buf, TRANSFER_BUFFER_MAX, _TRUNCATE, CommonFileTypeName2, uppercaseExt);
*resLen = _snprintf_s(buf, bufSize, _TRUNCATE, CommonFileTypeName2, uppercaseExt);
if (*resLen < 0)
*resLen = TRANSFER_BUFFER_MAX - 1; // _snprintf_s reports truncation to the buffer size
*resLen = bufSize - 1; // _snprintf_s reports truncation to the buffer size
}
else
{
memcpy(buf, CommonFileTypeName, CommonFileTypeNameLen + 1);
*resLen = CommonFileTypeNameLen;
lstrcpyn(buf, CommonFileTypeName, bufSize);
*resLen = (int)strlen(buf);
}
}

void GetCommonFileTypeStr(char* buf, int* resLen, const char* ext)
{
GetCommonFileTypeStr(buf, TRANSFER_BUFFER_MAX, resLen, ext);
}

void CFilesWindow::RefreshListBox(int suggestedXOffset,
int suggestedTopIndex, int suggestedFocusIndex,
BOOL ensureFocusIndexVisible, BOOL wholeItemVisible)
Expand Down Expand Up @@ -3859,7 +3874,7 @@ void CFilesWindow::RefreshListBox(int suggestedXOffset,
if (commonFileType)
{
int resLen;
GetCommonFileTypeStr(buf, &resLen, f->Ext);
GetCommonFileTypeStr(buf, _countof(buf), &resLen, f->Ext);
GetTextExtentPoint32(dc, buf, resLen, &act);
act.cx += SPACE_WIDTH;
if (columnWidthType < act.cx)
Expand Down
6 changes: 3 additions & 3 deletions src/fileswn3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ BOOL CFilesWindow::ReadDirectory(HWND parent, BOOL isRefresh)
}
if (drvType2 != DRIVE_REMOTE)
{
GetCurrentLocalReparsePoint(GetPath(), CheckPathRootWithRetryMsgBox);
GetCurrentLocalReparsePoint(GetPath(), CheckPathRootWithRetryMsgBox, MAX_PATH);
if (strlen(CheckPathRootWithRetryMsgBox) > 3)
{
lstrcpyn(drive, CheckPathRootWithRetryMsgBox, MAX_PATH);
Expand Down Expand Up @@ -2088,7 +2088,7 @@ BOOL CFilesWindow::ChangeDir(const char* newDir, int suggestedTopIndex, const ch

char fsName[2 * MAX_PATH];
char* fsUserPart;
if (!sendDirectlyToPluginLocal && IsPluginFSPath(path, fsName, (const char**)&fsUserPart))
if (!sendDirectlyToPluginLocal && IsPluginFSPath(path, fsName, _countof(fsName), (const char**)&fsUserPart))
{
if (strlen(fsUserPart) >= 2 * MAX_PATH) // plugins do not count with longer path
{
Expand Down Expand Up @@ -2553,7 +2553,7 @@ BOOL CFilesWindow::ChangeDir(const char* newDir, int suggestedTopIndex, const ch
}
if (drvType != DRIVE_REMOTE)
{
GetCurrentLocalReparsePoint(copy, CheckPathRootWithRetryMsgBox);
GetCurrentLocalReparsePoint(copy, CheckPathRootWithRetryMsgBox, MAX_PATH);
if (strlen(CheckPathRootWithRetryMsgBox) > 3)
{
lstrcpyn(drive, CheckPathRootWithRetryMsgBox, MAX_PATH);
Expand Down
2 changes: 1 addition & 1 deletion src/fileswn8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ void PluginFSConvertPathToExternal(char* path)
char* fsUserPart;
int index;
int fsNameIndex;
if (IsPluginFSPath(path, fsName, (const char**)&fsUserPart) &&
if (IsPluginFSPath(path, fsName, _countof(fsName), (const char**)&fsUserPart) &&
Plugins.IsPluginFS(fsName, index, fsNameIndex))
{
CPluginData* plugin = Plugins.Get(index);
Expand Down
Loading