From bdb7345a28f9b8d3edba46ac0dc5cfbd57cb92e8 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Mon, 26 Aug 2024 12:05:01 -0300 Subject: fix: patch: check array indices correctly --- spec/unit/tools_spec.lua | 5 ++--- src/luarocks/tools/patch.lua | 9 ++++----- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/spec/unit/tools_spec.lua b/spec/unit/tools_spec.lua index 5b85c86a..65d8b73d 100644 --- a/spec/unit/tools_spec.lua +++ b/spec/unit/tools_spec.lua @@ -223,9 +223,8 @@ describe("Luarocks patch test #unit", function() it("fails if the patch file is invalid", function() write_file("test.patch", invalid_patch1, finally) - local p = patch.read_patch("test.patch") - local result = pcall(patch.apply_patch, p) - assert.falsy(result) + local p, all_ok = patch.read_patch("test.patch") + assert.falsy(all_ok) end) it("returns false if the files from the patch doesn't exist", function() diff --git a/src/luarocks/tools/patch.lua b/src/luarocks/tools/patch.lua index 6f36d713..10654e06 100644 --- a/src/luarocks/tools/patch.lua +++ b/src/luarocks/tools/patch.lua @@ -11,7 +11,6 @@ local patch = {} local fs = require("luarocks.fs") -local fun = require("luarocks.fun") local io = io local os = os @@ -255,7 +254,7 @@ function patch.read_patch(filename, data) local advance if state == 'filenames' then if startswith(line, "--- ") then - if fun.contains(files.source, nextfileno) then + if files.source[nextfileno] then all_ok = false warning(format("skipping invalid patch for %s", files.source[nextfileno+1])) @@ -278,7 +277,7 @@ function patch.read_patch(filename, data) table.insert(files.source, match) end elseif not startswith(line, "+++ ") then - if fun.contains(files.source, nextfileno) then + if files.source[nextfileno] then all_ok = false warning(format("skipping invalid patch with no target for %s", files.source[nextfileno+1])) @@ -289,7 +288,7 @@ function patch.read_patch(filename, data) end state = 'header' else - if fun.contains(files.target, nextfileno) then + if files.target[nextfileno] then all_ok = false warning(format("skipping invalid patch - double target at line %d", lineno+1)) @@ -331,7 +330,7 @@ function patch.read_patch(filename, data) if not advance and state == 'hunkhead' then local m1, m2, m3, m4 = match_linerange(line) if not m1 then - if not fun.contains(files.hunks, nextfileno-1) then + if not files.hunks[nextfileno-1] then all_ok = false warning(format("skipping invalid patch with no hunks for file %s", files.target[nextfileno])) -- cgit v1.2.3-55-g6feb