Fixed the ancient buffer overflows in Memory Watch.

This commit is contained in:
aquanull@gmail.com 2018-12-24 12:01:25 +08:00
parent 3b531d0d7c
commit fe64ca3771
2 changed files with 54 additions and 51 deletions

View File

@ -2242,7 +2242,7 @@ BOOL CALLBACK MemFindCallB(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam
break; break;
case WM_CLOSE: case WM_CLOSE:
case WM_QUIT: case WM_QUIT:
GetDlgItemText(hwndDlg,IDC_MEMVIEWFIND_WHAT,FindTextBox,59); GetDlgItemText(hwndDlg,IDC_MEMVIEWFIND_WHAT,FindTextBox,60);
DestroyWindow(hwndDlg); DestroyWindow(hwndDlg);
hMemFind = 0; hMemFind = 0;
hwndDlg = 0; hwndDlg = 0;
@ -2302,7 +2302,7 @@ void FindNext(){
unsigned char data[60]; unsigned char data[60];
int datasize = 0, i, j, inputc = -1, found; int datasize = 0, i, j, inputc = -1, found;
if(hMemFind) GetDlgItemText(hMemFind,IDC_MEMVIEWFIND_WHAT,str,59); if(hMemFind) GetDlgItemText(hMemFind,IDC_MEMVIEWFIND_WHAT,str,60);
else strcpy(str,FindTextBox); else strcpy(str,FindTextBox);
for(i = 0;str[i] != 0;i++){ for(i = 0;str[i] != 0;i++){

View File

@ -72,12 +72,12 @@ extern void RemoveRecentItem(unsigned int which, char**bufferArray, const unsign
//Ram change monitor globals----------------------------------- //Ram change monitor globals-----------------------------------
bool RamChangeInitialize = false; //Set true during memw WM_INIT bool RamChangeInitialize = false; //Set true during memw WM_INIT
const int MAX_RAMMONITOR = 4; //Maximum number of Ram values that can be monitored const int MAX_RAMMONITOR = 4; //Maximum number of Ram values that can be monitored
char editboxnow[MAX_RAMMONITOR][5]; //current address put into editbox 00 char editboxnow[MAX_RAMMONITOR][8]; //current address put into editbox 00
char editboxlast[MAX_RAMMONITOR][5]; //last address put into editbox (1 frame ago) char editboxlast[MAX_RAMMONITOR][8]; //last address put into editbox (1 frame ago)
int editlast[MAX_RAMMONITOR]; //last address value (1 frame ago) int editlast[MAX_RAMMONITOR]; //last address value (1 frame ago)
int editnow[MAX_RAMMONITOR]; //current address value int editnow[MAX_RAMMONITOR]; //current address value
unsigned int editcount[MAX_RAMMONITOR]; //Current counter value unsigned int editcount[MAX_RAMMONITOR]; //Current counter value
char editchangem[MAX_RAMMONITOR][5]; //counter converted to string char editchangem[MAX_RAMMONITOR][12]; //counter converted to string
//------------------------------------------------- //-------------------------------------------------
@ -286,15 +286,15 @@ void UpdateMemWatch()
//Display blue if address is frozen //Display blue if address is frozen
if (FrozenAddressCount && FrozenAddresses.size()) if (FrozenAddressCount && FrozenAddresses.size())
{ {
for (unsigned int x = 0; x < FrozenAddressCount; x++) for (unsigned int x = 0; x < FrozenAddressCount; x++)
{
if (mwrec.addr == FrozenAddresses[x])
{ {
//SetTextColor(hdc,RGB(0,0,255)); if (mwrec.addr == FrozenAddresses[x])
SetTextColor(hdc,GetSysColor(COLOR_HIGHLIGHT)); {
//SetTextColor(hdc,RGB(0,0,255));
SetTextColor(hdc,GetSysColor(COLOR_HIGHLIGHT));
}
} }
} }
}
char* text; char* text;
if(mwrec.valid && GameInfo) if(mwrec.valid && GameInfo)
@ -336,7 +336,6 @@ void UpdateMemWatch()
TextOut(hdc,0,0,text,strlen(text)); TextOut(hdc,0,0,text,strlen(text));
SetTextColor(hdc,RGB(0,0,0)); SetTextColor(hdc,RGB(0,0,0));
} }
} }
} }
@ -388,14 +387,14 @@ bool iftextchanged()
int i,j; int i,j;
for(i=0;i<NUMWATCHES;i++) for(i=0;i<NUMWATCHES;i++)
{ {
for(j=0;j<LABELLENGTH;j++) for(j=0;j<ADDRESSLENGTH;j++)
{ {
if(addresses[i][j] != NULL || labels [i][j] != NULL) if(addresses[i][j] != '\0' || labels [i][j] != '\0')
return true; return true;
} }
for(;j<LABELLENGTH;j++) for(;j<LABELLENGTH;j++)
{ {
if(labels[i][j] != NULL) if(labels[i][j] != '\0')
return true; return true;
} }
} }
@ -445,16 +444,16 @@ static void SaveMemWatch()
for(i=0;i<NUMWATCHES;i++) for(i=0;i<NUMWATCHES;i++)
{ {
//Use dummy strings to fill empty slots //Use dummy strings to fill empty slots
if(labels[i][0] == 0)
{
labels[i][0] = '|';
labels[i][1] = 0;
}
if(addresses[i][0] == 0) if(addresses[i][0] == 0)
{ {
addresses[i][0] = '|'; addresses[i][0] = '|';
addresses[i][1] = 0; addresses[i][1] = 0;
} }
if(labels[i][0] == 0)
{
labels[i][0] = '|';
labels[i][1] = 0;
}
//spaces can be a problem for scanf so get rid of them //spaces can be a problem for scanf so get rid of them
TakeOutSpaces(i); TakeOutSpaces(i);
fprintf(fp, "%s %s\n", addresses[i], labels[i]); fprintf(fp, "%s %s\n", addresses[i], labels[i]);
@ -476,16 +475,16 @@ static void QuickSaveMemWatch() //Save rather than Save as
for(int i=0;i<NUMWATCHES;i++) for(int i=0;i<NUMWATCHES;i++)
{ {
//Use dummy strings to fill empty slots //Use dummy strings to fill empty slots
if(labels[i][0] == 0)
{
labels[i][0] = '|';
labels[i][1] = 0;
}
if(addresses[i][0] == 0) if(addresses[i][0] == 0)
{ {
addresses[i][0] = '|'; addresses[i][0] = '|';
addresses[i][1] = 0; addresses[i][1] = 0;
} }
if(labels[i][0] == 0)
{
labels[i][0] = '|';
labels[i][1] = 0;
}
//spaces can be a problem for scanf so get rid of them //spaces can be a problem for scanf so get rid of them
TakeOutSpaces(i); TakeOutSpaces(i);
fprintf(fp, "%s %s\n", addresses[i], labels[i]); fprintf(fp, "%s %s\n", addresses[i], labels[i]);
@ -572,7 +571,7 @@ static void LoadMemWatch()
} }
fclose(fp); fclose(fp);
} }
fileChanged = false; fileChanged = false;
} }
//Loads a recent file given the recent files array number(0-4) //Loads a recent file given the recent files array number(0-4)
@ -592,33 +591,33 @@ void OpenMemwatchRecentFile(int memwRFileNumber)
MemwAddRecentFile(x); MemwAddRecentFile(x);
int i,j; int i,j;
for(i=0;i<NUMWATCHES;i++) for(i=0;i<NUMWATCHES;i++)
{
fscanf(fp, "%s ", watchfcontents); //Reads contents of newly opened file
for(j = 0; j < ADDRESSLENGTH; j++)
addresses[i][j] = watchfcontents[j];
fscanf(fp, "%s\n", watchfcontents);
for(j = 0; j < LABELLENGTH; j++)
labels[i][j] = watchfcontents[j];
//Replace dummy strings with empty strings
if(addresses[i][0] == '|')
{ {
fscanf(fp, "%s ", watchfcontents); //Reads contents of newly opened file addresses[i][0] = 0;
for(j = 0; j < ADDRESSLENGTH; j++) }
addresses[i][j] = watchfcontents[j]; if(labels[i][0] == '|')
fscanf(fp, "%s\n", watchfcontents); {
for(j = 0; j < LABELLENGTH; j++) labels[i][0] = 0;
labels[i][j] = watchfcontents[j]; }
PutInSpaces(i);
//Replace dummy strings with empty strings int templl = LABELLENGTH - 1;
if(addresses[i][0] == '|') int tempal = ADDRESSLENGTH - 1;
{ addresses[i][tempal] = 0;
addresses[i][0] = 0; labels[i][templl] = 0; //just in case
}
if(labels[i][0] == '|')
{
labels[i][0] = 0;
}
PutInSpaces(i);
int templl = LABELLENGTH - 1; SetDlgItemText(hwndMemWatch,MW_VAL (i),(LPTSTR) "---");
int tempal = ADDRESSLENGTH - 1; SetDlgItemText(hwndMemWatch,MW_ADDR(i),(LPTSTR) addresses[i]);
addresses[i][tempal] = 0; SetDlgItemText(hwndMemWatch,MW_NAME(i),(LPTSTR) labels[i]);
labels[i][templl] = 0; //just in case
SetDlgItemText(hwndMemWatch,MW_VAL (i),(LPTSTR) "---");
SetDlgItemText(hwndMemWatch,MW_ADDR(i),(LPTSTR) addresses[i]);
SetDlgItemText(hwndMemWatch,MW_NAME(i),(LPTSTR) labels[i]);
} }
fclose(fp); //Close the file fclose(fp); //Close the file
fileChanged = false; //Flag that the memwatch file has not been changed since last save fileChanged = false; //Flag that the memwatch file has not been changed since last save
@ -958,11 +957,15 @@ void CreateMemWatch()
int i,j; int i,j;
for(i=0;i<NUMWATCHES;i++) for(i=0;i<NUMWATCHES;i++)
{ {
for(j=0;j<LABELLENGTH;j++) for(j=0;j<ADDRESSLENGTH;j++)
{ {
addresses[i][j] = 0; addresses[i][j] = 0;
labels[i][j] = 0; labels[i][j] = 0;
} }
for (;j<LABELLENGTH;j++)
{
labels[i][j] = 0;
}
} }
} }