Skip to content

Commit 92084df

Browse files
authored
generator: Wrap _as_c_str() getter for possibly-pointers in Option (#860)
While this function is already marked `unsafe` to represent cases where an invalid pointer might be dereferenced, it should at least be obvious to the caller that there is a real chance for `NULL` pointers in these `CStr` getters, which will now be returned as `None`. This function won't be used in `Debug` now as the dereference operation is still `unsafe`. The `_as_c_str()` getters for static arrays is left untouched, as the data is read directly from the known-valid struct here.
1 parent e992225 commit 92084df

File tree

2 files changed

+95
-32
lines changed

2 files changed

+95
-32
lines changed

ash/src/vk/definitions.rs

Lines changed: 84 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,8 +1062,12 @@ impl<'a> ApplicationInfo<'a> {
10621062
self
10631063
}
10641064
#[inline]
1065-
pub unsafe fn application_name_as_c_str(&self) -> &core::ffi::CStr {
1066-
core::ffi::CStr::from_ptr(self.p_application_name)
1065+
pub unsafe fn application_name_as_c_str(&self) -> Option<&core::ffi::CStr> {
1066+
if self.p_application_name.is_null() {
1067+
None
1068+
} else {
1069+
Some(core::ffi::CStr::from_ptr(self.p_application_name))
1070+
}
10671071
}
10681072
#[inline]
10691073
pub fn application_version(mut self, application_version: u32) -> Self {
@@ -1076,8 +1080,12 @@ impl<'a> ApplicationInfo<'a> {
10761080
self
10771081
}
10781082
#[inline]
1079-
pub unsafe fn engine_name_as_c_str(&self) -> &core::ffi::CStr {
1080-
core::ffi::CStr::from_ptr(self.p_engine_name)
1083+
pub unsafe fn engine_name_as_c_str(&self) -> Option<&core::ffi::CStr> {
1084+
if self.p_engine_name.is_null() {
1085+
None
1086+
} else {
1087+
Some(core::ffi::CStr::from_ptr(self.p_engine_name))
1088+
}
10811089
}
10821090
#[inline]
10831091
pub fn engine_version(mut self, engine_version: u32) -> Self {
@@ -3755,8 +3763,12 @@ impl<'a> PipelineShaderStageCreateInfo<'a> {
37553763
self
37563764
}
37573765
#[inline]
3758-
pub unsafe fn name_as_c_str(&self) -> &core::ffi::CStr {
3759-
core::ffi::CStr::from_ptr(self.p_name)
3766+
pub unsafe fn name_as_c_str(&self) -> Option<&core::ffi::CStr> {
3767+
if self.p_name.is_null() {
3768+
None
3769+
} else {
3770+
Some(core::ffi::CStr::from_ptr(self.p_name))
3771+
}
37603772
}
37613773
#[inline]
37623774
pub fn specialization_info(mut self, specialization_info: &'a SpecializationInfo<'a>) -> Self {
@@ -7869,8 +7881,12 @@ impl<'a> DisplayPropertiesKHR<'a> {
78697881
self
78707882
}
78717883
#[inline]
7872-
pub unsafe fn display_name_as_c_str(&self) -> &core::ffi::CStr {
7873-
core::ffi::CStr::from_ptr(self.display_name)
7884+
pub unsafe fn display_name_as_c_str(&self) -> Option<&core::ffi::CStr> {
7885+
if self.display_name.is_null() {
7886+
None
7887+
} else {
7888+
Some(core::ffi::CStr::from_ptr(self.display_name))
7889+
}
78747890
}
78757891
#[inline]
78767892
pub fn physical_dimensions(mut self, physical_dimensions: Extent2D) -> Self {
@@ -9172,8 +9188,12 @@ impl<'a> DebugMarkerObjectNameInfoEXT<'a> {
91729188
self
91739189
}
91749190
#[inline]
9175-
pub unsafe fn object_name_as_c_str(&self) -> &core::ffi::CStr {
9176-
core::ffi::CStr::from_ptr(self.p_object_name)
9191+
pub unsafe fn object_name_as_c_str(&self) -> Option<&core::ffi::CStr> {
9192+
if self.p_object_name.is_null() {
9193+
None
9194+
} else {
9195+
Some(core::ffi::CStr::from_ptr(self.p_object_name))
9196+
}
91779197
}
91789198
}
91799199
#[repr(C)]
@@ -9266,8 +9286,12 @@ impl<'a> DebugMarkerMarkerInfoEXT<'a> {
92669286
self
92679287
}
92689288
#[inline]
9269-
pub unsafe fn marker_name_as_c_str(&self) -> &core::ffi::CStr {
9270-
core::ffi::CStr::from_ptr(self.p_marker_name)
9289+
pub unsafe fn marker_name_as_c_str(&self) -> Option<&core::ffi::CStr> {
9290+
if self.p_marker_name.is_null() {
9291+
None
9292+
} else {
9293+
Some(core::ffi::CStr::from_ptr(self.p_marker_name))
9294+
}
92719295
}
92729296
#[inline]
92739297
pub fn color(mut self, color: [f32; 4]) -> Self {
@@ -18726,8 +18750,12 @@ impl<'a> DebugUtilsObjectNameInfoEXT<'a> {
1872618750
self
1872718751
}
1872818752
#[inline]
18729-
pub unsafe fn object_name_as_c_str(&self) -> &core::ffi::CStr {
18730-
core::ffi::CStr::from_ptr(self.p_object_name)
18753+
pub unsafe fn object_name_as_c_str(&self) -> Option<&core::ffi::CStr> {
18754+
if self.p_object_name.is_null() {
18755+
None
18756+
} else {
18757+
Some(core::ffi::CStr::from_ptr(self.p_object_name))
18758+
}
1873118759
}
1873218760
}
1873318761
#[repr(C)]
@@ -18816,8 +18844,12 @@ impl<'a> DebugUtilsLabelEXT<'a> {
1881618844
self
1881718845
}
1881818846
#[inline]
18819-
pub unsafe fn label_name_as_c_str(&self) -> &core::ffi::CStr {
18820-
core::ffi::CStr::from_ptr(self.p_label_name)
18847+
pub unsafe fn label_name_as_c_str(&self) -> Option<&core::ffi::CStr> {
18848+
if self.p_label_name.is_null() {
18849+
None
18850+
} else {
18851+
Some(core::ffi::CStr::from_ptr(self.p_label_name))
18852+
}
1882118853
}
1882218854
#[inline]
1882318855
pub fn color(mut self, color: [f32; 4]) -> Self {
@@ -18961,8 +18993,12 @@ impl<'a> DebugUtilsMessengerCallbackDataEXT<'a> {
1896118993
self
1896218994
}
1896318995
#[inline]
18964-
pub unsafe fn message_id_name_as_c_str(&self) -> &core::ffi::CStr {
18965-
core::ffi::CStr::from_ptr(self.p_message_id_name)
18996+
pub unsafe fn message_id_name_as_c_str(&self) -> Option<&core::ffi::CStr> {
18997+
if self.p_message_id_name.is_null() {
18998+
None
18999+
} else {
19000+
Some(core::ffi::CStr::from_ptr(self.p_message_id_name))
19001+
}
1896619002
}
1896719003
#[inline]
1896819004
pub fn message_id_number(mut self, message_id_number: i32) -> Self {
@@ -18975,8 +19011,12 @@ impl<'a> DebugUtilsMessengerCallbackDataEXT<'a> {
1897519011
self
1897619012
}
1897719013
#[inline]
18978-
pub unsafe fn message_as_c_str(&self) -> &core::ffi::CStr {
18979-
core::ffi::CStr::from_ptr(self.p_message)
19014+
pub unsafe fn message_as_c_str(&self) -> Option<&core::ffi::CStr> {
19015+
if self.p_message.is_null() {
19016+
None
19017+
} else {
19018+
Some(core::ffi::CStr::from_ptr(self.p_message))
19019+
}
1898019020
}
1898119021
#[inline]
1898219022
pub fn queue_labels(mut self, queue_labels: &'a [DebugUtilsLabelEXT<'a>]) -> Self {
@@ -42334,8 +42374,12 @@ impl<'a> CuFunctionCreateInfoNVX<'a> {
4233442374
self
4233542375
}
4233642376
#[inline]
42337-
pub unsafe fn name_as_c_str(&self) -> &core::ffi::CStr {
42338-
core::ffi::CStr::from_ptr(self.p_name)
42377+
pub unsafe fn name_as_c_str(&self) -> Option<&core::ffi::CStr> {
42378+
if self.p_name.is_null() {
42379+
None
42380+
} else {
42381+
Some(core::ffi::CStr::from_ptr(self.p_name))
42382+
}
4233942383
}
4234042384
}
4234142385
#[repr(C)]
@@ -44790,8 +44834,12 @@ impl<'a> CudaFunctionCreateInfoNV<'a> {
4479044834
self
4479144835
}
4479244836
#[inline]
44793-
pub unsafe fn name_as_c_str(&self) -> &core::ffi::CStr {
44794-
core::ffi::CStr::from_ptr(self.p_name)
44837+
pub unsafe fn name_as_c_str(&self) -> Option<&core::ffi::CStr> {
44838+
if self.p_name.is_null() {
44839+
None
44840+
} else {
44841+
Some(core::ffi::CStr::from_ptr(self.p_name))
44842+
}
4479544843
}
4479644844
}
4479744845
#[repr(C)]
@@ -51450,8 +51498,12 @@ impl<'a> ShaderCreateInfoEXT<'a> {
5145051498
self
5145151499
}
5145251500
#[inline]
51453-
pub unsafe fn name_as_c_str(&self) -> &core::ffi::CStr {
51454-
core::ffi::CStr::from_ptr(self.p_name)
51501+
pub unsafe fn name_as_c_str(&self) -> Option<&core::ffi::CStr> {
51502+
if self.p_name.is_null() {
51503+
None
51504+
} else {
51505+
Some(core::ffi::CStr::from_ptr(self.p_name))
51506+
}
5145551507
}
5145651508
#[inline]
5145751509
pub fn set_layouts(mut self, set_layouts: &'a [DescriptorSetLayout]) -> Self {
@@ -52262,8 +52314,12 @@ impl<'a> PipelineShaderStageNodeCreateInfoAMDX<'a> {
5226252314
self
5226352315
}
5226452316
#[inline]
52265-
pub unsafe fn name_as_c_str(&self) -> &core::ffi::CStr {
52266-
core::ffi::CStr::from_ptr(self.p_name)
52317+
pub unsafe fn name_as_c_str(&self) -> Option<&core::ffi::CStr> {
52318+
if self.p_name.is_null() {
52319+
None
52320+
} else {
52321+
Some(core::ffi::CStr::from_ptr(self.p_name))
52322+
}
5226752323
}
5226852324
#[inline]
5226952325
pub fn index(mut self, index: u32) -> Self {

generator/src/lib.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,8 @@ impl FieldExt for vkxml::Field {
838838

839839
fn is_pointer_to_static_sized_array(&self) -> bool {
840840
matches!(self.array, Some(vkxml::ArrayType::Dynamic))
841+
// TODO: This should not be hardcoded to one field name, but recognize this pattern somehow
842+
// (by checking if the len field is an expression consisting of purely constants)
841843
&& self.name.as_deref() == Some("pVersionData")
842844
}
843845
}
@@ -2009,8 +2011,12 @@ fn derive_getters_and_setters(
20092011
}
20102012
#deprecated
20112013
#[inline]
2012-
pub unsafe fn #param_ident_as_c_str(&self) -> &core::ffi::CStr {
2013-
core::ffi::CStr::from_ptr(self.#param_ident)
2014+
pub unsafe fn #param_ident_as_c_str(&self) -> Option<&core::ffi::CStr> {
2015+
if self.#param_ident.is_null() {
2016+
None
2017+
} else {
2018+
Some(core::ffi::CStr::from_ptr(self.#param_ident))
2019+
}
20142020
}
20152021
});
20162022
} else if is_static_array(field) {
@@ -2047,8 +2053,9 @@ fn derive_getters_and_setters(
20472053

20482054
let mut slice_param_ty_tokens = field.safe_type_tokens(quote!('a), type_lifetime.clone(), None);
20492055

2050-
// vkxml wrongly annotates static arrays with a len= field is dynamic. These fields have a static length,
2051-
// and a runtime field to describe the actual number of valid items in this static array.
2056+
// vkxml considers static arrays with len= to be Dynamic (which they are to some
2057+
// extent). These fields have a static upper-bound length, and a runtime field to
2058+
// describe the actual number of valid items in this static array.
20522059
if is_static_array(field) {
20532060
let array_size_ident = format_ident!("{}", array_size.to_snake_case());
20542061
let param_ident_short_as_slice = format_ident!("{}_as_slice", param_ident_short);

0 commit comments

Comments
 (0)