[*] Exceptionless-API hardening

This commit is contained in:
Reece Wilson 2023-12-22 00:17:19 +00:00
parent 298b095cc1
commit 66eca9ff8f
3 changed files with 181 additions and 118 deletions

View File

@ -50,40 +50,48 @@ namespace Aurora::IO::FS
virtual StatEx *Next() override virtual StatEx *Next() override
{ {
StatEx *pNext {};
if (!this->pDir) if (!this->pDir)
{ {
return {}; return {};
} }
auto pNext = this->pDir->Next(); try
while (!pNext)
{ {
DoNext();
if (!this->pDir)
{
return {};
}
pNext = this->pDir->Next(); pNext = this->pDir->Next();
} while (!pNext)
if (curSubDir.size())
{
pNext->fileName.insert(pNext->fileName.begin(), curSubDir.begin(), curSubDir.end());
}
if (pNext->bExistsDirectory)
{
nextLevel.push_back(pNext->fileName + "/");
nextLevel2.push_back(pNext->fileName + "/");
}
else
{
if (!AuFS::Remove(pNext->path))
{ {
this->failedPaths.push_back(pNext->fileName); DoNext();
if (!this->pDir)
{
return {};
}
pNext = this->pDir->Next();
} }
if (curSubDir.size())
{
pNext->fileName.insert(pNext->fileName.begin(), curSubDir.begin(), curSubDir.end());
}
if (pNext->bExistsDirectory)
{
nextLevel.push_back(pNext->fileName + "/");
nextLevel2.push_back(pNext->fileName + "/");
}
else
{
if (!AuFS::Remove(pNext->path))
{
this->failedPaths.push_back(pNext->fileName);
}
}
}
catch (...)
{
SysPushErrorCatch();
} }
return pNext; return pNext;
@ -107,19 +115,27 @@ namespace Aurora::IO::FS
auto pObj = AuMakeShared<RecursiveDirDeleter>(); auto pObj = AuMakeShared<RecursiveDirDeleter>();
SysCheckNotNullMemory(pObj, false); SysCheckNotNullMemory(pObj, false);
if (!pObj->OpenDir(string)) try
{ {
return {}; if (!pObj->OpenDir(string))
{
return {};
}
while (pObj->Next())
{
}
pObj->RemoveDirs();
AuFS::Remove(string);
}
catch (...)
{
SysPushErrorCatch();
} }
while (pObj->Next())
{
}
pObj->RemoveDirs();
AuFS::Remove(string);
return !AuFS::DirExists(string); return !AuFS::DirExists(string);
} }
@ -128,44 +144,51 @@ namespace Aurora::IO::FS
auto pObj = AuMakeShared<RecursiveDirDeleter>(); auto pObj = AuMakeShared<RecursiveDirDeleter>();
SysCheckNotNullMemory(pObj, false); SysCheckNotNullMemory(pObj, false);
if (!pObj->OpenDir(string)) try
{ {
return {}; if (!pObj->OpenDir(string))
}
while (pObj->Next())
{
}
pObj->RemoveDirs();
AuFS::Remove(string);
if (AuFS::DirExists(string))
{
for (const auto &str : pObj->failedPaths)
{ {
auto normalizedUserDir = string + "/" ; return {};
auto normalizedUserDir2 = normalizedUserDir + str;
if (AuFS::FileExists(normalizedUserDir2) ||
AuFS::DirExists(normalizedUserDir2))
{
if (AuEndsWith(normalizedUserDir, "//") ||
AuEndsWith(normalizedUserDir, "\\/"))
{
normalizedUserDir.pop_back();
normalizedUserDir[normalizedUserDir.size() - 1] = '/';
}
normalizedUserDir += str;
failingPaths.push_back(normalizedUserDir);
}
} }
return false; while (pObj->Next())
{
}
pObj->RemoveDirs();
AuFS::Remove(string);
if (AuFS::DirExists(string))
{
for (const auto &str : pObj->failedPaths)
{
auto normalizedUserDir = string + "/";
auto normalizedUserDir2 = normalizedUserDir + str;
if (AuFS::FileExists(normalizedUserDir2) ||
AuFS::DirExists(normalizedUserDir2))
{
if (AuEndsWith(normalizedUserDir, "//") ||
AuEndsWith(normalizedUserDir, "\\/"))
{
normalizedUserDir.pop_back();
normalizedUserDir[normalizedUserDir.size() - 1] = '/';
}
normalizedUserDir += str;
failingPaths.push_back(normalizedUserDir);
}
}
return false;
}
}
catch (...)
{
SysPushErrorCatch();
} }
return true; return true;

View File

