From ee643b64172bc0d841c6a478893afc2d37911a05 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Sat, 2 Mar 2024 23:14:15 +1300 Subject: [PATCH] LibDiff: Prevent negative underflow calculating suffix and prefix fuzz In the situation where the amount of content preceeding the hunk was greater than the max context of the hunk there would be an unsigned underflow, as the logic was assuming signed arithmitic. This underflow would result in the patch not applying, as patch would assume the massive calculated fuzz would result in the patch matching against any file. --- Userland/Libraries/LibDiff/Applier.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibDiff/Applier.cpp b/Userland/Libraries/LibDiff/Applier.cpp index 05a3a68676..8b8fd49608 100644 --- a/Userland/Libraries/LibDiff/Applier.cpp +++ b/Userland/Libraries/LibDiff/Applier.cpp @@ -70,9 +70,8 @@ static Optional locate_hunk(Vector const& content, Hunk co // match the hunk by ignoring an increasing amount of context lines. The number of context lines that are ignored is // called the 'fuzz'. for (size_t fuzz = 0; fuzz <= max_fuzz; ++fuzz) { - - auto suffix_fuzz = max(fuzz + patch_suffix_context - context, 0); - auto prefix_fuzz = max(fuzz + patch_prefix_context - context, 0); + auto suffix_fuzz = (patch_suffix_context >= context) ? (fuzz + patch_suffix_context - context) : 0; + auto prefix_fuzz = (patch_prefix_context >= context) ? (fuzz + patch_prefix_context - context) : 0; // If the fuzz is greater than the total number of lines for a hunk, then it may be possible for the hunk to match anything. if (suffix_fuzz + prefix_fuzz >= hunk.lines.size())