{"record":{"id":"912b97f7c69ca615","repo":"BoundaryML/baml","slug":"invalid-arg","errorCode":null,"errorMessage":"invalid arg","messagePattern":"invalid arg","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"languages/rust/baml/src/lib.rs","lineNumber":174,"sourceCode":"            cffi_value_holder::Value::EnumValue(_) => \"enum\",\n            cffi_value_holder::Value::LiteralValue(_) => \"literal\",\n            cffi_value_holder::Value::ObjectValue(_) => \"object\",\n            cffi_value_holder::Value::ListValue(_) => \"list\",\n            cffi_value_holder::Value::MapValue(_) => \"map\",\n            cffi_value_holder::Value::UnionVariantValue(_) => \"union\",\n            cffi_value_holder::Value::CheckedValue(_) => \"checked\",\n            cffi_value_holder::Value::StreamingStateValue(_) => \"streaming_state\",\n        }\n    }\n}\n\n/// Call baml-cli with the given arguments\n/// Returns the exit code\npub fn invoke_cli(args: &[&str]) -> i32 {\n    // Convert args to C strings\n    let c_args: Vec<CString> = args\n        .iter()\n        .map(|s| CString::new(*s).expect(\"invalid arg\"))\n        .collect();\n\n    // Create array of pointers\n    let c_arg_ptrs: Vec<*const libc::c_char> = c_args\n        .iter()\n        .map(|s| s.as_ptr())\n        .chain(std::iter::once(std::ptr::null())) // null terminator\n        .collect();\n\n    #[allow(unsafe_code, clippy::print_stderr)]\n    unsafe {\n        match ffi::invoke_runtime_cli(c_arg_ptrs.as_ptr()) {\n            Ok(code) => code,\n            Err(e) => {\n                // CLI errors should be printed to stderr\n                eprintln!(\"Failed to load BAML library: {e}\");\n                1\n            }","sourceCodeStart":156,"sourceCodeEnd":192,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/languages/rust/baml/src/lib.rs#L156-L192","documentation":"Panic raised inside `invoke_cli` in the baml Rust FFI layer when converting a CLI argument to a `CString`. `CString::new` fails if the argument contains an interior NUL byte (`\\0`), which cannot be represented in a C string. The `expect` turns that conversion failure into an immediate panic with message \"invalid arg\".","triggerScenarios":"Calling `invoke_cli(&[...])` with an argument string containing a NUL byte (e.g. `\"baml\\0--help\"`). Normal argument lists (version, help, subcommands) never trigger it.","commonSituations":"Programmatically building argv arrays where arguments are concatenated or sourced from buffers that accidentally include NUL terminators; passing raw binary data instead of text.","solutions":["Inspect the args slice passed to invoke_cli and remove any embedded NUL bytes","Sanitize/validate arguments before calling invoke_cli (reject strings containing '\\0')","Use CString::new(...).map_err(...) instead of expect if you need a graceful error"],"exampleFix":"// before\nlet c_args: Vec<CString> = args.iter().map(|s| CString::new(*s).expect(\"invalid arg\")).collect();\n// after\nlet c_args: Vec<CString> = args.iter().map(|s| CString::new(*s).expect(\"invalid arg\")).collect();\nlet c_args: Vec<CString> = args.iter().map(|s| CString::new(*s).unwrap_or_else(|e| panic!(\"invalid arg {:?}: {}\", s, e))).collect();","handlingStrategy":"validation","validationCode":"fn valid_args(args: &[&str]) -> bool { args.iter().all(|a| !a.contains('\\0')) }\nif !valid_args(&args) { return -1; }\nlet code = invoke_cli(&args);","typeGuard":"fn is_nul_free(s: &str) -> bool { !s.as_bytes().contains(&b'\\0') }","tryCatchPattern":"// panic-based; use catch_unwind if you must\nlet code = std::panic::catch_unwind(|| invoke_cli(&args)).unwrap_or(-1);","preventionTips":["Never embed NUL bytes in CLI arguments","Validate string arguments before passing to FFI-style APIs","Replace expect() with proper error handling in your own fork"],"tags":["rust","ffi","panic","cli"],"backgroundTag":"invalid-cli-argument","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}