Skip to content
This repository was archived by the owner on Mar 25, 2024. It is now read-only.

Commit 24dc184

Browse files
committed
Add a check for strings disguised as leading zeros followed by numbers
Resolves Issue #179 According to the YAML 1.2 spec (https://yaml.org/spec/1.2/spec.html#id2761292) numbers sequences with a leading zeros (that are not special encoding identifiers such as 0x, 0b, etc) are actually strings. This patch handles this case, all existing tests still pass.
1 parent f208925 commit 24dc184

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

‎src/de.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,13 @@ where
556556
return visitor.visit_i64(n);
557557
}
558558
}
559+
// After handling the different number encodings above
560+
// if we are left with leading zero(s) followed by numeric characters
561+
// this is in fact a string according to the YAML 1.2 spec
562+
// https://yaml.org/spec/1.2/spec.html#id2761292
563+
if v.len() > 1 && v.starts_with("0") && v.chars().all(char::is_numeric) {
564+
return visitor.visit_str(v);
565+
}
559566
if let Ok(n) = v.parse() {
560567
return visitor.visit_u64(n);
561568
}

‎tests/test_value.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ fn test_nan() {
1010
let neg_fake_nan = serde_yaml::from_str::<Value>("-.nan").unwrap();
1111
assert!(neg_fake_nan.is_string());
1212

13+
let num_string = serde_yaml::from_str::<Value>("01").unwrap();
14+
assert!(num_string.is_string());
15+
1316
let significand_mask = 0xF_FFFF_FFFF_FFFF;
1417
let bits = (f64::NAN.to_bits() ^ significand_mask) | 1;
1518
let different_pos_nan = Value::Number(Number::from(f64::from_bits(bits)));

0 commit comments

Comments
 (0)