fix(tvix/eval): allow negative substring lengths

Nix uses string::substr without checking the sign of the length[1].
The NixOS testing infrastructure relies on this[2], and on the
implicit conversion of that to the maximum possible value for a
size_t.

[1]: ecae62020b/src/libexpr/primops.cc (L3597)
[2]: c7c2984716/nixos/lib/testing/driver.nix (L29)

Change-Id: I6d0caf6830b6bda3fdf44c40c81de6a1befeca7b
Reviewed-on: https://cl.tvl.fyi/c/depot/+/8746
Reviewed-by: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
This commit is contained in:
Linus Heckemann 2023-06-01 21:22:59 +02:00
parent 2b4ad47c16
commit 5733274876
2 changed files with 5 additions and 21 deletions

View file

@ -869,12 +869,11 @@ mod pure_builtins {
return Ok(Value::String("".into()));
}
if len < 0 {
return Err(ErrorKind::NegativeLength { length: len });
}
let len = len as usize;
let end = cmp::min(beg + len, x.as_str().len());
let end = if len < 0 {
x.as_str().len() as usize
} else {
cmp::min(beg + (len as usize), x.as_str().len())
};
Ok(Value::String(x.as_str()[beg..end].into()))
}