Skip to content

Keep audit report export working when a violation carries invalid UTF-8 - #128

Merged
rcalicdan merged 1 commit into
typephp-php:mainfrom
naxvog:fix/report-invalid-utf8
Nov 11, 2026
Merged

rcalicdan merged 1 commit into
typephp-php:mainfrom
naxvog:fix/report-invalid-utf8

Conversation

@naxvog

@naxvog naxvog commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Problem

In report mode, one violation can make the whole audit report disappear.

TypeFormatter::formatGivenValue() previews long strings with a byte-based substr($value, 0, 17). If byte 17 falls inside a multibyte character (umlauts, CJK, emoji), the preview is malformed UTF-8. json_encode() then returns false, ViolationCollector::exportReport() returns null, and the shutdown handler ignores that result. No report file is written and nothing is printed. The same happens for worker shards in writeWorkerShard(), and for any binary string value.

Fix

Encode the report and the worker shards with JSON_INVALID_UTF8_SUBSTITUTE. Malformed bytes become U+FFFD and the report is written.

Verification

  • New test in tests/Internal/Reporting/ViolationCollectorTest.php builds the preview through the real TypeFormatter and exports both a report and a shard.
  • Without the fix the test fails with Failed asserting that null is identical to '/tmp/…/typephp-report.json'. With the fix it passes.
  • Full suite on PHP 8.4: 2582 passed. PHPStan: no errors. Pint: no new findings.

Not changed

The preview itself is still cut on a byte boundary, so a thrown TypeError message can still end in a partial character. Fixing that needs mbstring, which the package does not require.

@codecov

codecov Bot commented Nov 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

TypeFormatter previews string values with a byte-based substr(), which can
split a multibyte character. json_encode() then fails on the malformed
UTF-8 and exportReport()/writeWorkerShard() drop the whole report without
any signal.

Encode with JSON_INVALID_UTF8_SUBSTITUTE so the offending bytes become
U+FFFD and the report is still written.
@naxvog
naxvog force-pushed the fix/report-invalid-utf8 branch from 8dd0ba7 to a464d3f Compare November 11, 2026 08:35
@rcalicdan
rcalicdan merged commit d2bf388 into typephp-php:main Nov 11, 2026
15 checks passed
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.

2 participants