5 ms·
In principle, I feel like "Ignore the language-provided standard libraries and use this little macro I wrote in all your code" is bad advice. If the standard l
by AdmiralAsshat 8y ago
In principle, I feel like "Ignore the language-provided standard libraries and use this little macro I wrote in all your code" is bad advice.
If the standard libs are that bad, the community should really lobby for the inclusion of a "safe" string copy into the C standard. Failing that, use a battle-tested third-party library like bstrlib[0].
[0]http://bstring.sourceforge.net/ http://bstring.sourceforge.net/
- jstimpfle 8y agoStrings are so tiny an abstraction over plain memory that every C programmer ends up doing them their own way. And that can be a good thing. I already proposed a couple of ways to represent or muck with strings in my other comment.
- cliffy 8y agoHow is that a good thing? There are ways of abstracting/handling strings that are strictly safer than other ways. In an unsafe language like C, you're inviting problems by trying to do things in your own idiosyncratic way.
- jstimpfle 8y agoSometimes performance or flexibility are important concerns. But feel free to manage millions of small strings with std::string or even as GC'ed objects :). And incur the overhead of indirection when attaching bounded-size strings to other data.
- coldtea 8y agoRust manages to do it just fine...
- jstimpfle 8y agoHow?
- adrianN 8y agoDefaults are important. C++ provides a much safer string type as default and let's programmers do their own thing in case it doesn't work for their use case (I'd try providing a different allocator before writing a different string type tbh). C on the other hand doesn't really provide a string type at all and forces everyone to start by writing a string library if they don't want to hunt buffer overflows until hell freezes over.
- deleted 8y ago[deleted]
- pjmlp 8y agoApparently IBM and Unisys mainframe systems programming languages did just fine.
- coldtea 8y agoIn what way could ever that be a "good thing" over having 2-3 implementations of strings in the standard library that cover 99% of the different ways and trade-offs, and which each has a method to convert a string to any of the others?
- mikeash 8y agoWhat choice do you have? “Fix the standard” is ideal, but few programmers have the luxury of waiting a decade for the fix to happen and become available in all their target compilers.
- pcwalton 8y agoThe C standards committee is incredibly conservative, only adding new features if absolutely necessary. Unless the composition and goals of the committee drastically change, a new standard string API will never happen. If you must write C, you should use a real string library, and not string.h.
- iforgotpassword 8y agoNonsense, strncpy and friends are just fi2•∟1F↔Ñ
- adiusmus 8y agoLooks like a Perl script I read once.
- astrodust 8y agoThey should have a superset of the C standard for writing "secure C", as is important in some kernel or application code.
- lmm 8y agoUnfortunately C doesn't have a widely accepted dependency/build manager either.
- bitwize 8y agostrcpy_s, anyone?
- brucedawson 8y agostrcpy_s is a good option for those cases where the appropriate response to having too long an input string is terminating the program. However it is a poor choice when continuing execution is desired.
- rurban 8y agoIt is even C11: https://github.com/rurban/safeclib/blob/master/src/str/strcpy_s.c https://github.com/rurban/safeclib/blob/master/src/str/strcp...
- brucedawson 8y agostrcpy_safe is a template function, not a macro. That immediately makes it orders of magnitude better than you were claiming. I agree that a standard fix for this would be great. It's been five years since I wrote the post, the solution has been available for many years longer than that, and the standards committee has shown no interest in fixing this issue. In the interim I think it would be irresponsible for developers to continue using strncpy or any of the other unsafe options when strcpy_safe is so simple and effective.
- rurban 8y agoRather use safeclib. It is C11. It has a safe version of the wrongly spec'ed truncating functions, in this case strncpy_s. This version guarantees NULL termination. https://rurban.github.io/safeclib/ https://rurban.github.io/safeclib/