#Portable (-std=c99) parameter null-check

6 messages · Page 1 of 1 (latest)

mild kestrel
#

What do y'all think about the following syntax for requiring that a pointer argument is non-null?
(gcc/clang will issue a warning if they can detect that you're passing NULL)

void thing_do_stuff(thing_t thing[static 1]);

Would you consider this readable?
Would you easily understand what's happening if you encountered this in a library?

Would a macro make it better or worse?

#define MYLIB_NONNULL static 1
void thing_do_stuff(thing_t thing[MYLIB_NONNULL]);
jaunty aurora
#

a) i don't really think this is readable without some kind of macro
b) msvc won't compile this, which is entirely their fault but a thing to consider nonetheless

#

macro options i would put into the ring besides what you put: c #define MYLIB_NONNULL [static 1] void thing_do_stuff(thing_t thing MYLIB_NONNULL); the array syntax itself is unintuitive to me. but this reads a bit funny to me, so you could convert to a function like macro: ```c
#define MYLIB_NONNULL(param) param[static 1]
void thing_do_stuff(MYLIB_NONNULL(thing_t thing));

#

a problem i have with both of those is now the indirection is hidden now that i look at it properly. so idk maybe the array syntax with inner macro is best

mild kestrel
#

i don't really think this is readable without some kind of macro
that's what I was thinking. I'm guessing many people either don't know about this use of static in C, or wouldn't immediately make the connection between "at least one member" and "cannot be null".

a problem i have with both of those is now the indirection is hidden now that i look at it properly
yeah, unfortunately, there's no good way to indicate that the parameter is a pointer besides using the "array" syntax or just spelling it out: MYLIB_NONNULL_PTR(param)

msvc won't compile this
wait--what?
checks godbold
oh my god you're right
(so much for "supporting all required features"...)

well, thanks for the input/ideas (and pointing out my oversight!)

#

just one more reason to hate microsoft, I suppose...