TVL depot development (mail to depot@tvl.su)
 help / color / mirror / code / Atom feed
From: Linus Heckemann <git@sphalerite•org>
To: depot@tvl.su
Cc: Linus Heckemann <git@sphalerite•org>
Subject: [PATCH] fix(tvix/eval): allow negative substring lengths
Date: Thu,  1 Jun 2023 21:22:59 +0200	[thread overview]
Message-ID: <20230601192258.1159372-1-git@sphalerite.org> (raw)

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]: https://github.com/NixOS/nix/blob/ecae62020b64914d9859a71ce197d03688c6133c/src/libexpr/primops.cc#L3597
[2]: https://github.com/NixOS/nixpkgs/blob/c7c298471676ac1c7789ab3c424fbcebecaa6791/nixos/lib/testing/driver.nix#L29
---
 tvix/eval/src/builtins/mod.rs | 10 +++++-----
 tvix/eval/src/errors.rs       | 15 ---------------
 2 files changed, 5 insertions(+), 20 deletions(-)

diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs
index 53ad6f3f8..ab19ca5ea 100644
--- a/tvix/eval/src/builtins/mod.rs
+++ b/tvix/eval/src/builtins/mod.rs
@@ -869,11 +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 len = if len < 0 {
+            x.as_str().len() as usize
+        } else {
+            len as usize
+        };
         let end = cmp::min(beg + len, x.as_str().len());
 
         Ok(Value::String(x.as_str()[beg..end].into()))
diff --git a/tvix/eval/src/errors.rs b/tvix/eval/src/errors.rs
index 2fbb6496c..76f55d681 100644
--- a/tvix/eval/src/errors.rs
+++ b/tvix/eval/src/errors.rs
@@ -108,11 +108,6 @@ pub enum ErrorKind {
     /// An error occurred when parsing an integer
     ParseIntError(ParseIntError),
 
-    /// A negative integer was used as a value representing length.
-    NegativeLength {
-        length: i64,
-    },
-
     // Errors specific to nested attribute sets and merges thereof.
     /// Nested attributes can not be merged with an inherited value.
     UnmergeableInherit {
@@ -396,14 +391,6 @@ to a missing value in the attribute set(s) included via `with`."#,
                 write!(f, "invalid integer: {}", err)
             }
 
-            ErrorKind::NegativeLength { length } => {
-                write!(
-                    f,
-                    "cannot use a negative integer, {}, for a value representing length",
-                    length
-                )
-            }
-
             ErrorKind::UnmergeableInherit { name } => {
                 write!(
                     f,
@@ -765,7 +752,6 @@ impl Error {
             | ErrorKind::NotCoercibleToString { .. }
             | ErrorKind::NotAnAbsolutePath(_)
             | ErrorKind::ParseIntError(_)
-            | ErrorKind::NegativeLength { .. }
             | ErrorKind::UnmergeableInherit { .. }
             | ErrorKind::UnmergeableValue
             | ErrorKind::ImportParseError { .. }
@@ -808,7 +794,6 @@ impl Error {
             ErrorKind::IndexOutOfBounds { .. } => "E019",
             ErrorKind::NotAnAbsolutePath(_) => "E020",
             ErrorKind::ParseIntError(_) => "E021",
-            ErrorKind::NegativeLength { .. } => "E022",
             ErrorKind::TailEmptyList { .. } => "E023",
             ErrorKind::UnmergeableInherit { .. } => "E024",
             ErrorKind::UnmergeableValue => "E025",
-- 
2.40.1


             reply	other threads:[~2023-06-01 19:23 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-01 19:22 Linus Heckemann [this message]
2023-06-02 13:02 ` sternenseemann
2023-06-02 13:07 ` Vincent Ambo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230601192258.1159372-1-git@sphalerite.org \
    --to=git@sphalerite$(echo .)org \
    --cc=depot@tvl.su \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://code.tvl.fyi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).