3 ms·
Deep in the comments, 'slibc' is mentioned. I hadn't known about it, but this library provides str..._s() implementations of all the standard str...() function
by nkurz 12y ago
Deep in the comments, 'slibc' is mentioned. I hadn't known about it, but this library provides str..._s() implementations of all the standard str...() functions as defined in Annex K of the C11 standard (which I also hadn't known about).
https://code.google.com/p/slibc/ https://code.google.com/p/slibc/
At a glance, this seems like a better solution than any of alternatives: require the buffer length to always be specified, and call a user controlled handler instead of allowing an overflow. This handler defaults to abort().
While there are fine arguments to be made for using a real string library like bstring, are there downsides to the str_s approach compared to using the str, strp, or strl family of functions?
- hyperrail 12y agoOne issue with the str..._s() functions is that the invalid-parameter handler is a global setting - all code that calls the slibc functions and hits an invalid parameter problem will call the handler. This could cause anything from your application code to some random library you never heard of to make your program abort(). I personally don't think this is a big problem, as the kinds of issues that would cause the invalid parameter handler to be called usually are not recoverable no matter where they originate from. But I can definitely see why the loss of control is technically troubling - those random libraries could be calling set_constraint_handler_s() and thus causing your code to keep running and not abort(), forcing you to rely on your second defense of checking the return values from all the functions. (There's also a potential political issue with str..._s(), but I won't go into that one.)
- wahern 12y agoAnnex K is so useless that last time I checked not even Microsoft, the sponsor, implemented it completely. Also, if you dig into the Annex K API there are quite a few problems with it, both technical and practical. It was half-baked. Some parts are salvageable, but really the API as a whole should be trashed. strlcpy is useful and prevalent. Let's not let perfect be the enemy of good. Trying to solve string management problems in C by getting everybody to agree on a "safe", comprehensive API is misguided and a waste of time.