diff --git a/lib/macho/macho_file.rb b/lib/macho/macho_file.rb index 85be72742..629c2bfe6 100644 --- a/lib/macho/macho_file.rb +++ b/lib/macho/macho_file.rb @@ -415,11 +415,10 @@ def add_rpath(path, _options = {}) # @note `_options` is currently unused and is provided for signature # compatibility with {MachO::FatFile#delete_rpath} def delete_rpath(path, _options = {}) - rpath_cmds = command(:LC_RPATH).select { |r| r.path.to_s == path } - raise RpathUnknownError, path if rpath_cmds.empty? + rpath_cmd = command(:LC_RPATH).find { |r| r.path.to_s == path } + raise RpathUnknownError, path unless rpath_cmd - # delete the commands in reverse order, offset descending. - rpath_cmds.reverse_each { |cmd| delete_command(cmd) } + delete_command(rpath_cmd) end # Write all Mach-O data to the given filename. diff --git a/test/test_macho.rb b/test/test_macho.rb index a6ffdd009..aa5bb6cc7 100644 --- a/test/test_macho.rb +++ b/test/test_macho.rb @@ -451,14 +451,19 @@ def test_delete_rpath file = MachO::MachOFile.new(filename) refute_empty file.rpaths - orig_ncmds = file.ncmds + orig_ncmds = current_ncmds = file.ncmds orig_sizeofcmds = file.sizeofcmds - orig_npaths = file.rpaths.size + orig_npaths = current_npaths = file.rpaths.size - file.delete_rpath(file.rpaths.first) - assert_operator file.ncmds, :<, orig_ncmds - assert_operator file.sizeofcmds, :<, orig_sizeofcmds - assert_operator file.rpaths.size, :<, orig_npaths + file.rpaths.each do |rpath| + file.delete_rpath(rpath) + current_npaths -= 1 + current_ncmds -= 1 + + assert_equal file.ncmds, current_ncmds + assert_equal file.rpaths.size, current_npaths + assert_operator file.sizeofcmds, :<, orig_sizeofcmds + end file.write(actual) # ensure we can actually re-load and parse the modified file