Skip to content

fix(magic): handle null encoding to prevent decode TypeError#3447

Open
KevinLiebergen wants to merge 1 commit intotrailofbits:masterfrom
KevinLiebergen:fix-decode-typeerror
Open

fix(magic): handle null encoding to prevent decode TypeError#3447
KevinLiebergen wants to merge 1 commit intotrailofbits:masterfrom
KevinLiebergen:fix-decode-typeerror

Conversation

@KevinLiebergen
Copy link
Copy Markdown

This PR fixes a TypeError that occurs when PolyFile processes certain binary files (specifically observed with PNG, JPG, and GIF images).

The Problem: In polyfile/magic.py, the PlainTextTest.test method uses chardet.UniversalDetector to guess the encoding of a file. In some cases, the detector returns a confidence score higher than the minimum threshold but sets the encoding result to None.

When this happens, the following line triggers a crash because decode() expects a string:

value = data[absolute_offset:].decode(encoding)  # encoding is None

The Fix: Added a check to ensure detector.result["encoding"] is not None before attempting to use it for decoding. If it is None, the test correctly falls back to a FailedTest, allowing PolyFile to continue its analysis instead of exiting with a traceback.

Verification: Verified by running polyfile against several image files that previously triggered the crash. The analysis now completes successfully for all files.

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Apr 9, 2026

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants