{"record":{"id":"f8a75596dce35f30","repo":"rtk-ai/rtk","slug":"negative-string-length","errorCode":null,"errorMessage":"negative string length: {}","messagePattern":"negative string length: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmds/dotnet/binlog.rs","lineNumber":631,"sourceCode":"        let mut shift = 0;\n        loop {\n            let byte = self.read_u8()?;\n            value |= ((byte & 0x7F) as u32) << shift;\n            if (byte & 0x80) == 0 {\n                return Ok(value as i32);\n            }\n\n            shift += 7;\n            if shift >= 35 {\n                anyhow::bail!(\"invalid 7-bit encoded integer\");\n            }\n        }\n    }\n\n    fn read_dotnet_string(&mut self) -> Result<String> {\n        let len = self.read_7bit_i32()?;\n        if len < 0 {\n            anyhow::bail!(\"negative string length: {}\", len);\n        }\n        let bytes = self.read_exact(len as usize)?;\n        String::from_utf8(bytes.to_vec()).context(\"invalid UTF-8 string\")\n    }\n}\n\npub fn scrub_sensitive_env_vars(input: &str) -> String {\n    SENSITIVE_ENV_RE\n        .replace_all(input, \"${prefix}[REDACTED]\")\n        .into_owned()\n}\n\npub fn parse_build_from_text(text: &str) -> BuildSummary {\n    let text = text.replace(\"\\r\\n\", \"\\n\");\n    let clean = strip_ansi(&text);\n    let scrubbed = scrub_sensitive_env_vars(&clean);\n    let mut seen_errors: HashSet<(String, String, u32, u32, String)> = HashSet::new();\n    let mut seen_warnings: HashSet<(String, String, u32, u32, String)> = HashSet::new();","sourceCodeStart":613,"sourceCodeEnd":649,"githubUrl":"https://github.com/rtk-ai/rtk/blob/0924356b4caba4989607227b7c8824d3d8098719/src/cmds/dotnet/binlog.rs#L613-L649","documentation":"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`.","triggerScenarios":"`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.","commonSituations":"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.","solutions":["Handle the -1 (0xFFFFFFFF) null-string sentinel explicitly before rejecting: map it to an empty/None string if the format allows it.","Check cursor alignment — a misaligned read makes random bytes decode as negative lengths; fix the preceding record parse or upgrade reader version support.","Verify the file is a valid binlog and not truncated/corrupt; regenerate with `dotnet build -bl`.","Report the offending length value in upstream bug reports — the message already includes `{}` for the decoded length, useful for diagnosis."],"exampleFix":"// before\nlet len = self.read_7bit_i32()?;\nif len < 0 {\n    anyhow::bail!(\"negative string length: {}\", len);\n}\n\n// after\nlet len = self.read_7bit_i32()?;\nif len == -1 {\n    return Ok(String::new()); // null-string sentinel\n}\nif len < 0 {\n    anyhow::bail!(\"negative string length: {}\", len);\n}","handlingStrategy":"validation","validationCode":"// After decoding, verify length is a plausible size before use:\nfn is_plausible_string_len(len: i32, remaining: usize) -> bool {\n    len >= 0 && (len as usize) <= remaining\n}","typeGuard":null,"tryCatchPattern":"match read_dotnet_string(reader) {\n    Ok(s) => s,\n    Err(e) if e.to_string().starts_with(\"negative string length\") => {\n        eprintln!(\"binlog contains negative/encoded-null string length; check format handling\");\n        abort_parse_with_fallback()\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Decide explicitly how the -1 (0xFFFFFFFF) null-string sentinel should be handled and code that path.","Bound-check every length against the remaining buffer size before allocating.","Keep cursor alignment verified after each record to avoid decoding garbage as lengths.","Include the decoded length value in error logs — it identifies corruption vs. format mismatch immediately."],"tags":["dotnet","binlog","parse-error","corrupt-data"],"backgroundTag":"invalid-argument-value","analyzedSha":"0924356b4caba4989607227b7c8824d3d8098719","analyzedAt":"2026-09-19T19:25:29.612Z","contentChangedAt":"2026-09-19T19:25:29.612Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}