Skip to content

Commit a1621ac

Browse files
committed
scripts: west_command: patch.py: fix sha256 bug on windows
Fix SHA256 verification failures on Windows by ensuring consistent line ending handling in patch files. Replace inline hash calculation with existing get_file_sha256sum() function and modify it to read files as text with normalized line endings before encoding to UTF-8 for hashing. This ensures cross-platform compatibility and prevents patch integrity check failures due to CRLF/LF differences between operating systems. Signed-off-by: Benjamin Cabé <[email protected]>
1 parent 2f864d5 commit a1621ac

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

scripts/west_commands/patch.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -365,21 +365,14 @@ def apply(self, args, yml, dst_mods=None):
365365
apply_cmd_list = shlex.split(apply_cmd)
366366

367367
self.dbg(f"reading patch file {pth}")
368-
patch_file_data = None
369-
368+
expect_sha256 = patch_info["sha256sum"]
370369
try:
371-
with open(patch_path, "rb") as pf:
372-
patch_file_data = pf.read()
370+
actual_sha256 = self.get_file_sha256sum(patch_path)
373371
except Exception as e:
374372
self.err(f"failed to read {pth}: {e}")
375373
failed_patch = pth
376374
break
377375

378-
self.dbg("checking patch integrity... ", end="")
379-
expect_sha256 = patch_info["sha256sum"]
380-
hasher = hashlib.sha256()
381-
hasher.update(patch_file_data)
382-
actual_sha256 = hasher.hexdigest()
383376
if actual_sha256 != expect_sha256:
384377
self.dbg("FAIL")
385378
self.err(
@@ -391,7 +384,6 @@ def apply(self, args, yml, dst_mods=None):
391384
break
392385
self.dbg("OK")
393386
patch_count += 1
394-
patch_file_data = None
395387

396388
mod_path = Path(args.west_workspace) / mod
397389
patched_mods.add(mod)
@@ -529,12 +521,14 @@ def gh_fetch(self, args, yml, mods=None):
529521

530522
@staticmethod
531523
def get_file_sha256sum(filename: Path) -> str:
532-
with open(filename, "rb") as fp:
533-
# NOTE: If python 3.11 is the minimum, the following can be replaced with:
534-
# digest = hashlib.file_digest(fp, "sha256")
535-
digest = hashlib.new("sha256")
536-
while chunk := fp.read(2**10):
537-
digest.update(chunk)
524+
# Read as text to normalize line endings
525+
with open(filename, encoding="utf-8", newline=None) as fp:
526+
content = fp.read()
527+
528+
# NOTE: If python 3.11 is the minimum, the following can be replaced with:
529+
# digest = hashlib.file_digest(BytesIO(content_bytes), "sha256")
530+
digest = hashlib.new("sha256")
531+
digest.update(content.encode("utf-8"))
538532

539533
return digest.hexdigest()
540534

0 commit comments

Comments
 (0)