summaryrefslogtreecommitdiff
path: root/Userland/Applications/HexEditor
diff options
context:
space:
mode:
authorBrendan Coles <bcoles@gmail.com>2021-06-01 07:48:59 +0000
committerAndreas Kling <kling@serenityos.org>2021-06-01 12:23:43 +0200
commit996f69a1b27467f068ee7d06f007c9189fe61333 (patch)
tree29a6d001f622f895922798dcdd96e142b82884f2 /Userland/Applications/HexEditor
parent2e23954271a57324a57cceb61da91675eeae9643 (diff)
downloadserenity-996f69a1b27467f068ee7d06f007c9189fe61333.zip
HexEditor: Fix off-by-one bugs in selected text length calculations
find_and_highlight() selected +1 too many bytes. 'Select All' selected +1 too many bytes past the end of the buffer. Status bar 'Selected Bytes' count was off by -1 when more than zero bytes were selected.
Diffstat (limited to 'Userland/Applications/HexEditor')
-rw-r--r--Userland/Applications/HexEditor/HexEditor.cpp11
-rw-r--r--Userland/Applications/HexEditor/HexEditor.h1
-rw-r--r--Userland/Applications/HexEditor/HexEditorWidget.cpp2
3 files changed, 11 insertions, 3 deletions
diff --git a/Userland/Applications/HexEditor/HexEditor.cpp b/Userland/Applications/HexEditor/HexEditor.cpp
index 247b9bb3aa..1fdf40c982 100644
--- a/Userland/Applications/HexEditor/HexEditor.cpp
+++ b/Userland/Applications/HexEditor/HexEditor.cpp
@@ -113,6 +113,13 @@ bool HexEditor::write_to_file(const String& path)
return true;
}
+size_t HexEditor::selection_size()
+{
+ if (!has_selection())
+ return 0;
+ return abs(m_selection_end - m_selection_start) + 1;
+}
+
bool HexEditor::copy_selected_hex_to_clipboard()
{
if (!has_selection())
@@ -561,7 +568,7 @@ void HexEditor::paint_event(GUI::PaintEvent& event)
void HexEditor::select_all()
{
- highlight(0, m_buffer.size());
+ highlight(0, m_buffer.size() - 1);
set_position(0);
}
@@ -575,7 +582,7 @@ void HexEditor::highlight(int start, int end)
int HexEditor::find_and_highlight(ByteBuffer& needle, int start)
{
auto end_of_match = find(needle, start);
- highlight(end_of_match - needle.size(), end_of_match);
+ highlight(end_of_match - needle.size(), end_of_match - 1);
return end_of_match;
}
diff --git a/Userland/Applications/HexEditor/HexEditor.h b/Userland/Applications/HexEditor/HexEditor.h
index eb7764877b..9dbcfe7379 100644
--- a/Userland/Applications/HexEditor/HexEditor.h
+++ b/Userland/Applications/HexEditor/HexEditor.h
@@ -37,6 +37,7 @@ public:
void select_all();
bool has_selection() const { return !(m_selection_start == -1 || m_selection_end == -1 || (m_selection_end - m_selection_start) < 0 || m_buffer.is_empty()); }
+ size_t selection_size();
int selection_start_offset() const { return m_selection_start; }
bool copy_selected_text_to_clipboard();
bool copy_selected_hex_to_clipboard();
diff --git a/Userland/Applications/HexEditor/HexEditorWidget.cpp b/Userland/Applications/HexEditor/HexEditorWidget.cpp
index 5d08785881..20a3dc094b 100644
--- a/Userland/Applications/HexEditor/HexEditorWidget.cpp
+++ b/Userland/Applications/HexEditor/HexEditorWidget.cpp
@@ -51,7 +51,7 @@ HexEditorWidget::HexEditorWidget()
m_statusbar->set_text(1, String::formatted("Edit Mode: {}", edit_mode == HexEditor::EditMode::Hex ? "Hex" : "Text"));
m_statusbar->set_text(2, String::formatted("Selection Start: {}", selection_start));
m_statusbar->set_text(3, String::formatted("Selection End: {}", selection_end));
- m_statusbar->set_text(4, String::formatted("Selected Bytes: {}", abs(selection_end - selection_start)));
+ m_statusbar->set_text(4, String::formatted("Selected Bytes: {}", m_editor->selection_size()));
};
m_editor->on_change = [this] {