Skip to content

Fix wrong bitmask and other utf8 validation bugs - #57

Merged
biljazovic merged 4 commits into
boostorg:developfrom
KodeZ:fix-utf8-noncharacter-validation
Aug 11, 2026
Merged

Fix wrong bitmask and other utf8 validation bugs#57
biljazovic merged 4 commits into
boostorg:developfrom
KodeZ:fix-utf8-noncharacter-validation

Conversation

@KodeZ

@KodeZ KodeZ commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

validate_mqtt_utf8_char only looked at the low byte for 0xFE or 0xFF instead of
checking all the bits it needed to. Correct check is (c & 0xFFFE) == 0xFFFE. This
was rejecting normal characters like þ (U+00FE) and even some emoji, not just the
handful of reserved code points it was supposed to catch.

pop_front_unichar also decoded multi-byte characters without checking the extra
bytes were actually in range 0x80-0xBF, and without checking the result was big
enough to need that many bytes. So bad utf8 like "\xC3z" got decoded instead of
rejected, and a 2-byte encoding of a plain ascii letter got accepted too. Both are
fixed now, along with the 4-byte lead byte mask which was 0x1F instead of 0x07.

Commits go test, fix, test, fix. Each test commit fails on its own and passes once
the fix commit right after it lands.

Morgan Tørvolt added 4 commits August 8, 2026 01:25
…uation bytes

validate_mqtt_utf8_char uses (c & 0xFE) == 0xFE to detect unicode noncharacters. That  rejects any where low byte is 0xFE or 0xFF (e.g. U+00FE, U+00FF, U+1F5FE). It should reject the 34 non-characters. pop_front_unichar also never validates continuation bytes. That means malformed sequences like "\xC3z" are considered valid.

These test currently fail and show both defects.
validate_mqtt_utf8_char only looked at the low byte for 0xFE or 0xFF instead of checking all the bits it needed to. Correct check is (c & 0xFFFE) == 0xFFFE. pop_front_unichar now checks the extra bytes are actually in range 0x80-0xBF before using them. Bad utf8 like "\xC3z" gets rejected now instead of decoded. Also fixed the 4-byte lead byte mask from 0x1F to 0x07 since only 3 bits are used there.

Tests from the last commit pass now.
Two of these already pass since the last fix. The three overlong
ones don't, since nothing checks for that yet.
pop_front_unichar decoded multi-byte sequences without checking the
result was big enough to actually need that many bytes. So
something like a 2-byte encoding of a plain ascii letter got
accepted. Now it checks the decoded value against the minimum for
each length (0x80, 0x800, 0x10000) and rejects it if too small.
@codecov

codecov Bot commented Aug 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.46%. Comparing base (a4ecbdb) to head (82d6e4a).
⚠️ Report is 7 commits behind head on develop.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop      #57      +/-   ##
===========================================
+ Coverage    96.24%   96.46%   +0.21%     
===========================================
  Files           44       44              
  Lines         3250     3280      +30     
===========================================
+ Hits          3128     3164      +36     
+ Misses         122      116       -6     
Files with missing lines Coverage Δ
include/boost/mqtt5/detail/utf8_mqtt.hpp 100.00% <100.00%> (ø)

... and 4 files with indirect coverage changes

Impacted file tree graph

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@biljazovic

Copy link
Copy Markdown
Collaborator

Thanks so much for the PR! Looks good to me.

Unfortunately, 1.92 release is closed for all changes, so this will land in Boost 1.93 in December.

@biljazovic
biljazovic merged commit d84bca6 into boostorg:develop Aug 11, 2026
23 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