[Pkg-rust-maintainers] Bug#1069047: librust-bindgen-dev: Thread 'main' panicked at 'called Result::unwrap() on an Err value

NoisyCoil noisycoil at tutanota.com
Mon Apr 15 14:55:22 BST 2024


Package: librust-bindgen-dev
Version: 0.66.1-4
Severity: normal
Tags: patch upstream
X-Debbugs-Cc: noisycoil at tutanota.com

Dear Maintainer,

While building the linux kernel with additional patches to enable support for Apple Silicon, I came across a Rust panic due to the way bindgen v0.66 generates bindings. The error message I got was

> Thread 'main' panicked at 'called Result::unwrap() on an Err value: FromBytesWithNulError { kind: InteriorNul(x) }

(where x is an integer I did not take note of). After a bit of digging I found out this is a known regression introduced in v0.66 and fixed in v0.68, having to do with how bindgen deals with strings that have null bytes inside them. More detail can be found at https://github.com/rust-lang/rust-bindgen/issues/2566 (issue) and https://github.com/rust-lang/rust-bindgen/pull/2567 (resolution). Would it be possible to upload a version of bindgen v0.66 with the patch backported from v0.68, or, as an alternative, to upgrade to the latter? I checked that the original patch applies cleanly to v0.66 as present in the Debian archive, e.g. by streamlining it and modifying the path of the patched file like in attachment.

Thank you,

NoisyCoil


*** /home/noisycoil/bindgen.patch
--- a/codegen/mod.rs
+++ b/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)]



More information about the Pkg-rust-maintainers mailing list