mirror of
https://github.com/storytold/lightcraft.git
synced 2026-10-30 21:18:11 -06:00
Issue #2097: reject fractional command indices
This commit is contained in:
@@ -1103,11 +1103,8 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
|s, p| {
|
||||
let c = "history.restore";
|
||||
let id = active(s, c)?;
|
||||
let i = f64_req(p, "index", c)?;
|
||||
if !(i.is_finite() && i >= 0.0) {
|
||||
return Err(bad(c, "no such step"));
|
||||
}
|
||||
go_to_history_step(s, c, id, i as usize)
|
||||
let i = index_req(p, "index", c)?;
|
||||
go_to_history_step(s, c, id, i)
|
||||
}
|
||||
),
|
||||
cmd!("history.previous", "Previous History Step", [], None, "{} — steps the History cursor back one step", has_history_before, |s, _| {
|
||||
|
||||
@@ -198,12 +198,18 @@ pub(crate) fn bad(cmd: &str, msg: impl Into<String>) -> EngineError {
|
||||
pub(crate) fn f64_req(p: &Value, key: &str, cmd: &str) -> Result<f64> {
|
||||
p.get(key).and_then(Value::as_f64).ok_or_else(|| bad(cmd, format!("missing number `{key}`")))
|
||||
}
|
||||
/// A non-negative integer parameter: `index: -1` must be an error, not element 0 (`-1.0 as usize`
|
||||
/// saturates to 0), just as `history.restore` already refuses a negative step index.
|
||||
/// A non-negative whole number that fits `usize`. Keep integer JSON exact; accept integral
|
||||
/// floating-point values for compatibility, without truncating or saturating their conversion.
|
||||
pub(crate) fn index_req(p: &Value, key: &str, cmd: &str) -> Result<usize> {
|
||||
let invalid = || bad(cmd, format!("`{key}` must be a non-negative integer"));
|
||||
if let Some(i) = p.get(key).and_then(Value::as_u64) {
|
||||
return usize::try_from(i).map_err(|_| invalid());
|
||||
}
|
||||
let i = f64_req(p, key, cmd)?;
|
||||
if !(i.is_finite() && i >= 0.0) {
|
||||
return Err(bad(cmd, format!("`{key}` must be a non-negative integer")));
|
||||
// The exclusive bound is exactly representable, unlike usize::MAX on 64-bit targets.
|
||||
let upper_bound = 2.0_f64.powi(usize::BITS as i32);
|
||||
if !(i.is_finite() && i >= 0.0 && i.fract() == 0.0 && i < upper_bound) {
|
||||
return Err(invalid());
|
||||
}
|
||||
Ok(i as usize)
|
||||
}
|
||||
@@ -223,3 +229,29 @@ pub(crate) fn point(p: &Value, key: &str) -> Option<lightcraft_geom::Point> {
|
||||
let a = p.get(key)?.as_array()?;
|
||||
Some(lightcraft_geom::Point::new(a.first()?.as_f64()?, a.get(1)?.as_f64()?))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde_json::json;
|
||||
|
||||
use super::index_req;
|
||||
|
||||
#[test]
|
||||
fn command_indices_preserve_integer_precision_and_check_usize_bounds() {
|
||||
for i in [0, 1, usize::MAX] {
|
||||
assert_eq!(index_req(&json!({"index": i}), "index", "test").unwrap(), i);
|
||||
}
|
||||
#[cfg(target_pointer_width = "64")]
|
||||
{
|
||||
let i = 9_007_199_254_740_993usize;
|
||||
assert_eq!(index_req(&json!({"index": i}), "index", "test").unwrap(), i, "integer JSON must not round through f64");
|
||||
}
|
||||
let upper_bound = 2.0_f64.powi(usize::BITS as i32);
|
||||
assert!(index_req(&json!({"index": upper_bound}), "index", "test").is_err(), "2^usize::BITS must not saturate");
|
||||
#[cfg(target_pointer_width = "32")]
|
||||
{
|
||||
assert!(index_req(&json!({"index": u64::from(u32::MAX) + 1}), "index", "test").is_err());
|
||||
assert_eq!(index_req(&json!({"index": f64::from(u32::MAX)}), "index", "test").unwrap(), usize::MAX);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user