The definition of ValidCStr
is mem(p+len, p+len+1) = '\0'
according to the primitive-sp of tag-std.
I think a better name for it would just be CStr
?
And we could just define a SP to include them all, and name it ValidCStr
, since SP are composable and custom. The grain of a SP depends on the crate author.
I think the reason to see five properties like that on an API is what we see from the safety doc comments: CStr in std::ffi - Rust
Yes, we can support that like this:
#[safety::discharges {
ValidCStr(ptr, _), ValidPtr(ptr, i8, _), NonNull(ptr), ValidNum(compute_nul_pos(ptr), 0..=isize::MAX), Alias(ptr, ret)
}]
as long as macro syntax is supported by the compiler: Rust Playground
And in principle, we can make SP arguments a bit smarter if the tool queries more info from rustc. We can know the type of ptr, and there's single argument, so why not elide it to be:
#[discharges { ValidCStr, ValidPtr, NonNull, ValidNum, Alias(*name, &STRUCTS) }]
The less we write, the more info from rustc. See Future possibilities Interaction with Rust type system in our proposal.
Also note that we can use safety::discharge;
to import the proc-macro for shorter code.
If there's a need to elaborate a SP in discharge macro, here's how to do
#[discharges {
ValidPtr("Detailed reasons to help readers understand why ptr is valid"),
ValidCStr, NonNull, ValidNum, Alias(*name, &STRUCTS)
}]