From 23df5748f6d05b5b2255dd3859342734e5c71bcf Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Sat, 24 Jun 2023 12:44:22 +1200 Subject: [PATCH] LibDiff: Make Diff::parse_hunks fallible Currently the only error that can happen is an OOM. However, in the future there may be other errors that this function may throw, such as detecting an invalid patch. --- Userland/DevTools/HackStudio/EditorWrapper.cpp | 2 +- Userland/DevTools/HackStudio/Git/DiffViewer.cpp | 4 ++-- Userland/Libraries/LibDiff/Hunks.cpp | 11 ++++++----- Userland/Libraries/LibDiff/Hunks.h | 2 +- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/Userland/DevTools/HackStudio/EditorWrapper.cpp b/Userland/DevTools/HackStudio/EditorWrapper.cpp index ea104594db..9d377013ff 100644 --- a/Userland/DevTools/HackStudio/EditorWrapper.cpp +++ b/Userland/DevTools/HackStudio/EditorWrapper.cpp @@ -88,7 +88,7 @@ void EditorWrapper::save() void EditorWrapper::update_diff() { if (m_git_repo) { - m_hunks = Diff::parse_hunks(m_git_repo->unstaged_diff(filename()).value()); + m_hunks = Diff::parse_hunks(m_git_repo->unstaged_diff(filename()).value()).release_value_but_fixme_should_propagate_errors(); editor().update_git_diff_indicators().release_value_but_fixme_should_propagate_errors(); } } diff --git a/Userland/DevTools/HackStudio/Git/DiffViewer.cpp b/Userland/DevTools/HackStudio/Git/DiffViewer.cpp index 2eb501b6cf..078289e0d5 100644 --- a/Userland/DevTools/HackStudio/Git/DiffViewer.cpp +++ b/Userland/DevTools/HackStudio/Git/DiffViewer.cpp @@ -134,7 +134,7 @@ Gfx::IntRect DiffViewer::separator_rect() const void DiffViewer::set_content(DeprecatedString const& original, DeprecatedString const& diff) { m_original_lines = split_to_lines(original); - m_hunks = Diff::parse_hunks(diff); + m_hunks = Diff::parse_hunks(diff).release_value_but_fixme_should_propagate_errors(); if constexpr (DIFF_DEBUG) { for (size_t i = 0; i < m_original_lines.size(); ++i) @@ -149,7 +149,7 @@ DiffViewer::DiffViewer() DiffViewer::DiffViewer(DeprecatedString const& original, DeprecatedString const& diff) : m_original_lines(split_to_lines(original)) - , m_hunks(Diff::parse_hunks(diff)) + , m_hunks(Diff::parse_hunks(diff).release_value_but_fixme_should_propagate_errors()) { setup_properties(); } diff --git a/Userland/Libraries/LibDiff/Hunks.cpp b/Userland/Libraries/LibDiff/Hunks.cpp index 1482103841..ae029d61c5 100644 --- a/Userland/Libraries/LibDiff/Hunks.cpp +++ b/Userland/Libraries/LibDiff/Hunks.cpp @@ -8,11 +8,12 @@ #include namespace Diff { -Vector parse_hunks(DeprecatedString const& diff) + +ErrorOr> parse_hunks(DeprecatedString const& diff) { Vector diff_lines = diff.split('\n'); if (diff_lines.is_empty()) - return {}; + return Vector {}; Vector hunks; @@ -40,12 +41,12 @@ Vector parse_hunks(DeprecatedString const& diff) hunk.target_start_line = current_location.target_start_line; while (line_index < diff_lines.size() && diff_lines[line_index][0] == '-') { - hunk.removed_lines.append(diff_lines[line_index].substring(1, diff_lines[line_index].length() - 1)); + TRY(hunk.removed_lines.try_append(diff_lines[line_index].substring(1, diff_lines[line_index].length() - 1))); current_location.apply_offset(1, HunkLocation::LocationType::Original); ++line_index; } while (line_index < diff_lines.size() && diff_lines[line_index][0] == '+') { - hunk.added_lines.append(diff_lines[line_index].substring(1, diff_lines[line_index].length() - 1)); + TRY(hunk.added_lines.try_append(diff_lines[line_index].substring(1, diff_lines[line_index].length() - 1))); current_location.apply_offset(1, HunkLocation::LocationType::Target); ++line_index; } @@ -54,7 +55,7 @@ Vector parse_hunks(DeprecatedString const& diff) current_location.apply_offset(1, HunkLocation::LocationType::Both); ++line_index; } - hunks.append(hunk); + TRY(hunks.try_append(hunk)); } if constexpr (HUNKS_DEBUG) { diff --git a/Userland/Libraries/LibDiff/Hunks.h b/Userland/Libraries/LibDiff/Hunks.h index dd140feb8e..256458cd97 100644 --- a/Userland/Libraries/LibDiff/Hunks.h +++ b/Userland/Libraries/LibDiff/Hunks.h @@ -32,6 +32,6 @@ struct Hunk { Vector added_lines; }; -Vector parse_hunks(DeprecatedString const& diff); +ErrorOr> parse_hunks(DeprecatedString const& diff); HunkLocation parse_hunk_location(StringView location_line); };