Debugger address validation, fix for #444

This commit is contained in:
Alexey 'Cluster' Avdyukhin 2022-02-09 20:06:22 +03:00
parent a75c27d517
commit 132e062d77
1 changed files with 39 additions and 27 deletions

View File

@ -1696,35 +1696,47 @@ int Debugger_CheckClickingOnAnAddressOrSymbolicName(unsigned int lineNumber, boo
if (sel_end > sel_start) if (sel_end > sel_start)
return EOF; return EOF;
// find the ":" or "$" before sel_start // check for the hex address value
int i = sel_start - 1; for (int i = sel_start - 1; (i > sel_start - 6) && (i >= 0); i--)
for (; i > sel_start - 6; i--)
if ((i >= 0 && debug_wstr[i] == L':' || debug_wstr[i] == L'$') && debug_wstr[i+3] != L'\n')
break;
if (i > sel_start - 6)
{ {
wchar_t offsetBuffer[5]; // find the first character before hex value
wcsncpy(offsetBuffer, debug_wstr + i + 1, 4); if (!((debug_wstr[i] >= '0' && debug_wstr[i] <= '9') || (debug_wstr[i] >= 'A' && debug_wstr[i] <= 'F')))
offsetBuffer[4] = 0;
// invalidate the string if a space or \r is found in it
wchar_t* firstspace = wcsstr(offsetBuffer, L" ");
if (!firstspace)
firstspace = wcsstr(offsetBuffer, L"\r");
if (!firstspace)
{ {
int hex_pos = i + 1;
int hex_len = 0;
unsigned int offset; unsigned int offset;
int numend;
if (swscanf(offsetBuffer, L"%4X", &offset) != EOF) // find length of the hex string
{ while (
if (debug_wstr[i + 3] == L',' || debug_wstr[i+3] == L')') (debug_wstr[hex_pos + hex_len] >= '0' && debug_wstr[hex_pos + hex_len] <= '9') ||
numend = 3; (debug_wstr[hex_pos + hex_len] >= 'A' && debug_wstr[hex_pos + hex_len] <= 'F')
else ) hex_len++;
numend = 5; // validate length of the value
// select the text if ((hex_len != 2) && (hex_len != 4)) break;
SendDlgItemMessage(hDebug, IDC_DEBUGGER_DISASSEMBLY, EM_SETSEL, (WPARAM)(i + 1), (LPARAM)(i + numend)); // validate symbol before the hex value
PrintOffsetToSeekAndBookmarkFields(offset); if (
return (int)offset; (debug_wstr[i] != L':') && // ":XX" or ":XXXX"
} (debug_wstr[i] != L'$') // "$XX" or "$XXXX"
) break;
// block "#$XX" pattern
if (
(i > 0) &&
(debug_wstr[i] == L'$') &&
(debug_wstr[i - 1] == L'#')
) break;
// validate symbol after the hex value
if (
((debug_wstr[hex_pos + hex_len] != L':') || (hex_len != 4)) && // opcode address
(debug_wstr[hex_pos + hex_len] != L',') &&
(debug_wstr[hex_pos + hex_len] != L')') &&
(debug_wstr[hex_pos + hex_len] != L' ') &&
(debug_wstr[hex_pos + hex_len] != L'\n')
) break;
if (swscanf(&debug_wstr[hex_pos], (hex_len == 2) ? L"%2X" : L"%4X", &offset) == EOF) break;
// select the text
SendDlgItemMessage(hDebug, IDC_DEBUGGER_DISASSEMBLY, EM_SETSEL, (WPARAM)(hex_pos), (LPARAM)(hex_pos + hex_len));
PrintOffsetToSeekAndBookmarkFields(offset);
return (int)offset;
} }
} }
@ -1781,7 +1793,7 @@ int Debugger_CheckClickingOnAnAddressOrSymbolicName(unsigned int lineNumber, boo
{ {
// clicked on the operand name // clicked on the operand name
// select the text // select the text
SendDlgItemMessage(hDebug, IDC_DEBUGGER_DISASSEMBLY, EM_SETSEL, (WPARAM)(int)(pos - debug_wstr), (LPARAM)((int)(pos - debug_wstr) + nameLen)); SendDlgItemMessage(hDebug, IDC_DEBUGGER_DISASSEMBLY, EM_SETSEL, (WPARAM)(int)(pos - debug_wstr), (LPARAM)((int)(pos - debug_wstr + nameLen - 1)));
PrintOffsetToSeekAndBookmarkFields(addr); PrintOffsetToSeekAndBookmarkFields(addr);
return (int)addr; return (int)addr;
} }