Skip to content

Commit 5a214ab

Browse files
committed
DBG: fixed all kinds of small coding bugs (thanks to Coverity)
1 parent 8e795bf commit 5a214ab

14 files changed

+47
-39
lines changed

x64_dbg_dbg/_exports.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ extern "C" DLL_EXPORT bool _dbg_addrinfoget(duint addr, SEGMENTREG segment, ADDR
108108
if(SymFromAddr(fdProcessInfo->hProcess, (DWORD64)addr, &displacement, pSymbol) and !displacement)
109109
{
110110
if(settingboolget("Engine", "UndecorateSymbolNames") or !UnDecorateSymbolName(pSymbol->Name, addrinfo->label, MAX_LABEL_SIZE, UNDNAME_COMPLETE))
111-
strcpy(addrinfo->label, pSymbol->Name);
111+
strcpy_s(addrinfo->label, pSymbol->Name);
112112
retval = true;
113113
}
114114
if(!retval) //search for CALL <jmp.&user32.MessageBoxA>

x64_dbg_dbg/_global.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ int memleaks()
7878

7979
void setalloctrace(const char* file)
8080
{
81-
strcpy(alloctrace, file);
81+
strcpy_s(alloctrace, file);
8282
}
8383

8484
bool arraycontains(const char* cmd_list, const char* cmd)
@@ -87,7 +87,7 @@ bool arraycontains(const char* cmd_list, const char* cmd)
8787
if(!cmd_list or !cmd)
8888
return false;
8989
char temp[deflen] = "";
90-
strcpy(temp, cmd_list);
90+
strcpy_s(temp, cmd_list);
9191
int len = (int)strlen(cmd_list);
9292
if(len >= deflen)
9393
return false;

x64_dbg_dbg/addrinfo.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ void commentcacheload(JSON root)
482482
curComment.manual = false;
483483
const char* text = json_string_value(json_object_get(value, "text"));
484484
if(text)
485-
strcpy(curComment.text, text);
485+
strcpy_s(curComment.text, text);
486486
else
487487
continue; //skip
488488
const uint key = modhashfromname(curComment.mod) + curComment.addr;
@@ -664,7 +664,7 @@ void labelcacheload(JSON root)
664664
curLabel.manual = false;
665665
const char* text = json_string_value(json_object_get(value, "text"));
666666
if(text)
667-
strcpy(curLabel.text, text);
667+
strcpy_s(curLabel.text, text);
668668
else
669669
continue; //skip
670670
const uint key = modhashfromname(curLabel.mod) + curLabel.addr;

x64_dbg_dbg/argument.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void argformat(char* cmd)
4444
start = len;
4545
char arguments_[deflen] = "";
4646
char* arguments = arguments_;
47-
strcpy(arguments, command + start);
47+
strcpy_s(arguments, deflen, command + start);
4848
char temp[deflen] = "";
4949
len = (int)strlen(arguments);
5050
for(int i = 0, j = 0; i < len; i++)
@@ -53,7 +53,7 @@ void argformat(char* cmd)
5353
i += 2;
5454
j += sprintf(temp + j, "%c", arguments[i]);
5555
}
56-
strcpy(arguments, temp);
56+
strcpy_s(arguments, deflen, temp);
5757
len = (int)strlen(arguments);
5858
for(int i = 0; i < len; i++)
5959
if(arguments[i] == '\\' and arguments[i + 1] == '\\')
@@ -79,7 +79,7 @@ void argformat(char* cmd)
7979
arguments[i] = 0;
8080

8181
for(int i = 0; i < len; i++)
82-
if(arguments[i] == 1 and arguments[i + 1] == 1)
82+
if(arguments[i] == 1 and (i < len - 1 and arguments[i + 1] == 1))
8383
{
8484
arguments[i] = '\\';
8585
arguments[i + 1] = '\\';
@@ -164,7 +164,7 @@ int arggetcount(const char* cmd)
164164
char* temp = temp_ + 1;
165165
strcpy(temp, cmd);
166166
for(int i = start; i < len; i++)
167-
if(temp[i] == '\\' and temp[i + 1] == '\\')
167+
if(temp[i] == '\\' and (i < len - 1 and temp[i + 1] == '\\'))
168168
{
169169
temp[i] = 1;
170170
temp[i + 1] = 1;

x64_dbg_dbg/breakpoint.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ bool bpnew(uint addr, bool enabled, bool singleshoot, short oldbytes, BP_TYPE ty
3636
bp.addr = addr - modbase;
3737
bp.enabled = enabled;
3838
if(name and * name)
39-
strcpy(bp.name, name);
39+
strcpy_s(bp.name, name);
4040
else
4141
*bp.name = '\0';
4242
bp.oldbytes = oldbytes;
@@ -110,7 +110,7 @@ bool bpsetname(uint addr, BP_TYPE type, const char* name)
110110
BreakpointsInfo::iterator found = breakpoints.find(BreakpointKey(type, modhashfromva(addr)));
111111
if(found == breakpoints.end()) //not found
112112
return false;
113-
strcpy(breakpoints[found->first].name, name);
113+
strcpy_s(breakpoints[found->first].name, name);
114114
return true;
115115
}
116116

@@ -193,8 +193,10 @@ void bptobridge(const BREAKPOINT* bp, BRIDGEBP* bridge)
193193
break;
194194
case BPMEMORY:
195195
bridge->type = bp_memory;
196+
break; //so that's why it didn't show in the gui.
196197
default:
197198
bridge->type = bp_none;
199+
break;
198200
}
199201
}
200202

