ash: empty slices don't always result in null pointers

Ran into this specifically for https://docs.rs/ash/0.29.0/ash/vk/struct.PipelineMultisampleStateCreateInfoBuilder.html#method.sample_mask – if I pass a reference to an empty slice, as_ptr doesn’t necessarily return NULL.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 15

Commits related to this issue

Most upvoted comments

@coffeenotfound it is fairly trivial to adjust this function as it was already overridden. Afaik it was overridden for this reason in the first place: the length of this array is specified by a latexmath expression instead of a simple name of a field. This appears to be the only member affected by the null-pointer issue, everything else seems to have a separate length field (or otherwise generate to invalid Rust).

Is this still an issue in practice? This case seems rare enough that it won’t hurt to have something along the lines of:

let mut builder = vk::PipelineMultisampleStateCreateInfo::builder()
    ...;

if need_to_set_sample_mask {
    builder = builder.sample_mask(my_slice);
}
...

It wouldn’t be too hard to change this though since pSampleMask is already special-cased:

https://github.com/MaikKlein/ash/blob/c7216f1c1e337f42572995c53ced178b41cf2dbe/generator/src/lib.rs#L1826-L1833

Specifically, that is appropriate if and only if there is an implicit length field and it’s correct to combine a nonzero length with a null pointer. This is very rare; the overwhelming majority of the time, either you pass a length of zero and the pointer doesn’t matter or the pointer must be nonnull.

The best solution would be taking Option<&'a [T]> where the Vulkan spec allows pointers to be null