4 ms·
Whenever I've used functionality like this before, the inevitable problem is the string ends up with nulls in it, meaning C and the library disagree on the leng
by CJefferson 7y ago
Whenever I've used functionality like this before, the inevitable problem is the string ends up with nulls in it, meaning C and the library disagree on the length of the string, which inevitably causes problems and corruption.
How do you handle this?
- jdsully 7y agoYou use sdslen() instead of strlen(). Ditto for everything else. Failure to do so results in subtle bugs.
- CJefferson 7y agoThen (in my opinion) the library shouldn't advertise itself as compatible with Null terminated C strings, as users will assume that means they can use strlen (or more likely, other functions which use strlen themselves). In a similar system I work on, we have a list of functions (like strlen, Strcat), where we carefully vet every occurrence in the code base. It's annoying but the only way to stop subtle bugs we've found.
- jdsully 7y agoRedis doesn’t really have this problem because its mostly developed by Antirez.
- stouset 7y agoThen why bother making these strings superficially compatible with C strings? Just printf or am I missing something?
- jdsully 7y agoThe same reason std::string has the c_str() method. It’s convenient.