Skip to content

Commit 1422076

Browse files
committed
Rearrange the early return on lone or encoded surrogate
1 parent 311f185 commit 1422076

File tree

1 file changed

+23
-26
lines changed

1 file changed

+23
-26
lines changed

src/read.rs

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -868,13 +868,12 @@ fn parse_escape<'de, R: Read<'de>>(
868868

869869
let c = match tri!(read.decode_hex_escape()) {
870870
n @ 0xDC00..=0xDFFF => {
871-
if validate {
872-
return error(read, ErrorCode::LoneLeadingSurrogateInHexEscape);
873-
}
874-
875-
encode_surrogate(scratch, n);
876-
877-
return Ok(());
871+
return if validate {
872+
error(read, ErrorCode::LoneLeadingSurrogateInHexEscape)
873+
} else {
874+
encode_surrogate(scratch, n);
875+
Ok(())
876+
};
878877
}
879878

880879
// Non-BMP characters are encoded as a sequence of two hex
@@ -883,31 +882,29 @@ fn parse_escape<'de, R: Read<'de>>(
883882
// whereas deserializing a byte string accepts lone surrogates.
884883
n1 @ 0xD800..=0xDBFF => {
885884
if tri!(peek_or_eof(read)) != b'\\' {
886-
if validate {
885+
return if validate {
887886
read.discard();
888-
return error(read, ErrorCode::UnexpectedEndOfHexEscape);
889-
}
890-
891-
encode_surrogate(scratch, n1);
892-
893-
return Ok(());
887+
error(read, ErrorCode::UnexpectedEndOfHexEscape)
888+
} else {
889+
encode_surrogate(scratch, n1);
890+
Ok(())
891+
};
894892
}
895893
read.discard();
896894

897895
if tri!(peek_or_eof(read)) != b'u' {
898-
if validate {
896+
return if validate {
899897
read.discard();
900-
return error(read, ErrorCode::UnexpectedEndOfHexEscape);
901-
}
902-
903-
encode_surrogate(scratch, n1);
904-
905-
// The \ prior to this byte started an escape sequence,
906-
// so we need to parse that now. This recursive call
907-
// does not blow the stack on malicious input because
908-
// the escape is not \u, so it will be handled by one
909-
// of the easy nonrecursive cases.
910-
return parse_escape(read, validate, scratch);
898+
error(read, ErrorCode::UnexpectedEndOfHexEscape)
899+
} else {
900+
encode_surrogate(scratch, n1);
901+
// The \ prior to this byte started an escape sequence,
902+
// so we need to parse that now. This recursive call
903+
// does not blow the stack on malicious input because
904+
// the escape is not \u, so it will be handled by one
905+
// of the easy nonrecursive cases.
906+
parse_escape(read, validate, scratch)
907+
};
911908
}
912909
read.discard();
913910

0 commit comments

Comments
 (0)