Fix unsafe ByteString bounds on wire protocol data#76
Merged
jappeace merged 3 commits intowinterland1989:masterfrom Mar 31, 2026
Merged
Fix unsafe ByteString bounds on wire protocol data#76jappeace merged 3 commits intowinterland1989:masterfrom
jappeace merged 3 commits intowinterland1989:masterfrom
Conversation
Replace unsafeDrop, unsafeTail, and unsafeIndex with safe bounded alternatives in text protocol parsers (MySQLValue.hs), add a precision >= scale guard for NEWDECIMAL in BinLogValue.hs, and add bounds checking to eventHeaderLen in BinLogEvent.hs. The unsafe operations caused undefined behavior (reading garbage memory, segfaults) when receiving malformed data from the MySQL wire protocol. The safe alternatives are also O(1) — the only cost is a single integer comparison per call. Changes: - MySQLValue.hs: Add guards before B.tail/B.drop in dateParser, timeParser, timestamp/datetime, and time field parsers - BinLogValue.hs: Guard precision >= scale in NEWDECIMAL to prevent Word8 underflow causing out-of-bounds sizeTable access - BinLogEvent.hs: Bounds check in eventHeaderLen to handle unknown event types (negative index) gracefully by returning 0 - Add test/BoundsCheck.hs with 8 regression tests covering all fixed code paths Prompt: Implement plan to fix unsafe bounds in mysql-haskell (TDD) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The old timestamp/datetime tests used "abc" and "" as inputs. readDecimal returns Nothing on non-numeric input, which short-circuits via Maybe's Applicative instance before unsafeDrop is ever forced (lazy evaluation). These tests passed even without the fix. Replace with inputs where dateParser succeeds but the string is too short for the subsequent unsafeDrop 11: - "2024-01-01" (10 bytes) — dateParser succeeds, unsafeDrop 11 is UB - "2024-01-01 1" (12 bytes) — dateParser succeeds, unsafeDrop 11 gives "1", timeParser reads hour, then unsafeTail on "" is UB Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.
Summary
unsafeDrop,unsafeTail, andunsafeIndexwith safe bounded alternatives in text protocol parsers (MySQLValue.hs) — addsguardchecks beforeB.tail/B.dropindateParser,timeParser, and timestamp/time field decodersprecision >= scaleguard forNEWDECIMALinBinLogValue.hsto preventWord8underflow causing out-of-boundssizeTableaccesseventHeaderLeninBinLogEvent.hsto handle unknown event types (negative index) gracefully by returning 0Motivation
The unsafe
ByteStringoperations cause undefined behavior (reading garbage memory, segfaults, silent wrong results) when receiving malformed data from the MySQL wire protocol. The safe alternatives are also O(1) — the only overhead is a single integer comparison per call. This is negligible compared to the cost of a network round-trip.Test plan
test/BoundsCheck.hscovering all fixed code paths🤖 Generated with Claude Code