News for hackers. Stuff that matters.
Re:Simple Dynamic Strings library for C, compatible with nul... by CJefferson on Tuesday December 03, 2019 @02:01PM (#21695418)
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?

Re:Simple Dynamic Strings library for C, compatible with nul... by jdsully on Wednesday December 04, 2019 @03:33AM (#21700565)
You use sdslen() instead of strlen(). Ditto for everything else. Failure to do so results in subtle bugs.
Re:Simple Dynamic Strings library for C, compatible with nul... by CJefferson on Wednesday December 04, 2019 @08:39AM (#21702030)
Then (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.

Re:Simple Dynamic Strings library for C, compatible with nul... by jdsully on Wednesday December 04, 2019 @01:17PM (#21705112)
Redis doesn’t really have this problem because its mostly developed by Antirez.
Re:Simple Dynamic Strings library for C, compatible with nul... by stouset on Wednesday December 04, 2019 @08:00AM (#21701777)
Then why bother making these strings superficially compatible with C strings? Just printf or am I missing something?
Re:Simple Dynamic Strings library for C, compatible with nul... by jdsully on Wednesday December 04, 2019 @01:18PM (#21705128)
The same reason std::string has the c_str() method. It’s convenient.