From bb54e04c1bf443772414ba1c21d072b1fce83506 Mon Sep 17 00:00:00 2001 From: edef Date: Tue, 10 Oct 2023 10:03:38 +0000 Subject: fix(tvix/nix-compat): clean up the debug assertions a bit Consistent error messages, and slightly nicer code layout. We avoid printing the input data, since we primarily want to point out the specific violated invariant. In the one place where we do want to, we use BStr's Debug implementation, since byte slices don't print nicely. Change-Id: I3a9a0c37be270ea5f16cf124922c254608fb849e Reviewed-on: https://cl.tvl.fyi/c/depot/+/9617 Tested-by: BuildkiteCI Reviewed-by: flokli Autosubmit: edef --- tvix/nix-compat/src/nar/writer/mod.rs | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/tvix/nix-compat/src/nar/writer/mod.rs b/tvix/nix-compat/src/nar/writer/mod.rs index f73d323e38..9abc2510d1 100644 --- a/tvix/nix-compat/src/nar/writer/mod.rs +++ b/tvix/nix-compat/src/nar/writer/mod.rs @@ -28,6 +28,7 @@ //! # Ok::<(), std::io::Error>(()) //! ``` +use bstr::ByteSlice; use std::io::{ self, BufRead, ErrorKind::{InvalidInput, UnexpectedEof}, @@ -73,11 +74,8 @@ impl<'a, 'w> Node<'a, 'w> { "target.len() > {}", wire::MAX_TARGET_LEN ); - debug_assert!( - !target.contains(&b'\0'), - "invalid target characters: {target:?}" - ); - debug_assert!(!target.is_empty(), "empty target"); + debug_assert!(!target.is_empty(), "target is empty"); + debug_assert!(!target.contains(&0), "target contains null byte"); self.write(&wire::TOK_SYM)?; self.write(&target.len().to_le_bytes())?; @@ -176,12 +174,11 @@ impl<'a, 'w> Directory<'a, 'w> { "name.len() > {}", wire::MAX_NAME_LEN ); - debug_assert!(name != b"", "name may not be empty"); - debug_assert!(name != b".", "invalid name: {name:?}"); - debug_assert!(name != b"..", "invalid name: {name:?}"); - - debug_assert!(!name.contains(&b'/'), "invalid name characters: {name:?}"); - debug_assert!(!name.contains(&b'\0'), "invalid name characters: {name:?}"); + debug_assert!(!name.is_empty(), "name is empty"); + debug_assert!(!name.contains(&0), "name contains null byte"); + debug_assert!(!name.contains(&b'/'), "name contains {:?}", '/'); + debug_assert!(name != b".", "name == {:?}", "."); + debug_assert!(name != b"..", "name == {:?}", ".."); match self.prev_name { None => { @@ -192,7 +189,9 @@ impl<'a, 'w> Directory<'a, 'w> { { assert!( &**_prev_name < name, - "misordered names: {_prev_name:?} >= {name:?}" + "misordered names: {:?} >= {:?}", + _prev_name.as_bstr(), + name.as_bstr() ); _prev_name.clear(); _prev_name.extend_from_slice(name); -- cgit 1.4.1