Merge pull request #7166 from MrMha/fix/unwrap-preserve-input-20261017

fix: preserve SELF inputs when an output aliases them
This commit is contained in:
Dean authored and GitHub committed 2026-10-18 09:47:48 +13:00
commit 802db07168
2 files changed
+21 -1

No files matched your search

+17
View File
@@ -82,6 +82,23 @@ def read_program_headers(path):
class UnwrapTest(unittest.TestCase):
def test_output_aliases_cannot_replace_the_input_container(self):
container = build_container(default_segments(), default_records(), default_payloads())
source = Path(self.directory) / "input.self"
source.write_bytes(container)
alias = Path(self.directory) / "alias.elf"
import os
os.link(source, alias)
for output in (source, source.parent / "." / source.name, alias):
with self.subTest(output=output):
result = subprocess.run([sys.executable, str(Path(unwrap_self.__file__)), str(source), str(output)],
capture_output=True, text=True, timeout=30)
self.assertEqual(result.returncode, 2, result.stderr)
self.assertIn("output aliases the input", result.stderr)
self.assertNotIn("Traceback", result.stderr)
self.assertEqual(source.read_bytes(), container)
self.assertEqual(alias.read_bytes(), container)
def convert(self, container, name="input.self"):
directory = Path(self.directory)
source = directory / name
+4 -1
View File
@@ -191,7 +191,10 @@ def check_decrypted(data, headers):
def unwrap(path, output, verbose=False):
data = bytes(Path(path).read_bytes())
source, destination = Path(path), Path(output)
if source.resolve() == destination.resolve() or (destination.exists() and source.samefile(destination)):
raise Refused("output aliases the input container")
data = bytes(source.read_bytes())
if len(data) < FIRST_REGION:
raise Refused("file is smaller than the SELF header area")
magic = struct.unpack_from("<I", data, 0)[0]