Guillaume <
mes...@bottle.org> writes:
> > Rather than wishing the C standard would be different than it is,
> > avoid the problem by using wrapper functions (disclaimer: not
> > compiled):
>
> Yes of course. It's straighforward and this way, you get the checks if
> you want/need them instread of imposing them on everyone regardless of
> the use case.
I wouldn't propose additional checks. Just define the behavior
naturally, i.e. if the size arguement is 0, it means 0 elements
starting at the pointed adresses are accessed, so the pointers don't
matter at all.
> Those std functions should be considered "low-level" functions.
> I for one frankly don't care if any of the above examples, like
> "memcmp(NULL, NULL, 0)" are UB. If you are bound to call those with
> such parameters, there's something wrong with your coding style
> IMHO.
Not in my opinion. There are often cases where the size of a dynamic
array to be allocated and then worked on, is calculated from other
values and 0 is a possible outcome[1]. This is, of cource some
special case, but often the algorithms work the same on these special
cases and no special handling would be required if the functions from
the standard lib like malloc() and memcpy() wouldn't behave specially.
[1] Even if sometimes these are not very useful use-cases of the
software, I prefer no to add arbitrary and unneeded limits to the
range of valid arguments, making the code more complex and less
general.
> And now, in particular cases for which it would make sense
> (because it could make your code more elegant or something), then you
> can use wrappers as suggested above.
Yes, and I will do this. But for the reader of the code it means, he
sees calls to safe_memcpy() et al. and has to first lookup where and
how they are defined instead of just seeing memcpy() that everyone
knows.
I know that's not that big a deal and there are more complex things in
the software field to be solved, but still I'd like more simple, clean,
and elegant low-level libs to work with.
Steve