@@ -242,10 +244,10 @@ void bpcacheload(JSON root)
242244
curBreakpoint.titantype = (DWORD)json_hex_value(json_object_get(value, "titantype"));
243245
const char* name = json_string_value(json_object_get(value, "name"));
244246
if(name)
245-
strcpy(curBreakpoint.name, name);
247+
strcpy_s(curBreakpoint.name, name);
246248
const char* mod = json_string_value(json_object_get(value, "module"));
247249
if(mod && *mod && strlen(mod) < MAX_MODULE_SIZE)
248-
strcpy(curBreakpoint.mod, mod);
250+
strcpy_s(curBreakpoint.mod, mod);
249251
const uint key = modhashfromname(curBreakpoint.mod) + curBreakpoint.addr;
250252
breakpoints.insert(std::make_pair(BreakpointKey(curBreakpoint.type, key), curBreakpoint));
251253
}

x64_dbg_dbg/command.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ bool cmdnew(COMMAND* command_list, const char* name, CBCOMMAND cbCommand, bool d
7575
COMMAND* cmdget(COMMAND* command_list, const char* cmd)
7676
{
7777
char new_cmd[deflen] = "";
78-
strcpy(new_cmd, cmd);
78+
strcpy_s(new_cmd, cmd);
7979
int len = (int)strlen(new_cmd);
8080
int start = 0;
8181
while(new_cmd[start] != ' ' and start < len)

x64_dbg_dbg/debugger.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -661,11 +661,11 @@ static void cbCreateProcess(CREATE_PROCESS_DEBUG_INFO* CreateProcessInfo)
661661
len--;
662662
if(len)
663663
len++;
664-
strcpy(sqlitedb, szFileName + len);
664+
strcpy_s(sqlitedb, szFileName + len);
665665
#ifdef _WIN64
666-
strcat(sqlitedb, ".dd64");
666+
strcat_s(sqlitedb, ".dd64");
667667
#else
668-
strcat(sqlitedb, ".dd32");
668+
strcat_s(sqlitedb, ".dd32");
669669
#endif // _WIN64
670670
sprintf(dbpath, "%s\\%s", dbbasepath, sqlitedb);
671671
dprintf("Database file: %s\n", dbpath);
@@ -1171,7 +1171,7 @@ DWORD WINAPI threadDebugLoop(void* lpParameter)
11711171
INIT_STRUCT* init = (INIT_STRUCT*)lpParameter;
11721172
bFileIsDll = IsFileDLL(init->exe, 0);
11731173
pDebuggedEntry = GetPE32Data(init->exe, 0, UE_OEP);
1174-
strcpy(szFileName, init->exe);
1174+
strcpy_s(szFileName, init->exe);
11751175
if(bFileIsDll)
11761176
fdProcessInfo = (PROCESS_INFORMATION*)InitDLLDebug(init->exe, false, init->commandline, init->currentfolder, 0);
11771177
else
@@ -1429,12 +1429,12 @@ DWORD WINAPI threadAttachLoop(void* lpParameter)
14291429
//inform GUI start we started without problems
14301430
GuiSetDebugState(initialized);
14311431
//set GUI title
1432-
strcpy(szBaseFileName, szFileName);
1432+
strcpy_s(szBaseFileName, szFileName);
14331433
int len = (int)strlen(szBaseFileName);
14341434
while(szBaseFileName[len] != '\\' and len)
14351435
len--;
14361436
if(len)
1437-
strcpy(szBaseFileName, szBaseFileName + len + 1);
1437+
strcpy_s(szBaseFileName, szBaseFileName + len + 1);
14381438
GuiUpdateWindowTitle(szBaseFileName);
14391439
//call plugin callback (init)
14401440
PLUG_CB_INITDEBUG initInfo;
@@ -1706,7 +1706,7 @@ bool dbggetjitauto(bool* auto_on, arch arch_in, arch* arch_out, readwritejitkey_
17061706

17071707
if(_readwritejitkey(jit_entry, & jit_entry_size, "Auto", arch_in, arch_out, & rw_error, false) == false)
17081708
{
1709-
if(rw_error = ERROR_RW_FILE_NOT_FOUND)
1709+
if(rw_error == ERROR_RW_FILE_NOT_FOUND)
17101710
{
17111711
if(rw_error_out != NULL)
17121712
* rw_error_out = rw_error;
@@ -1738,7 +1738,7 @@ bool dbgsetjitauto(bool auto_on, arch arch_in, arch* arch_out, readwritejitkey_e
17381738

17391739
if(_readwritejitkey(jit_entry, & jit_entry_size, "Auto", arch_in, arch_out, & rw_error, false) == false)
17401740
{
1741-
if(rw_error = ERROR_RW_FILE_NOT_FOUND)
1741+
if(rw_error == ERROR_RW_FILE_NOT_FOUND)
17421742
return true;
17431743
}
17441744
}

x64_dbg_dbg/debugger_commands.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -812,13 +812,15 @@ static DWORD WINAPI scyllaThread(void* lpParam)
812812
{
813813
dputs("error loading Scylla.dll!");
814814
bScyllaLoaded = false;
815+
FreeLibrary(hScylla);
815816
return 0;
816817
}
817818
ScyllaStartGui = (SCYLLASTARTGUI)GetProcAddress(hScylla, "ScyllaStartGui");
818819
if(!ScyllaStartGui)
819820
{
820821
dputs("could not find export 'ScyllaStartGui' inside Scylla.dll");
821822
bScyllaLoaded = false;
823+
FreeLibrary(hScylla);
822824
return 0;
823825
}
824826
if(dbgisdll())
@@ -1324,7 +1326,7 @@ CMDRESULT cbDebugDisableMemoryBreakpoint(int argc, char* argv[])
13241326

13251327
CMDRESULT cbDebugDownloadSymbol(int argc, char* argv[])
13261328
{
1327-
char szDefaultStore[MAX_PATH] = "";
1329+
char szDefaultStore[MAX_SETTING_SIZE] = "";
13281330
const char* szSymbolStore = szDefaultStore;
13291331
if(!BridgeSettingGet("Symbols", "DefaultStore", szDefaultStore)) //get default symbol store from settings
13301332
{
@@ -1390,8 +1392,8 @@ CMDRESULT cbDebugDownloadSymbol(int argc, char* argv[])
13901392

13911393
CMDRESULT cbDebugGetJITAuto(int argc, char* argv[])
13921394
{
1393-
bool jit_auto;
1394-
arch actual_arch;
1395+
bool jit_auto = false;
1396+
arch actual_arch = invalid;
13951397

13961398
if(argc == 1)
13971399
{
@@ -1514,17 +1516,17 @@ CMDRESULT cbDebugSetJITAuto(int argc, char* argv[])
15141516

15151517
CMDRESULT cbDebugSetJIT(int argc, char* argv[])
15161518
{
1517-
arch actual_arch;
1518-
char* jit_debugger_cmd;
1519+
arch actual_arch = invalid;
1520+
char* jit_debugger_cmd = "";
15191521
char oldjit[MAX_SETTING_SIZE] = "";
1522+
char path[JIT_ENTRY_DEF_SIZE];
15201523
if(!IsProcessElevated())
15211524
{
15221525
dprintf("Error run the debugger as Admin to setjit\n");
15231526
return STATUS_ERROR;
15241527
}
15251528
if(argc < 2)
15261529
{
1527-
char path[JIT_ENTRY_DEF_SIZE];
15281530
dbggetdefjit(path);
15291531

15301532
jit_debugger_cmd = path;

x64_dbg_dbg/math.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ static bool printlayer(char* exp, EXPRESSION* exps, int layer, bool silent, bool
310310
int len = close - open;
311311
strncpy(temp, exp + open + 1, len - 1);
312312

313-
strcpy(backup, exp + open + len + 1);
313+
strcpy_s(backup, exp + open + len + 1);
314314

315315
uint value;
316316
if(!mathfromstring(temp, &value, silent, baseonly, 0, 0))

x64_dbg_dbg/simplescript.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ static bool volatile bIsRunning = false;
1717
static SCRIPTBRANCHTYPE scriptgetbranchtype(const char* text)
1818
{
1919
char newtext[MAX_SCRIPT_LINE_SIZE] = "";
20-
strcpy(newtext, text);
20+
strcpy_s(newtext, text);
2121
argformat(newtext); //format jump commands
2222
if(!strstr(newtext, " "))
2323
strcat(newtext, " ");
@@ -157,7 +157,7 @@ static bool scriptcreatelinemap(const char* filename)
157157
}
158158
else //no space before comment
159159
{
160-
strcpy(line_comment, comment);
160+
strcpy_s(line_comment, comment);
161161
*comment = 0;
162162
}
163163
}
@@ -177,7 +177,9 @@ static bool scriptcreatelinemap(const char* filename)
177177
cur.type = linelabel;
178178
sprintf(cur.u.label, "l %.*s", rawlen - 1, cur.raw); //create a fake command for formatting
179179
argformat(cur.u.label); //format labels
180-
strcpy(cur.u.label, cur.u.label + 2); //remove fake command
180+
char temp[256] = "";
181+
strcpy_s(temp, cur.u.label + 2);
182+
strcpy_s(cur.u.label, temp); //remove fake command
181183
if(!*cur.u.label or !strcmp(cur.u.label, "\"\"")) //no label text
182184
{
183185
char message[256] = "";
@@ -341,7 +343,7 @@ static CMDRESULT scriptinternalcmdexec(const char* cmd)
341343
else if(scriptisinternalcommand(cmd, "nop")) //do nothing
342344
return STATUS_CONTINUE;
343345
char command[deflen] = "";
344-
strcpy(command, cmd);
346+
strcpy_s(command, cmd);
345347
argformat(command);
346348
COMMAND* found = cmdfindmain(dbggetcommandlist(), command);
347349
if(!found) //invalid command
@@ -495,7 +497,7 @@ static DWORD WINAPI scriptLoadThread(void* filename)
495497
void scriptload(const char* filename)
496498
{
497499
static char filename_[MAX_PATH] = "";
498-
strcpy(filename_, filename);
500+
strcpy_s(filename_, filename);
499501
CloseHandle(CreateThread(0, 0, scriptLoadThread, filename_, 0, 0));
500502
}
501503

x64_dbg_dbg/symbolinfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ const char* symgetsymbolicname(uint addr)
156156
if(SymFromAddr(fdProcessInfo->hProcess, (DWORD64)addr, &displacement, pSymbol) and !displacement)
157157
{
158158
if(!settingboolget("Engine", "UndecorateSymbolNames") or !UnDecorateSymbolName(pSymbol->Name, label, MAX_SYM_NAME, UNDNAME_COMPLETE))
159-
strcpy(label, pSymbol->Name);
159+
strcpy_s(label, pSymbol->Name);
160160
retval = true;
161161
}
162162
}

x64_dbg_dbg/thread.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ bool threadsetname(DWORD dwThreadId, const char* name)
9393
if(threadList.at(i).dwThreadId == dwThreadId)
9494
{
9595
if(name)
96-
strcpy(threadList.at(i).threadName, name);
96+
strcpy_s(threadList.at(i).threadName, name);
9797
else
9898
*threadList.at(i).threadName = '\0';
9999
}

x64_dbg_dbg/value.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,7 @@ bool valapifromstring(const char* name, uint* value, int* value_size, bool print
10031003
int len = (int)strlen(szModName);
10041004
while(szModName[len] != '\\')
10051005
len--;
1006-
strcpy(szBaseName, szModName + len + 1);
1006+
strcpy_s(szBaseName, szModName + len + 1);
10071007
HMODULE mod = LoadLibraryExA(szModName, 0, DONT_RESOLVE_DLL_REFERENCES | LOAD_LIBRARY_AS_DATAFILE);
10081008
if(!mod)
10091009
{

x64_dbg_dbg/variable.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ bool varget(const char* name, uint* value, int* size, VAR_TYPE* type)
150150
return true; //variable was valid, just get the size
151151
if(type)
152152
*type = vartype;
153-
*value = varvalue.u.value;
153+
if(value)
154+
*value = varvalue.u.value;
154155
return true;
155156
}
156157

@@ -167,7 +168,8 @@ bool varget(const char* name, char* string, int* size, VAR_TYPE* type)
167168
return true; //variable was valid, just get the size
168169
if(type)
169170
*type = vartype;
170-
memcpy(string, &varvalue.u.data->front(), varsize);
171+
if(string)
172+
memcpy(string, &varvalue.u.data->front(), varsize);
171173
return true;
172174
}
173175

0 commit comments

Comments
 (0)