@ -48,31 +48,39 @@ namespace Aurora::IO::FS
virtual StatEx *Next() override virtual StatEx *Next() override
{ {
StatEx *pNext {};
if (!this->pDir) if (!this->pDir)
{ {
return {}; return {};
} }
auto pNext = this->pDir->Next(); try
while (!pNext)
{ {
DoNext(); pNext = this->pDir->Next();
if (!this->pDir) while (!pNext)
{ {
return {}; DoNext();
if (!this->pDir)
{
return {};
}
pNext = this->pDir->Next();
} }
pNext = this->pDir->Next(); if (curSubDir.size())
} {
pNext->fileName.insert(pNext->fileName.begin(), curSubDir.begin(), curSubDir.end());
}
if (curSubDir.size()) if (pNext->bExistsDirectory)
{ {
pNext->fileName.insert(pNext->fileName.begin(), curSubDir.begin(), curSubDir.end()); nextLevel.push_back(pNext->fileName + "/");
}
} }
catch (...)
if (pNext->bExistsDirectory)
{ {
nextLevel.push_back(pNext->fileName + "/"); SysPushErrorCatch();
} }
return pNext; return pNext;
@ -88,9 +96,16 @@ namespace Aurora::IO::FS
return {}; return {};
} }
if (!pObj->OpenDir(string)) try
{ {
return {}; if (!pObj->OpenDir(string))
{
return {};
}
}
catch (...)
{
SysPushErrorCatch();
} }
return pObj; return pObj;

View File

@ -20,15 +20,22 @@ namespace Aurora::Process
{ {
AUKN_SYM bool GetWorkingDirectory(AuString &path) AUKN_SYM bool GetWorkingDirectory(AuString &path)
{ {
#if !defined(AURORA_IS_MODERNNT_DERIVED)
if (!AuTryResize(path, 0x4000)) if (!AuTryResize(path, 0x4000))
{ {
return false; return false;
} }
#endif
#if defined(AURORA_IS_MODERNNT_DERIVED) #if defined(AURORA_IS_MODERNNT_DERIVED)
std::wstring eh; std::wstring eh;
eh.resize(path.size()); if (!AuTryResize(eh, 0x4000))
{
return false;
}
auto length = ::GetCurrentDirectoryW(eh.size(), eh.data()); auto length = ::GetCurrentDirectoryW(eh.size(), eh.data());
if (length == 0) if (length == 0)
@ -36,11 +43,19 @@ namespace Aurora::Process
return false; return false;
} }
path = Locale::ConvertFromWChar(eh.data(), length); path = AuMove(Locale::ConvertFromWChar(eh.data(), length));
if (!path.ends_with(AuFS::kPathSplitter)) try
{ {
path += AuFS::kPathSplitter; if (!path.ends_with(AuFS::kPathSplitter))
{
path += AuFS::kPathSplitter;
}
}
catch (...)
{
SysPushErrorCatch();
return false;
} }
return true; return true;
@ -174,46 +189,56 @@ namespace Aurora::Process
static bool GetModulePath(const AuString *&module, const AuString *&partialPath, const AuString *&fullPath) static bool GetModulePath(const AuString *&module, const AuString *&partialPath, const AuString *&fullPath)
{ {
static AuString cachedModule, cachedPartialPath, cachedFullPath; static AuString cachedModule, cachedPartialPath, cachedFullPath;
static bool init = false; static AuInitOnce gInitOnce;
static AuThreadPrimitives::SpinLock spinlock; static bool bFailed {};
char spitter;
if (!AuThreading::InitOnceLocker::TryLock(&gInitOnce, true))
AU_TRY_LOCK_GUARD_RET_DEF(spinlock);
if (AuExchange(init, true))
{ {
if (!cachedModule.size()) gInitOnce.Wait();
}
else
{
try
{ {
return false; char spitter;
if (GetModulePathSlow(cachedFullPath, spitter))
{
auto indexA = cachedFullPath.find_last_of(spitter);
if (indexA != AuString::npos)
{
cachedModule = cachedFullPath.substr(indexA + 1);
cachedPartialPath = cachedFullPath.substr(0, indexA);
}
else
{
bFailed = true;
}
if (!cachedPartialPath.ends_with(AuFS::kPathSplitter))
{
cachedPartialPath += AuFS::kPathSplitter;
}
}
else
{
bFailed = true;
}
}
catch (...)
{
bFailed = true;
SysPushErrorCatch();
} }
goto returnCached; AuThreading::InitOnceLocker::Finish(&gInitOnce, bFailed);
} }
if (!GetModulePathSlow(cachedFullPath, spitter)) if (bFailed)
{ {
return false; return false;
} }
// this scope is required for c++20, bleh
{
auto indexA = cachedFullPath.find_last_of(spitter);
if (indexA == AuString::npos)
{
return false;
}
cachedModule = cachedFullPath.substr(indexA + 1);
cachedPartialPath = cachedFullPath.substr(0, indexA);
}
if (!cachedPartialPath.ends_with(AuFS::kPathSplitter))
{
cachedPartialPath += AuFS::kPathSplitter;
}
returnCached:
module = &cachedModule; module = &cachedModule;
fullPath = &cachedFullPath; fullPath = &cachedFullPath;
partialPath = &cachedPartialPath; partialPath = &cachedPartialPath;