Skip to content

Commit 4fb675b

Browse files
killerswanerickt
authored andcommitted
Fix JSON parsing of unicode escapes
1 parent 272c5ab commit 4fb675b

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/libstd/json.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -428,17 +428,25 @@ impl Parser {
428428
while i < 4u {
429429
match self.next_char() {
430430
'0' to '9' => {
431-
n = n * 10u +
432-
(self.ch as uint) - ('0' as uint);
433-
}
434-
_ => return self.error(~"invalid \\u escape")
431+
n = n * 16u + (self.ch as uint)
432+
- ('0' as uint);
433+
},
434+
'a' | 'A' => n = n * 16u + 10u,
435+
'b' | 'B' => n = n * 16u + 11u,
436+
'c' | 'C' => n = n * 16u + 12u,
437+
'd' | 'D' => n = n * 16u + 13u,
438+
'e' | 'E' => n = n * 16u + 14u,
439+
'f' | 'F' => n = n * 16u + 15u,
440+
_ => return self.error(
441+
~"invalid \\u escape (unrecognized hex)")
435442
}
436443
i += 1u;
437444
}
438445

439446
// Error out if we didn't parse 4 digits.
440447
if i != 4u {
441-
return self.error(~"invalid \\u escape");
448+
return self.error(
449+
~"invalid \\u escape (not four digits)");
442450
}
443451

444452
str::push_char(res, n as char);

0 commit comments

Comments
 (0)