Browse Source

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.
Shannon Booth 1 year ago
parent
commit
ee643b6417
1 changed files with 2 additions and 3 deletions
  1. 2 3
      Userland/Libraries/LibDiff/Applier.cpp

+ 2 - 3
Userland/Libraries/LibDiff/Applier.cpp

@@ -70,9 +70,8 @@ static Optional<Location> locate_hunk(Vector<StringView> 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())