Skip to content

Commit

Permalink
Return error if numeric key starts with whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
overdrivenpotato committed Jul 10, 2023
1 parent 468a94a commit a4e2719
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/de.rs
Expand Up @@ -2125,7 +2125,12 @@ macro_rules! deserialize_numeric_key {
V: de::Visitor<'de>,
{
self.de.eat_char();
let value = self.de.$method(visitor)?;

if let Some(b' ') | Some(b'\n') | Some(b'\r') | Some(b'\t') = tri!(self.de.peek()) {
return Err(self.de.peek_error(ErrorCode::UnexpectedWhitespaceInKey));
}

let value = tri!(self.de.$method(visitor));

match self.de.peek()? {
Some(b'"') => self.de.eat_char(),
Expand Down
7 changes: 7 additions & 0 deletions src/error.rs
Expand Up @@ -72,6 +72,7 @@ impl Error {
| ErrorCode::ControlCharacterWhileParsingString
| ErrorCode::KeyMustBeAString
| ErrorCode::FloatKeyMustBeFinite
| ErrorCode::UnexpectedWhitespaceInKey
| ErrorCode::LoneLeadingSurrogateInHexEscape
| ErrorCode::TrailingComma
| ErrorCode::TrailingCharacters
Expand Down Expand Up @@ -290,6 +291,9 @@ pub(crate) enum ErrorCode {
/// Object key is a non-finite float value.
FloatKeyMustBeFinite,

/// Unexpected whitespace in a numeric key.
UnexpectedWhitespaceInKey,

/// Lone leading surrogate in hex escape.
LoneLeadingSurrogateInHexEscape,

Expand Down Expand Up @@ -368,6 +372,9 @@ impl Display for ErrorCode {
ErrorCode::FloatKeyMustBeFinite => {
f.write_str("float key must be finite (got NaN or +/-inf)")
}
ErrorCode::UnexpectedWhitespaceInKey => {
f.write_str("unexpected whitespace in object key")
}
ErrorCode::LoneLeadingSurrogateInHexEscape => {
f.write_str("lone leading surrogate in hex escape")
}
Expand Down
9 changes: 9 additions & 0 deletions tests/test.rs
Expand Up @@ -1900,6 +1900,15 @@ fn test_integer_key() {
test_parse_err::<BTreeMap<i32, ()>>(&[(j, "expected value at line 1 column 3")]);
}

#[test]
fn test_integer_key_leading_whitespace() {
let j = r#"{" 123":null}"#;
test_parse_err::<BTreeMap<i32, ()>>(&[(
j,
"unexpected whitespace in object key at line 1 column 3",
)]);
}

#[test]
fn test_integer128_key() {
let map = treemap! {
Expand Down

0 comments on commit a4e2719

Please sign in to comment.