From 1a139628067cf0aed5a2b9dc6e7f84da77bcfa76 Mon Sep 17 00:00:00 2001 From: mrexodia Date: Thu, 2 Jun 2016 11:21:39 +0200 Subject: [PATCH] PROJECT: fixed various bugs (thanks to Coverity!) --- src/dbg/TraceRecord.cpp | 2 +- src/dbg/debugger_commands.cpp | 4 ++-- src/dbg/instruction.cpp | 8 +++++-- src/dbg/module.cpp | 2 +- src/dbg/simplescript.cpp | 31 ++++++++------------------ src/gui/Src/BasicView/SearchListView.h | 1 - src/gui/Src/Gui/CommandLineEdit.cpp | 8 ++++++- src/gui/Src/Gui/PatchDialog.cpp | 2 +- src/launcher/x64_dbg_launcher.cpp | 2 +- 9 files changed, 28 insertions(+), 32 deletions(-) diff --git a/src/dbg/TraceRecord.cpp b/src/dbg/TraceRecord.cpp index 136c9ae6..11be9c9f 100644 --- a/src/dbg/TraceRecord.cpp +++ b/src/dbg/TraceRecord.cpp @@ -68,7 +68,7 @@ bool TraceRecordManager::setTraceRecordType(duint pageAddress, TraceRecordType t auto inserted = TraceRecord.insert(std::make_pair(ModHashFromAddr(pageAddress), newPage)); if(inserted.second == false) // we failed to insert new page into the map { - free(newPage.rawPtr); + efree(newPage.rawPtr); return false; } return true; diff --git a/src/dbg/debugger_commands.cpp b/src/dbg/debugger_commands.cpp index baf3da3b..9e335a6b 100644 --- a/src/dbg/debugger_commands.cpp +++ b/src/dbg/debugger_commands.cpp @@ -2249,14 +2249,14 @@ CMDRESULT cbDebugLoadLib(int argc, char* argv[]) counter += size; SetContextDataEx(LoadLibThread, UE_CIP, (duint)ASMAddr); - SetBPX((duint)ASMAddr + counter, UE_SINGLESHOOT | UE_BREAKPOINT_TYPE_INT3, (void*)cbDebugLoadLibBPX); + auto ok = SetBPX((duint)ASMAddr + counter, UE_SINGLESHOOT | UE_BREAKPOINT_TYPE_INT3, (void*)cbDebugLoadLibBPX); ThreadSuspendAll(); ResumeThread(LoadLibThread); unlock(WAITID_RUN); - return STATUS_CONTINUE; + return ok ? STATUS_CONTINUE : STATUS_ERROR; } void cbDebugLoadLibBPX() diff --git a/src/dbg/instruction.cpp b/src/dbg/instruction.cpp index 8a1fc275..0d3e132f 100644 --- a/src/dbg/instruction.cpp +++ b/src/dbg/instruction.cpp @@ -190,7 +190,11 @@ CMDRESULT cbInstrMov(int argc, char* argv[]) b[0] = dataText[i]; b[1] = dataText[i + 1]; int res = 0; - sscanf_s(b, "%X", &res); + if(sscanf_s(b, "%X", &res) != 1) + { + dprintf("invalid hex byte \"%s\"\n", b); + return STATUS_ERROR; + } data()[j] = res; } //Move data to destination @@ -212,7 +216,7 @@ CMDRESULT cbInstrMov(int argc, char* argv[]) } bool isvar = false; duint temp = 0; - valfromstring(argv[1], &temp, true, false, 0, &isvar, 0); + valfromstring(argv[1], &temp, true, false, 0, &isvar, 0); //there is no return check on this because the destination might not exist yet if(!isvar) isvar = vargettype(argv[1], 0); if(!isvar || !valtostring(argv[1], set_value, true)) diff --git a/src/dbg/module.cpp b/src/dbg/module.cpp index 23a7b5c8..e920ed2f 100644 --- a/src/dbg/module.cpp +++ b/src/dbg/module.cpp @@ -387,7 +387,7 @@ bool ModAddImportToModule(duint Base, const MODIMPORTINFO & importInfo) // Search in Import Vector auto pImports = &(module->imports); - auto it = std::find_if(pImports->begin(), pImports->end(), [importInfo](const MODIMPORTINFO & currentImportInfo)->bool + auto it = std::find_if(pImports->begin(), pImports->end(), [&importInfo](const MODIMPORTINFO & currentImportInfo)->bool { return (importInfo.addr == currentImportInfo.addr); }); diff --git a/src/dbg/simplescript.cpp b/src/dbg/simplescript.cpp index 9ec64cf1..8cd2722b 100644 --- a/src/dbg/simplescript.cpp +++ b/src/dbg/simplescript.cpp @@ -9,6 +9,7 @@ #include "variable.h" #include "x64_dbg.h" #include "debugger.h" +#include "filehelper.h" static std::vector linemap; @@ -74,34 +75,20 @@ static int scriptinternalstep(int fromIp) //internal step routine static bool scriptcreatelinemap(const char* filename) { - Handle hFile = CreateFileW(StringUtils::Utf8ToUtf16(filename).c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0); - if(hFile == INVALID_HANDLE_VALUE) + String filedata; + if(FileHelper::ReadAllText(filename, filedata)) { - GuiScriptError(0, "CreateFile failed..."); + GuiScriptError(0, "FileHelper::ReadAllText failed..."); return false; } - unsigned int filesize = GetFileSize(hFile, 0); - if(!filesize) - { - GuiScriptError(0, "Empty script..."); - return false; - } - Memory filedata(filesize + 1, "createlinemap:filedata"); - DWORD read = 0; - if(!ReadFile(hFile, filedata(), filesize, &read, 0)) - { - GuiScriptError(0, "ReadFile failed..."); - return false; - } - hFile.Close(); - int len = (int)strlen(filedata()); + auto len = filedata.length(); char temp[256] = ""; LINEMAPENTRY entry; memset(&entry, 0, sizeof(entry)); std::vector().swap(linemap); - for(int i = 0, j = 0; i < len; i++) //make raw line map + for(size_t i = 0, j = 0; i < len; i++) //make raw line map { - if(filedata()[i] == '\r' && filedata()[i + 1] == '\n') //windows file + if(filedata[i] == '\r' && filedata[i + 1] == '\n') //windows file { memset(&entry, 0, sizeof(entry)); int add = 0; @@ -113,7 +100,7 @@ static bool scriptcreatelinemap(const char* filename) i++; linemap.push_back(entry); } - else if(filedata()[i] == '\n') //other file + else if(filedata[i] == '\n') //other file { memset(&entry, 0, sizeof(entry)); int add = 0; @@ -136,7 +123,7 @@ static bool scriptcreatelinemap(const char* filename) linemap.push_back(entry); } else - j += sprintf(temp + j, "%c", filedata()[i]); + j += sprintf(temp + j, "%c", filedata[i]); } if(*temp) { diff --git a/src/gui/Src/BasicView/SearchListView.h b/src/gui/Src/BasicView/SearchListView.h index c4dd5b39..4bbce8ce 100644 --- a/src/gui/Src/BasicView/SearchListView.h +++ b/src/gui/Src/BasicView/SearchListView.h @@ -44,7 +44,6 @@ protected: private: QCheckBox* mRegexCheckbox; - QWidget* mListPlaceHolder; QAction* mSearchAction; }; diff --git a/src/gui/Src/Gui/CommandLineEdit.cpp b/src/gui/Src/Gui/CommandLineEdit.cpp index 7e56fea8..9730d8c6 100644 --- a/src/gui/Src/Gui/CommandLineEdit.cpp +++ b/src/gui/Src/Gui/CommandLineEdit.cpp @@ -1,7 +1,9 @@ #include "CommandLineEdit.h" #include "Bridge.h" -CommandLineEdit::CommandLineEdit(QWidget* parent) : HistoryLineEdit(parent) +CommandLineEdit::CommandLineEdit(QWidget* parent) + : HistoryLineEdit(parent), + mCurrentScriptIndex(-1) { // QComboBox mCmdScriptType = new QComboBox(this); @@ -77,6 +79,8 @@ bool CommandLineEdit::focusNextPrevChild(bool next) void CommandLineEdit::execute() { + if(mCurrentScriptIndex == -1) + return; GUISCRIPTEXECUTE exec = mScriptInfo[mCurrentScriptIndex].execute; const QString & cmd = text(); @@ -99,6 +103,8 @@ QWidget* CommandLineEdit::selectorWidget() void CommandLineEdit::autoCompleteUpdate(const QString text) { + if(mCurrentScriptIndex == -1) + return; // No command, no completer if(text.length() <= 0) { diff --git a/src/gui/Src/Gui/PatchDialog.cpp b/src/gui/Src/Gui/PatchDialog.cpp index 032314a4..e9a3195e 100644 --- a/src/gui/Src/Gui/PatchDialog.cpp +++ b/src/gui/Src/Gui/PatchDialog.cpp @@ -643,7 +643,7 @@ void PatchDialog::on_btnExport_clicked() // TODO: C program source } -void PatchDialog::saveAs1337(const QString &filename) +void PatchDialog::saveAs1337(const QString & filename) { QStringList lines; diff --git a/src/launcher/x64_dbg_launcher.cpp b/src/launcher/x64_dbg_launcher.cpp index d5a0f22b..197b0baf 100644 --- a/src/launcher/x64_dbg_launcher.cpp +++ b/src/launcher/x64_dbg_launcher.cpp @@ -109,7 +109,7 @@ static HRESULT AddDesktopShortcut(TCHAR* szPathOfFile, const TCHAR* szNameOfLink //Get the working directory TCHAR pathFile[MAX_PATH + 1]; - _tcscpy(pathFile, szPathOfFile); + _tcscpy_s(pathFile, szPathOfFile); PathRemoveFileSpec(pathFile); CComPtr psl;