refactor extension code and add error handling

This commit is contained in:
Akash Mozumdar 2018-09-01 04:23:29 -04:00
parent a978df2655
commit ca749691f8
3 changed files with 19 additions and 12 deletions

View File

@ -26,7 +26,7 @@ std::map<int, QString> LoadExtensions()
return extensionNames; return extensionNames;
} }
std::wstring DispatchSentenceToExtensions(std::wstring sentence, std::unordered_map<std::string, int> miscInfo) bool DispatchSentenceToExtensions(std::wstring& sentence, std::unordered_map<std::string, int> miscInfo)
{ {
wchar_t* sentenceBuffer = (wchar_t*)malloc((sentence.size() + 1) * sizeof(wchar_t)); wchar_t* sentenceBuffer = (wchar_t*)malloc((sentence.size() + 1) * sizeof(wchar_t));
wcscpy(sentenceBuffer, sentence.c_str()); wcscpy(sentenceBuffer, sentence.c_str());
@ -34,15 +34,19 @@ std::wstring DispatchSentenceToExtensions(std::wstring sentence, std::unordered_
InfoForExtension* miscInfoTraverser = miscInfoLinkedList; InfoForExtension* miscInfoTraverser = miscInfoLinkedList;
for (auto& i : miscInfo) miscInfoTraverser = miscInfoTraverser->nextProperty = new InfoForExtension{ i.first.c_str(), i.second, nullptr }; for (auto& i : miscInfo) miscInfoTraverser = miscInfoTraverser->nextProperty = new InfoForExtension{ i.first.c_str(), i.second, nullptr };
extenMutex.lock_shared(); extenMutex.lock_shared();
try
{
for (auto i : extensions) for (auto i : extensions)
{ {
wchar_t* prev = sentenceBuffer; wchar_t* prev = sentenceBuffer;
sentenceBuffer = i.second(sentenceBuffer, miscInfoLinkedList); sentenceBuffer = i.second(sentenceBuffer, miscInfoLinkedList);
if (sentenceBuffer != prev) free((void*)prev); if (sentenceBuffer != prev) free((void*)prev);
} }
}
catch (...) { sentenceBuffer[0] = 0; }
extenMutex.unlock_shared(); extenMutex.unlock_shared();
delete miscInfoLinkedList; delete miscInfoLinkedList;
std::wstring newSentence = std::wstring(sentenceBuffer); sentence = std::wstring(sentenceBuffer);
free((void*)sentenceBuffer); free((void*)sentenceBuffer);
return newSentence; return sentence.size() > 0;
} }

View File

@ -5,7 +5,7 @@
#include <map> #include <map>
std::map<int, QString> LoadExtensions(); std::map<int, QString> LoadExtensions();
std::wstring DispatchSentenceToExtensions(std::wstring sentence, std::unordered_map<std::string, int> miscInfo); bool DispatchSentenceToExtensions(std::wstring& sentence, std::unordered_map<std::string, int> miscInfo);
struct InfoForExtension struct InfoForExtension
{ {
~InfoForExtension() { if (nextProperty) delete nextProperty; }; ~InfoForExtension() { if (nextProperty) delete nextProperty; };

View File

@ -84,9 +84,11 @@ void MainWindow::AddThread(TextThread* thread)
); );
thread->RegisterOutputCallBack([&](TextThread* thread, std::wstring output) thread->RegisterOutputCallBack([&](TextThread* thread, std::wstring output)
{ {
output = DispatchSentenceToExtensions(output, GetInfoForExtensions(thread)); if (DispatchSentenceToExtensions(output, GetInfoForExtensions(thread)))
{
output += L"\r\n"; output += L"\r\n";
emit SigThreadOutput(thread, QString::fromStdWString(output)); emit SigThreadOutput(thread, QString::fromStdWString(output));
}
return output; return output;
}); });
} }
@ -150,7 +152,8 @@ std::unordered_map<std::string, int> MainWindow::GetInfoForExtensions(TextThread
{ "text number", 0 }, { "text number", 0 },
{ "process id", thread->GetThreadParam().pid }, { "process id", thread->GetThreadParam().pid },
{ "hook address", (int)thread->GetThreadParam().hook }, { "hook address", (int)thread->GetThreadParam().hook },
{ "hook address (upper 32 bits)", (int)(thread->GetThreadParam().hook >> 32) } { "hook address (upper 32 bits)", (int)(thread->GetThreadParam().hook >> 32) },
{ "text handle", (int)thread }
}; };
} }