Skip to content

Commit

Permalink
Fix deletion of duplicate rpaths (#357)
Browse files Browse the repository at this point in the history
* Fix deletion of duplicate rpaths

Deferring field population to the end seems to break rpath deletion of
duplicated rpaths.

Closes #356.

* Add test for deleting duplicate rpaths
  • Loading branch information
carlocab committed May 15, 2021
1 parent d50738f commit 26a92d3
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
7 changes: 2 additions & 5 deletions lib/macho/macho_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -418,11 +418,8 @@ def delete_rpath(path, _options = {})
rpath_cmds = command(:LC_RPATH).select { |r| r.path.to_s == path }
raise RpathUnknownError, path if rpath_cmds.empty?

# delete the commands in reverse order, offset descending. this
# allows us to defer (expensive) field population until the very end
rpath_cmds.reverse_each { |cmd| delete_command(cmd, :repopulate => false) }

populate_fields
# delete the commands in reverse order, offset descending.
rpath_cmds.reverse_each { |cmd| delete_command(cmd) }
end

# Write all Mach-O data to the given filename.
Expand Down
Binary file added test/bin/x86_64/libdupe.dylib
Binary file not shown.
5 changes: 5 additions & 0 deletions test/test_macho.rb
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,10 @@ def test_delete_rpath
end
end

groups << ["libdupe.dylib", "libdupe_actual.dylib"].map do |fn|
fixture(:x86_64, fn)
end

groups.each do |filename, actual|
file = MachO::MachOFile.new(filename)

Expand All @@ -460,6 +464,7 @@ def test_delete_rpath
# ensure we can actually re-load and parse the modified file
modified = MachO::MachOFile.new(actual)

assert_empty modified.rpaths
assert_equal file.serialize.bytesize, modified.serialize.bytesize
assert_operator modified.ncmds, :<, orig_ncmds
assert_operator modified.sizeofcmds, :<, orig_sizeofcmds
Expand Down

0 comments on commit 26a92d3

Please sign in to comment.