about summary refs log tree commit diff
path: root/tvix/eval/src/value/string.rs
diff options
context:
space:
mode:
authorAspen Smith <root@gws.fyi>2023-12-05T22·11-0500
committerclbot <clbot@tvl.fyi>2023-12-05T23·12+0000
commit8135a8d38cefdc4632b3c85fdbb663d067f91248 (patch)
tree12506241f23e7071a49f4d03ea73b32d16d2afd3 /tvix/eval/src/value/string.rs
parent41235bf9086c6b048549c7564679d748c35e0de3 (diff)
fix(tvix/eval): Return error rather than panicking on bad substring r/7119
If builtins.substring is invoked with (byte!!) offsets that aren't at
codepoint boundaries, return an error rather than panicking. This is
still incorrect (see b/337) but pushes the incorrectness forward a step.

Change-Id: I5a4261f2ff250874cd36489ef598dcf886669d04
Reviewed-on: https://cl.tvl.fyi/c/depot/+/10199
Tested-by: BuildkiteCI
Autosubmit: grfn <grfn@gws.fyi>
Reviewed-by: sterni <sternenseemann@systemli.org>
Diffstat (limited to 'tvix/eval/src/value/string.rs')
-rw-r--r--tvix/eval/src/value/string.rs9
1 files changed, 9 insertions, 0 deletions
diff --git a/tvix/eval/src/value/string.rs b/tvix/eval/src/value/string.rs
index 8a69e060c6..314b21d668 100644
--- a/tvix/eval/src/value/string.rs
+++ b/tvix/eval/src/value/string.rs
@@ -8,6 +8,7 @@ use std::ffi::OsStr;
 use std::hash::Hash;
 use std::ops::Deref;
 use std::path::Path;
+use std::str::{self, Utf8Error};
 use std::{borrow::Cow, fmt::Display, str::Chars};
 
 use serde::de::{Deserializer, Visitor};
@@ -37,6 +38,14 @@ impl Ord for NixString {
     }
 }
 
+impl TryFrom<&[u8]> for NixString {
+    type Error = Utf8Error;
+
+    fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
+        Ok(Self(Box::from(str::from_utf8(value)?)))
+    }
+}
+
 impl From<&str> for NixString {
     fn from(s: &str) -> Self {
         NixString(Box::from(s))