Repository navigation
Description
Hi,
the investigation of haskell/attoparsec#232 found that the iter function on Text [1] can lead to out of bounds memory access if the last byte in the text is a continuation byte and the index points to this last byte. This can happen if the last character in the text is a multibyte sequence.
This is the result of a change in 2.1.3, namely #635 which changed the branchless implementation of utf8LengthByLeader [3] to the current branched version.
Continuation bytes in the range 0x80 - 0xBF thus get a length of 2 bytes - this cannot be guaranteed and indeed does fail as shown above.
This happens with valid UTF-8 text. It is somewhat mitigated by the fact that the iterator is normally only applied to known valid leading bytes (usually retrieved from the output of iter itself) and will almost certainly return garbage anyway if not. However, the OOB issue might still be undesired.
A simple fix would be to adapt the branched implementation to also include the range 0x80 - 0xBF in the conditional returning 1.
The relevant code snippets are appended below.
Thanks for your time,
Vekhir
Appendix
[1]
Lines 71 to 80 in c0b11ac
| -- | /O(1)/ Iterate (unsafely) one step forwards through a UTF-8 | |
| -- array, returning the current character and the delta to add to give | |
| -- the next offset to iterate at. | |
| iter :: | |
| #if defined(ASSERTS) | |
| HasCallStack => | |
| #endif | |
| Text -> Int -> Iter | |
| iter (Text arr off _len) i = iterArray arr (off + i) | |
| {-# INLINE iter #-} |
[2]
Lines 88 to 98 in c0b11ac
| iterArray arr j = Iter chr l | |
| where m0 = A.unsafeIndex arr j | |
| m1 = A.unsafeIndex arr (j+1) | |
| m2 = A.unsafeIndex arr (j+2) | |
| m3 = A.unsafeIndex arr (j+3) | |
| l = utf8LengthByLeader m0 | |
| chr = case l of | |
| 1 -> unsafeChr8 m0 | |
| 2 -> chr2 m0 m1 | |
| 3 -> chr3 m0 m1 m2 | |
| _ -> chr4 m0 m1 m2 m3 |
[3]
text/src/Data/Text/Internal/Encoding/Utf8.hs
Lines 85 to 94 in c0b11ac
| -- | Measure byte length of UTF-8 encoding for characters, | |
| -- starting with a given byte. | |
| -- | |
| -- @since 2.0 | |
| utf8LengthByLeader :: Word8 -> Int | |
| utf8LengthByLeader w | |
| | w < 0x80 = 1 | |
| | w < 0xE0 = 2 | |
| | w < 0xF0 = 3 | |
| | otherwise = 4 |
Activity
Return length of 1 for continuation bytes
Bodigrim commentedon Jul 12, 2026
Well,
iteris fromData.Text.Unsafeand explicitly says that it iterates "unsafely". The real problem is thatattoparsectests calliterin the middle of a multibyte sequence, and that's what should be fixed.(I don't mind merging #705 as well, the change of behavior in #635 was not planned, but it would not make
iteranyhow more correct or safe)Bodigrim commentedon Jul 12, 2026
Also, it would be convenient if someone implements #406, which would allow
attoparsecto ask whether a givenTextis actually valid or not.Vekhir commentedon Jul 12, 2026
True. I think the previous behaviour was a bit more logical in a sense. And if we can choose the value anyhow, why not make it nice 😜
Return length of 1 for continuation bytes
Return length of 1 for continuation bytes
Return length of 1 for continuation bytes