about summary refs log tree commit diff
path: root/tvix/nix-compat/src
diff options
context:
space:
mode:
authoredef <edef@edef.eu>2023-10-27T12·02+0000
committeredef <edef@edef.eu>2023-10-27T13·56+0000
commitb994f692d3c02a858eacfece9e6a7d6990f46539 (patch)
tree60bc8d47749a6845cd481f41f170404f33d2c0d6 /tvix/nix-compat/src
parent6238a05868c8597183023e34cb7f47e9d64270eb (diff)
feat(nix-compat/store_path): validate_name over borrowed data r/6890
Change-Id: Ifeb6231f48d4ad267a7acd398b4b3b687ee4d560
Reviewed-on: https://cl.tvl.fyi/c/depot/+/9857
Reviewed-by: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/nix-compat/src')
-rw-r--r--tvix/nix-compat/src/store_path/mod.rs14
-rw-r--r--tvix/nix-compat/src/store_path/utils.rs2
2 files changed, 10 insertions, 6 deletions
diff --git a/tvix/nix-compat/src/store_path/mod.rs b/tvix/nix-compat/src/store_path/mod.rs
index 93b1d80bbfe3..56bf639f2129 100644
--- a/tvix/nix-compat/src/store_path/mod.rs
+++ b/tvix/nix-compat/src/store_path/mod.rs
@@ -1,6 +1,10 @@
 use crate::nixbase32::{self, Nixbase32DecodeError};
 use data_encoding::BASE64;
-use std::{fmt, path::PathBuf, str::FromStr};
+use std::{
+    fmt,
+    path::PathBuf,
+    str::{self, FromStr},
+};
 use thiserror;
 
 #[cfg(target_family = "unix")]
@@ -111,7 +115,7 @@ impl StorePath {
         }
 
         Ok(StorePath {
-            name: validate_name(&s[ENCODED_DIGEST_SIZE + 1..])?,
+            name: validate_name(&s[ENCODED_DIGEST_SIZE + 1..])?.to_owned(),
             digest: digest.try_into().expect("size is known"),
         })
     }
@@ -129,7 +133,7 @@ impl StorePath {
     /// Construct a [StorePath] from a name and digest.
     pub fn from_name_and_digest(name: String, digest: &[u8]) -> Result<StorePath, Error> {
         Ok(Self {
-            name: validate_name(name.as_bytes())?,
+            name: validate_name(name.as_bytes())?.to_owned(),
             digest: digest.try_into().map_err(|_| Error::InvalidLength())?,
         })
     }
@@ -173,7 +177,7 @@ impl StorePath {
 
 /// Checks a given &[u8] to match the restrictions for [StorePath::name], and
 /// returns the name as string if successful.
-pub(crate) fn validate_name(s: &[u8]) -> Result<String, Error> {
+pub(crate) fn validate_name(s: &[u8]) -> Result<&str, Error> {
     // Empty or excessively long names are not allowed.
     if s.is_empty() || s.len() > 211 {
         return Err(Error::InvalidLength());
@@ -194,7 +198,7 @@ pub(crate) fn validate_name(s: &[u8]) -> Result<String, Error> {
         return Err(Error::InvalidName(s.to_vec(), i));
     }
 
-    Ok(String::from_utf8(s.to_vec()).unwrap())
+    Ok(str::from_utf8(s).unwrap())
 }
 
 impl fmt::Display for StorePath {
diff --git a/tvix/nix-compat/src/store_path/utils.rs b/tvix/nix-compat/src/store_path/utils.rs
index 31964bc6b1ff..94e9f106d9d5 100644
--- a/tvix/nix-compat/src/store_path/utils.rs
+++ b/tvix/nix-compat/src/store_path/utils.rs
@@ -167,7 +167,7 @@ fn build_store_path_from_fingerprint_parts<B: AsRef<[u8]>>(
     name: B,
 ) -> Result<StorePath, Error> {
     let name = name.as_ref();
-    let name = super::validate_name(name.as_ref())?;
+    let name = super::validate_name(name.as_ref())?.to_owned();
 
     let digest = compress_hash(&{
         let mut h = Sha256::new();