1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 15:37:46 +00:00

HexEditor: Fix out of bounds cursor

Fixing out of bounds cursor in three different cases:
- when the buffer is empty
- when loading new files
- when entering values at the end of the buffer
This commit is contained in:
Tibor Nagy 2020-02-19 08:56:57 +01:00 committed by Andreas Kling
parent a31ca1282e
commit 97878dfb4d

View file

@ -67,6 +67,8 @@ void HexEditor::set_buffer(const ByteBuffer& buffer)
{
m_buffer = buffer;
set_content_length(buffer.size());
m_position = 0;
m_byte_position = 0;
update();
update_status();
}
@ -417,6 +419,10 @@ void HexEditor::keydown_event(GUI::KeyEvent& event)
void HexEditor::hex_mode_keydown_event(GUI::KeyEvent& event)
{
if ((event.key() >= KeyCode::Key_0 && event.key() <= KeyCode::Key_9) || (event.key() >= KeyCode::Key_A && event.key() <= KeyCode::Key_F)) {
if (m_buffer.is_empty())
return;
ASSERT(m_position >= 0);
ASSERT(m_position < m_buffer.size());
// yes, this is terrible... but it works.
auto value = (event.key() >= KeyCode::Key_0 && event.key() <= KeyCode::Key_9)
@ -429,7 +435,8 @@ void HexEditor::hex_mode_keydown_event(GUI::KeyEvent& event)
m_byte_position++;
} else {
m_buffer.data()[m_position] = (m_buffer.data()[m_position] & 0xF0) | value; // save the first 4 bits, OR the new value in the last 4
m_position++;
if (m_position + 1 < m_buffer.size())
m_position++;
m_byte_position = 0;
}
@ -441,10 +448,17 @@ void HexEditor::hex_mode_keydown_event(GUI::KeyEvent& event)
void HexEditor::text_mode_keydown_event(GUI::KeyEvent& event)
{
if (m_buffer.is_empty())
return;
ASSERT(m_position >= 0);
ASSERT(m_position < m_buffer.size());
m_tracked_changes.set(m_position, m_buffer.data()[m_position]);
m_buffer.data()[m_position] = (u8)event.text().characters()[0]; // save the first 4 bits, OR the new value in the last 4
m_position++;
if (m_position + 1 < m_buffer.size())
m_position++;
m_byte_position = 0;
update();
update_status();
did_change();