This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-12329-20aa5cf80b126a1a4530efbf7398d85d00613e12 in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
commit ab1751ead80b24cb2b919d7eaaaf4b739c7fd355 Author: Kasra Ghaffari <[email protected]> AuthorDate: Fri Nov 20 10:10:10 2026 +0000 fix(arrow-json): decode surrogate pairs properly according to rfc2781 (#12329) # Which issue does this PR close? - Closes #12320 # Rationale for this change This fixes a data corruption bug when decoding surrogate pairs in json strings/keys when you rewrite the existing code to not be a one liner. it becomes this: ```rs let high_bits = (high - 0xD800) as u32; // top 10 bits let low_bits = (low - 0xDC00) as u32; // bottom 10 bits let n = 0x1_0000 | (high_bits << 10) | low_bits;; ``` however [the RFC](https://www.rfc-editor.org/info/rfc2781/#section-2.2) mentions (step 5) that we need to add 0x10000, not OR it. ```rs let high_bits = (high - 0xD800) as u32; // top 10 bits let low_bits = (low - 0xDC00) as u32; // bottom 10 bits let n = 0x1_0000 + (high_bits << 10) | low_bits;; ``` the pr just makes the decoding compliant with the rfc # What changes are included in this PR? fixing the surrogate pair decoding calculation and adding some tests to check a few surrogate pairs. its not that extensive but it covers a few boundary values I think are worth. the test fails without the fix but succeeds with it. # Are these changes tested? yes. i added the test as mentioned above and then ran cargo test # Are there any significant user-facing changes? just a fix for data corruption of these unicode characters. I guess its possible someone might have depended on this incorrect behavior but I think it makes more sense to fix the issue. # AI usage disclosure I used ai to help find the rfc text and write a few surrogate pairs for the test. --- arrow-json/src/reader/tape.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/arrow-json/src/reader/tape.rs b/arrow-json/src/reader/tape.rs index e0ccec6f1d..0b3017c808 100644 --- a/arrow-json/src/reader/tape.rs +++ b/arrow-json/src/reader/tape.rs @@ -813,7 +813,7 @@ fn err(b: u8, ctx: &str) -> ArrowError { fn char_from_surrogate_pair(low: u16, high: u16) -> Result<char, ArrowError> { match (low, high) { (0xDC00..=0xDFFF, 0xD800..=0xDBFF) => { - let n = (((high - 0xD800) as u32) << 10) | ((low - 0xDC00) as u32 + 0x1_0000); + let n = (((high - 0xD800) as u32) << 10) + ((low - 0xDC00) as u32 + 0x1_0000); char::from_u32(n) .ok_or_else(|| ArrowError::JsonError(format!("Invalid UTF-16 surrogate pair {n}"))) } @@ -987,6 +987,32 @@ mod tests { assert_eq!(decoder.num_buffered_rows(), 0); } + #[test] + fn test_surrogate_pairs() { + let mut decoder = tape_decoder(); + decoder + .decode( + br#"{"v": [ + "\ud800\udc00", + "\ud83d\ude00", + "\ud840\udc00", + "\udbff\udfff", + "\uD840\uDC00", + "a\ud840\udc00b" + ]}"#, + ) + .unwrap(); + let tape = decoder.finish().unwrap(); + + // String 0 is the key "v" + assert_eq!(tape.get_string(1), "\u{10000}"); // lowest supplementary + assert_eq!(tape.get_string(2), "\u{1F600}"); // emoji + assert_eq!(tape.get_string(3), "\u{20000}"); + assert_eq!(tape.get_string(4), "\u{10FFFF}"); + assert_eq!(tape.get_string(5), "\u{20000}"); // uppercase hex + assert_eq!(tape.get_string(6), "a\u{20000}b"); // surrounded by ASCII + } + #[test] fn test_invalid() { // Test invalid
