diff options
| author | Johannes Hofmann <Johannes.Hofmann@gmx.de> | 2026-10-28 09:23:55 +0100 |
|---|---|---|
| committer | Johannes Hofmann <Johannes.Hofmann@gmx.de> | 2026-10-28 10:35:03 +0100 |
| commit | cfe81453ee279fb4d6e2c5e47bdde312025ab021 (patch) | |
| tree | 9bdecf3c6e00a4d60603f362f7df6b68d51c9edc /crates/gipfel-core/src/hill.rs | |
| parent | 8a4fea421c5a1701889b42bd5100d70999709c40 (diff) | |
core: guard atof prefix slicing against char boundaries
The longest-numeric-prefix fallback sliced s[..i] at every byte index,
which panics when i lands inside a multi-byte character (reachable via
lossy-decoded Latin-1 database fields, e.g. a trailing degree sign).
Skip non-boundary indices; behavior on ASCII numeric fields is
unchanged. Includes regression test atof("12.5 deg").
Diffstat (limited to 'crates/gipfel-core/src/hill.rs')
| -rw-r--r-- | crates/gipfel-core/src/hill.rs | 5 |
1 files changed, 5 insertions, 0 deletions
diff --git a/crates/gipfel-core/src/hill.rs b/crates/gipfel-core/src/hill.rs index a904948..b40eb1d 100644 --- a/crates/gipfel-core/src/hill.rs +++ b/crates/gipfel-core/src/hill.rs @@ -70,6 +70,9 @@ fn atof(s: &str) -> f64 { return v; } for i in (1..s.len()).rev() { + if !s.is_char_boundary(i) { + continue; + } if let Ok(v) = s[..i].parse::<f64>() { return v; } @@ -163,6 +166,8 @@ mod tests { assert_eq!(atof("abc"), 0.0); assert_eq!(atof("12abc"), 12.0); assert_eq!(atof(" -3.5x"), -3.5); + // prefix cut must not slice through a multi-byte character + assert_eq!(atof("12.5\u{b0}"), 12.5); } #[test] |
