rtk-ai/rtk · error

negative string length

Error message

negative string length: {}

What it means

Raised in `read_dotnet_string` after decoding the 7-bit length prefix: the decoded Int32 is negative, which cannot be a valid string byte length. In .NET, a length-encoded string with the high bit of the length prefix set may encode a negative value (or a 'null string' sentinel of 0xFFFFFFFF = -1); the reader rejects it explicitly rather than calling `read_exact` with a huge `usize`.

Solutions

  1. Handle the -1 (0xFFFFFFFF) null-string sentinel explicitly before rejecting: map it to an empty/None string if the format allows it.
  2. Check cursor alignment — a misaligned read makes random bytes decode as negative lengths; fix the preceding record parse or upgrade reader version support.
  3. Verify the file is a valid binlog and not truncated/corrupt; regenerate with `dotnet build -bl`.
  4. Report the offending length value in upstream bug reports — the message already includes `{}` for the decoded length, useful for diagnosis.

Example fix

// before
let len = self.read_7bit_i32()?;
if len < 0 {
    anyhow::bail!("negative string length: {}", len);
}

// after
let len = self.read_7bit_i32()?;
if len == -1 {
    return Ok(String::new()); // null-string sentinel
}
if len < 0 {
    anyhow::bail!("negative string length: {}", len);
}
Defensive patterns

Strategy: validation

Validate before calling

// After decoding, verify length is a plausible size before use:
fn is_plausible_string_len(len: i32, remaining: usize) -> bool {
    len >= 0 && (len as usize) <= remaining
}

Try / catch

match read_dotnet_string(reader) {
    Ok(s) => s,
    Err(e) if e.to_string().starts_with("negative string length") => {
        eprintln!("binlog contains negative/encoded-null string length; check format handling");
        abort_parse_with_fallback()
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: `read_dotnet_string` decodes a length prefix whose 7-bit-encoded value has the sign bit set — e.g. the binlog encodes a null string (-1), or the cursor is misaligned so arbitrary bytes are decoded as the length.

Common situations: Reading a binlog that uses the -1 null-string convention the reader does not handle; cursor misalignment from an earlier misparsed record; parsing non-binlog data; corrupted log files.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of rtk-ai/rtk@0924356b4c (2026-09-19). Data as JSON: /api/errors/f8a75596dce35f30. Report an issue: GitHub.

Appendix: source

Thrown at src/cmds/dotnet/binlog.rs:631

        let mut shift = 0;
        loop {
            let byte = self.read_u8()?;
            value |= ((byte & 0x7F) as u32) << shift;
            if (byte & 0x80) == 0 {
                return Ok(value as i32);
            }

            shift += 7;
            if shift >= 35 {
                anyhow::bail!("invalid 7-bit encoded integer");
            }
        }
    }

    fn read_dotnet_string(&mut self) -> Result<String> {
        let len = self.read_7bit_i32()?;
        if len < 0 {
            anyhow::bail!("negative string length: {}", len);
        }
        let bytes = self.read_exact(len as usize)?;
        String::from_utf8(bytes.to_vec()).context("invalid UTF-8 string")
    }
}

pub fn scrub_sensitive_env_vars(input: &str) -> String {
    SENSITIVE_ENV_RE
        .replace_all(input, "${prefix}[REDACTED]")
        .into_owned()
}

pub fn parse_build_from_text(text: &str) -> BuildSummary {
    let text = text.replace("\r\n", "\n");
    let clean = strip_ansi(&text);
    let scrubbed = scrub_sensitive_env_vars(&clean);
    let mut seen_errors: HashSet<(String, String, u32, u32, String)> = HashSet::new();
    let mut seen_warnings: HashSet<(String, String, u32, u32, String)> = HashSet::new();

View on GitHub (pinned to 0924356b4c)