Fix infinite loop in fs.md5sum / fs.sha1sum on Python 3#500
Open
potato-20 wants to merge 3 commits into
Open
Conversation
The project shipped without any automated tests. This adds a tests/ package with 34 unit tests covering the host-side pure-Python helpers: - reversec.common.list (chunk, flatten) - reversec.common.text (indent, wrap) - reversec.common.fs (read/write/touch round-trips) - drozer.util (parse_server, StoreZeroOrTwo) - drozer.android.Intent (validity, defaults, flag combination) pyproject.toml gains a [tool.pytest.ini_options] section (pythonpath = src) so the suite runs with a bare 'pytest' invocation, plus a 'test' optional dependency. No production code is changed.
Adds a GitHub Actions workflow that runs the unit test suite across Python 3.9-3.13 on every push and pull request.
Both functions read the file in binary mode but terminated their read loop on 'while line != ""' — comparing bytes to a str literal. In Python 3 b"" != "" is always True, so the loop never exited and the call hung indefinitely (reachable via tools.setup.minimalsu, which calls fs.md5sum). Replace the hand-rolled loop with a single 'with open(...)' read, matching the existing fs.read() helper. Also corrects sha1sum's copy-pasted docstring. Adds regression tests asserting the digests match hashlib for normal and empty files, and that a missing file returns None.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug
reversec.common.fs.md5sum()andsha1sum()open the file in binary mode but drive their read loop with:In Python 3,
f.read()on a binary handle returnsbytes, andb"" != ""is alwaysTrue— so the loop never terminates. Any call on a readable file spins forever.This is reachable in normal use:
drozer/modules/common/superuser.pycallsfs.md5sum()when verifying the bundled minimal-su binary, so that path hangs.Reproduction (hangs before this fix):
The fix
Replace the hand-rolled loop with a single
with open(...)read — the same pattern already used byfs.read()a few lines above:sha1sum()gets the identical treatment, and its copy-pasted docstring (which said "md5sum") is corrected.Tests
Adds regression tests in
tests/test_fs.pyasserting the digests matchhashlibfor normal and empty files, and that a missing file returnsNone. Full suite: 38 passed, no hang.