Hacker News new | ask | show | jobs
by dajtxx 1124 days ago
I know it's not your code, but related to this comment, it looks like it missing a check for lks == null?

https://github.com/robdelacruz/lkwebserver/blob/main/lkstrin...

2 comments

That assert is checking a library invariant; it should never fail unless there’s a bug in the string library itself (although I’m not entirely sure this string library would tolerate a malloc failure from a quick glance through).

This is distinct from checking the parameters; if lks is null then the user of the API has made an error. Some libraries may sanitise user parameters, others don’t. At any rate, an assert would be the wrong choice to check user parameters since this would result in a (recoverable) user error leading to an abort unless the assert is disabled at compile time (-DNDEBUG), returning an error would be a better choice.

assert is an acceptable way to deal with precondition failures.
Thanks, much of the asserts were scaffolding while I was programming the helper functions. They function similar to comments, reminding me of the internal conditions that should always be valid.