Repository: rust-lang/rust-bindgen (rust)

Issue:
Thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: FromBytesWithNulError { kind: InteriorNul(29) }
### Input C/C++ Header

Sorry, but I wasn't able to reduce headers for some reason - error disappears. I am attaching the full project and an archive with the Rizin headers.

The system is macOS ARM64 13.4

[bindgen-crash-reproducer.zip](https://github.com/rust-lang/rust-bindgen/files/11862629/bindgen-crash-reproducer.zip)

[rizin-headers.zip](https://github.com/rust-lang/rust-bindgen/files/11862634/rizin-headers.zip)

[error_report.log.zip](https://github.com/rust-lang/rust-bindgen/files/11862639/error_report.log.zip)

The headers are of the Rizin project: https://github.com/rizinorg/rizin

### Bindgen Invocation

See `build.rs` in the attached archive

### Actual Results

```
  --- stderr
  thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: FromBytesWithNulError { kind: InteriorNul(29) }', /Users/anton.kochkov/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.66.1/codegen/mod.rs:717:71
  stack backtrace:
     0:        0x1047a73cc - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hf56058dac04a8100
     1:        0x1047d6cc0 - core::fmt::write::h887ee594c50d2f6b
     2:        0x1047b454c - std::io::Write::write_fmt::h8e068fbe7ba944b7
     3:        0x1047a71dc - std::sys_common::backtrace::print::hefee1a8be582057a
     4:        0x1047b1b78 - std::panicking::default_hook::{{closure}}::h7ba479390a999ae5
     5:        0x1047b1840 - std::panicking::default_hook::hc4ff20421fd3aa8b
     6:        0x1047b2188 - std::panicking::rust_panic_with_hook::h97e5266e8ce2f24f
     7:        0x1047a76b8 - std::panicking::begin_panic_handler::{{closure}}::h5c38d4c71a65b53e
     8:        0x1047a74c4 - std::sys_common::backtrace::__rust_end_short_backtrace::h9c79ccffe3575672
     9:        0x1047b1da4 - _rust_begin_unwind
    10:        0x1047f1a10 - core::panicking::panic_fmt::h8d86c61b68da2636
    11:        0x1047f1978 - core::result::unwrap_failed::h2eeab09a9c5ad8bf
    12:        0x1042d8030 - core::result::Result<T,E>::unwrap::h1b98d57917a790d1
                                 at /private/tmp/rust-20230613-7622-103lepv/rustc-1.70.0-src/library/core/src/result.rs:1089:23
    13:        0x1042dd688 - <bindgen::ir::var::Var as bindgen::codegen::CodeGenerator>::codegen::hfa34c4704686e659
                                 at /Users/anton.kochkov/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.66.1/codegen/mod.rs:717:32
    14:        0x1043dbb70 - <bindgen::ir::item::Item as bindgen::codegen::CodeGenerator>::codegen::h6555b708a86e7e40
                                 at /Users/anton.kochkov/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.66.1/codegen/mod.rs:540:17
    15:        0x1043dc458 - <bindgen::ir::module::Module as bindgen::codegen::CodeGenerator>::codegen::{{closure}}::h0ab018d8d97f5085
                                 at /Users/anton.kochkov/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.66.1/codegen/mod.rs:566:21
    16:        0x1043dbd68 - <bindgen::ir::module::Module as bindgen::codegen::CodeGenerator>::codegen::hb4681d111f39a976
                                 at /Users/anton.kochkov/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.66.1/codegen/mod.rs:596:13
    17:        0x1043dbb10 - <bindgen::ir::item::Item as bindgen::codegen::CodeGenerator>::codegen::h6555b708a86e7e40
                                 at /Users/anton.kochkov/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.66.1/codegen/mod.rs:534:17
    18:        0x1043e52a0 - bindgen::codegen::codegen::{{closure}}::h92236ee983b08dc7
                                 at /Users/anton.kochkov/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.66.1/codegen/mod.rs:4719:9
    19:        0x1042bc200 - bindgen::ir::context::BindgenContext::gen::h2137dffcdda25f99
                                 at /Users/anton.kochkov/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.66.1/ir/context.rs:1218:19
    20:        0x1043e4764 - bindgen::codegen::codegen::h98b27d48bd6c34e1
                                 at /Users/anton.kochkov/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.66.1/codegen/mod.rs:4683:5
    21:        0x1042c9940 - bindgen::Bindings::generate::hbd2b5f712cc64035
                                 at /Users/anton.kochkov/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.66.1/lib.rs:896:13
    22:        0x1042c7b84 - bindgen::Builder::generate::hd4e381d53245babd
                                 at /Users/anton.kochkov/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.66.1/lib.rs:338:9
    23:        0x10425afa4 - build_script_build::main::h42d09c1057897f8a
                                 at /Users/anton.kochkov/Misc/bindgen-crash-reproducer/build.rs:51:20
    24:        0x10425b470 - core::ops::function::FnOnce::call_once::hf83113fa8666a300
                                 at /private/tmp/rust-20230613-7622-103lepv/rustc-1.70.0-src/library/core/src/ops/function.rs:250:5
    25:        0x10425a458 - std::sys_common::backtrace::__rust_begin_short_backtrace::hdcfdf19e930f4e76
                                 at /private/tmp/rust-20230613-7622-103lepv/rustc-1.70.0-src/library/std/src/sys_common/backtrace.rs:134:18
    26:        0x10425c018 - std::rt::lang_start::{{closure}}::hf4635be123a6423d
                                 at /private/tmp/rust-20230613-7622-103lepv/rustc-1.70.0-src/library/std/src/rt.rs:166:18
    27:        0x1047b1cc8 - std::panicking::try::hd27950ade386a4c1
    28:        0x1047bcb14 - std::rt::lang_start_internal::h4c832f79c4cb8516
    29:        0x10425bfe4 - std::rt::lang_start::h3cc6ef3008c55f2f
                                 at /private/tmp/rust-20230613-7622-103lepv/rustc-1.70.0-src/library/std/src/rt.rs:165:17
    30:        0x10425b150 - _main
```

### Expected Results

No crash


Pull request description:
codegen: Generate CStr only if possible.
And fix a crash when strings have interior nulls.

Fixes #2566

Patch:
diff --git a/bindgen/codegen/mod.rs b/bindgen/codegen/mod.rs
index 11425e02..3d99fa02 100644
--- a/bindgen/codegen/mod.rs
+++ b/bindgen/codegen/mod.rs
@@ -714,18 +714,18 @@ impl CodeGenerator for Var {
                     let len = proc_macro2::Literal::usize_unsuffixed(
                         cstr_bytes.len(),
                     );
-                    let cstr = CStr::from_bytes_with_nul(&cstr_bytes).unwrap();
 
                     // TODO: Here we ignore the type we just made up, probably
                     // we should refactor how the variable type and ty ID work.
                     let array_ty = quote! { [u8; #len] };
                     let cstr_ty = quote! { ::#prefix::ffi::CStr };
 
-                    let bytes = proc_macro2::Literal::byte_string(
-                        cstr.to_bytes_with_nul(),
-                    );
+                    let bytes = proc_macro2::Literal::byte_string(&cstr_bytes);
 
-                    if rust_features.const_cstr && options.generate_cstr {
+                    if options.generate_cstr &&
+                        rust_features.const_cstr &&
+                        CStr::from_bytes_with_nul(&cstr_bytes).is_ok()
+                    {
                         result.push(quote! {
                             #(#attrs)*
                             #[allow(unsafe_code)]


Test patch:
diff --git a/bindgen-tests/tests/expectations/tests/issue-2566-cstr.rs b/bindgen-tests/tests/expectations/tests/issue-2566-cstr.rs
new file mode 100644
index 00000000..4227cf29
--- /dev/null
+++ b/bindgen-tests/tests/expectations/tests/issue-2566-cstr.rs
@@ -0,0 +1,2 @@
+#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
+pub const FOO: &[u8; 4] = b"a\0b\0";
diff --git a/bindgen-tests/tests/expectations/tests/issue-2566.rs b/bindgen-tests/tests/expectations/tests/issue-2566.rs
new file mode 100644
index 00000000..4227cf29
--- /dev/null
+++ b/bindgen-tests/tests/expectations/tests/issue-2566.rs
@@ -0,0 +1,2 @@
+#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
+pub const FOO: &[u8; 4] = b"a\0b\0";
diff --git a/bindgen-tests/tests/headers/issue-2566-cstr.h b/bindgen-tests/tests/headers/issue-2566-cstr.h
new file mode 100644
index 00000000..674b8940
--- /dev/null
+++ b/bindgen-tests/tests/headers/issue-2566-cstr.h
@@ -0,0 +1,4 @@
+// bindgen-flags: --generate-cstr
+
+/// We should _not_ generate a cstr for this because cstr shouldn't have interior nulls.
+#define FOO "a\0b"
diff --git a/bindgen-tests/tests/headers/issue-2566.h b/bindgen-tests/tests/headers/issue-2566.h
new file mode 100644
index 00000000..6e15ec19
--- /dev/null
+++ b/bindgen-tests/tests/headers/issue-2566.h
@@ -0,0 +1,1 @@
+#define FOO "a\0b"