From 43af16123557f0dd169363551d5d504fabf5d9a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rasmus=20Br=C3=B6nneg=C3=A5rd?= <1162652+rasmusgo@users.noreply.github.com> Date: Tue, 7 May 2024 12:15:07 +0200 Subject: [PATCH 1/4] Remove null terminators from other extensions --- generator/src/main.rs | 6 +++++- openxr/src/generated.rs | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/generator/src/main.rs b/generator/src/main.rs index a525c12c..78ab52dd 100644 --- a/generator/src/main.rs +++ b/generator/src/main.rs @@ -1518,7 +1518,11 @@ impl Parser { #(#ext_set_inits)* bytes => { if let Ok(name) = std::str::from_utf8(bytes) { - out.other.push(name.into()); + if bytes.last() == Some(&0) { + out.other.push(name[0..(bytes.len() - 1)].into()); + } else { + out.other.push(name.into()); + } } } } diff --git a/openxr/src/generated.rs b/openxr/src/generated.rs index 8b9ee106..ad4e6a0c 100644 --- a/openxr/src/generated.rs +++ b/openxr/src/generated.rs @@ -687,7 +687,11 @@ impl ExtensionSet { } bytes => { if let Ok(name) = std::str::from_utf8(bytes) { - out.other.push(name.into()); + if bytes.last() == Some(&0) { + out.other.push(name[0..(bytes.len() - 1)].into()); + } else { + out.other.push(name.into()); + } } } } From bab066b14db8b30b9c852a98827e284232781866 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rasmus=20Br=C3=B6nneg=C3=A5rd?= <1162652+rasmusgo@users.noreply.github.com> Date: Sat, 18 May 2024 16:28:54 +0200 Subject: [PATCH 2/4] Add comment about testing null terminated string --- generator/src/main.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/generator/src/main.rs b/generator/src/main.rs index 78ab52dd..31f96b91 100644 --- a/generator/src/main.rs +++ b/generator/src/main.rs @@ -1518,6 +1518,7 @@ impl Parser { #(#ext_set_inits)* bytes => { if let Ok(name) = std::str::from_utf8(bytes) { + // This is probably always true but we check just to be sure. if bytes.last() == Some(&0) { out.other.push(name[0..(bytes.len() - 1)].into()); } else { From 8f5b67e73ef0ee30810f7b94a6501d79819054c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rasmus=20Br=C3=B6nneg=C3=A5rd?= <1162652+rasmusgo@users.noreply.github.com> Date: Sun, 19 May 2024 00:46:49 +0200 Subject: [PATCH 3/4] Use CStr::from_bytes_with_nul for extensions names --- generator/src/main.rs | 13 +++++-------- openxr/src/generated.rs | 12 +++++------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/generator/src/main.rs b/generator/src/main.rs index 31f96b91..b16e7bd3 100644 --- a/generator/src/main.rs +++ b/generator/src/main.rs @@ -1495,6 +1495,7 @@ impl Parser { #![allow(clippy::wrong_self_convention, clippy::transmute_ptr_to_ptr)] use std::borrow::Cow; + use std::ffi::CStr; use std::mem::MaybeUninit; pub use sys::{#(#reexports),*}; pub use sys::platform::{EGLenum, VkFilter, VkSamplerMipmapMode, VkSamplerAddressMode, VkComponentSwizzle}; @@ -1517,14 +1518,10 @@ impl Parser { match crate::fixed_str_bytes(&ext.extension_name) { #(#ext_set_inits)* bytes => { - if let Ok(name) = std::str::from_utf8(bytes) { - // This is probably always true but we check just to be sure. - if bytes.last() == Some(&0) { - out.other.push(name[0..(bytes.len() - 1)].into()); - } else { - out.other.push(name.into()); - } - } + let cstr = CStr::from_bytes_with_nul(bytes) + .expect("extension names should be null terminated strings"); + let string = String::from_utf8_lossy(cstr.to_bytes()).to_string(); + out.other.push(string); } } } diff --git a/openxr/src/generated.rs b/openxr/src/generated.rs index ad4e6a0c..99fb8847 100644 --- a/openxr/src/generated.rs +++ b/openxr/src/generated.rs @@ -2,6 +2,7 @@ #![allow(clippy::wrong_self_convention, clippy::transmute_ptr_to_ptr)] use crate::*; use std::borrow::Cow; +use std::ffi::CStr; use std::mem::MaybeUninit; pub use sys::platform::{ EGLenum, VkComponentSwizzle, VkFilter, VkSamplerAddressMode, VkSamplerMipmapMode, @@ -686,13 +687,10 @@ impl ExtensionSet { out.htcx_vive_tracker_interaction = true; } bytes => { - if let Ok(name) = std::str::from_utf8(bytes) { - if bytes.last() == Some(&0) { - out.other.push(name[0..(bytes.len() - 1)].into()); - } else { - out.other.push(name.into()); - } - } + let cstr = CStr::from_bytes_with_nul(bytes) + .expect("extension names should be null terminated strings"); + let string = String::from_utf8_lossy(cstr.to_bytes()).to_string(); + out.other.push(string); } } } From 547deb1a9ba6e7747c573a855f31f893ca118912 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rasmus=20Br=C3=B6nneg=C3=A5rd?= <1162652+rasmusgo@users.noreply.github.com> Date: Sun, 19 May 2024 11:02:23 +0200 Subject: [PATCH 4/4] Use .to_string() instead of String::from_utf8_lossy --- generator/src/main.rs | 5 ++++- openxr/src/generated.rs | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/generator/src/main.rs b/generator/src/main.rs index b16e7bd3..ec3c689a 100644 --- a/generator/src/main.rs +++ b/generator/src/main.rs @@ -1520,7 +1520,10 @@ impl Parser { bytes => { let cstr = CStr::from_bytes_with_nul(bytes) .expect("extension names should be null terminated strings"); - let string = String::from_utf8_lossy(cstr.to_bytes()).to_string(); + let string = cstr + .to_str() + .expect("extension names should be valid UTF-8") + .to_string(); out.other.push(string); } } diff --git a/openxr/src/generated.rs b/openxr/src/generated.rs index 99fb8847..c15e6d0b 100644 --- a/openxr/src/generated.rs +++ b/openxr/src/generated.rs @@ -689,7 +689,10 @@ impl ExtensionSet { bytes => { let cstr = CStr::from_bytes_with_nul(bytes) .expect("extension names should be null terminated strings"); - let string = String::from_utf8_lossy(cstr.to_bytes()).to_string(); + let string = cstr + .to_str() + .expect("extension names should be valid UTF-8") + .to_string(); out.other.push(string); } }