From f424a15c74794c0c9f5834d976db81a45e00d300 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 12 Dec 2021 20:08:45 -0800 Subject: [PATCH 1/2] Add test of cases that are NOT supposed to be numbers The latter two of these currently fail. thread 'test_numbers' panicked at 'expected string. input="+0127", result=Number(PosInt(127))', tests/test_de.rs:352:18 thread 'test_numbers' panicked at 'expected string. input="-0127", result=Number(NegInt(-127))', tests/test_de.rs:352:18 --- tests/test_de.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/test_de.rs b/tests/test_de.rs index 9d7b5bc6..67464b17 100644 --- a/tests/test_de.rs +++ b/tests/test_de.rs @@ -339,7 +339,17 @@ fn test_numbers() { let value = serde_yaml::from_str::(yaml).unwrap(); match value { Value::Number(number) => assert_eq!(number.to_string(), expected), - _ => panic!("expected number"), + _ => panic!("expected number. input={:?}, result={:?}", yaml, value), + } + } + + // NOT numbers. + let cases = ["0127", "+0127", "-0127"]; + for yaml in &cases { + let value = serde_yaml::from_str::(yaml).unwrap(); + match value { + Value::String(string) => assert_eq!(string, *yaml), + _ => panic!("expected string. input={:?}, result={:?}", yaml, value), } } } From 57f2e661b8db22491b404f0e515c362bc6d8a235 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 12 Dec 2021 20:16:53 -0800 Subject: [PATCH 2/2] Treat YAML 1.1 style octals with sign as string, not base 10 number --- src/de.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/de.rs b/src/de.rs index e5f21b06..bab38d13 100644 --- a/src/de.rs +++ b/src/de.rs @@ -987,7 +987,10 @@ where return visitor.visit_i64(n); } } - if v.len() > 1 && v.starts_with('0') && v.bytes().all(|b| b.is_ascii_digit()) { + if { + let v = v.trim_start_matches(&['-', '+'][..]); + v.len() > 1 && v.starts_with('0') && v[1..].bytes().all(|b| b.is_ascii_digit()) + } { // After handling the different number encodings above if we are left // with leading zero(s) followed by numeric characters this is in fact a // string according to the YAML 1.2 spec.