67 points · by corbet · 12y ago · 51 comments
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?
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.)
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.
But if you know how long is the source and destiny buffers (and if you are using str[nl]cpy, probably you know it), you could use memcpy and get a much more faster copy.
char buffer[1 << 24}]
strncpy(buffer, "a", sizeof(buffer));
will set all of the array to zero, instead of just copying two bytes.And as you noted, it won't NUL terminate the string. Since strncpy() doesn't return C strings, it's not really a "C string" function. It's a horrible, inefficient bastardization which should be used by no one.
size_t len = (src_size < dst_size) ? src_size : dst_size;
memcpy (dst, src, len); // or memmove
dst[len-1] = 0;
So not fills unecesary 0's and it's much more faster that any str?cpy1) char buf[1024]; strncpy(buf, "hello", sizeof buf);
now fills in 5 characters, and 1019 0 bytes in the buffer. (i.e. you're writing 1018 nul bytes that's usually not needed)
2) char buf[5]; strncpy(buf, "hello", sizeof buf);
results in buf NOT being a string. (it isn't nul terminated). So with most use of strncpy, you'll have to also do
buf[sizeof buf - 1] = 0;PHP's Zend Engine has safely-handled strings, for example, but we do that by reference-counting them and having an explicit length.
https://github.com/git/git/blob/master/strbuf.c
https://www.kernel.org/pub/software/scm/git/docs/technical/a...
Somehow, the message seems to keep getting lost.
Visual C++ is a C++ compiler, that is the language that is relevant to keep supporting for systems programming in Windows. Time to move forward.
As of Windows 8 it is now possible to use C++ in DDK and kernel team is making the code compile C++ compatible.
C89 support is good enough for backwards compatibility and what has been added from C99 was what is required from the C++11 standard.
In fact, compound literal syntax is probably incompatible with C++, because similar syntax in C++ requires entirely different object lifetimes--expression scope in C++, block scope in C99.
Actually, I think the target may be C11, as I doubt they'll add VLAs.
(When I first discovered strlcpy/strlcat I went and changed a bunch of string-fiddling code to use them and it was really amazing how much simpler everything became. Virtually all of my bounds checks could go away, leaving just the string stuff. Much nicer? Well, I wouldn't go that far. But certainly fewer ways for it to go wrong.)
So how hard would it be to add a return value indicating this?
What most people want is some simple functions that that can tell you if something would have gone bad. (e.g. truncate the copied string and tell you if it was truncated)