3 ms·
That the _s functions are part of Annex K, which is optional, often not very good[0], and whose reliance on runtime checking is often considered problematic giv
by masklinn 6y ago
That the _s functions are part of Annex K, which is optional, often not very good[0], and whose reliance on runtime checking is often considered problematic given we're talking about C developers. The politics referred to are because the annex is basically a bunch of Microsoft extensions pasted in the standard.
Take printf_s for instance, its main job is to forbid %n, at runtime[1]. It's much safer and more reliable to perform static analysis to mandate literal format strings and forbid %n.
[0] aka not very well designed, and not really good at leading developers away from mistakes, they also deviated from some "unsafe" functions in ways which could be quite critical e.g. strncpy_s infamously does not zero the destination buffer like strncpy, which might not be expected by people catering to codebases knowingly taking advantage of this property
[1] it will also check for %s null pointer which is somewhat more helpful, but again we're talking about C people here
- colejohnson66 6y ago> The politics referred to are because the annex is basically a bunch of Microsoft extensions pasted in the standard. So... M$FT bad, therefore anything they do is bad? That makes no sense.
- pjmlp 6y agoBasically, almost no compiler vendor implemented it, they also had the issue that you still need to handle pointer and length as separated arguments. So instead of fixing the security issues, like having some struct based type, the whole annex was made optional, thus everyone that was against it, just doesn't offer any alternative.
- saagarjha 6y agoNo, it’s actually “this set of extensions Microsoft wrote is bad and they forced it into the standard”.
- pjmlp 6y agoStill waiting for the counter proposal from anyone else in WG14, though. Instead OS vendors seem to go via hardware memory tagging solutions, than keeping up a lost war, as we have discussed multiple times.
- a1369209993 6y ago> M$FT bad, therefore anything they do is bad That's backward; it's actually "anything [rather, most things] Microsoft does is bad, therefore Microsoft is bad". And (most of) Annex K is a example of that. (The strncpy/_s one is bit odd though, because strncpy was specifically for initializing a fixed-length buffer, which is not nul-terminated, but rather nul-padded. strncpy works fine for that purpose, although nobody uses nul-padded buffers anymore. Using it as a size-aware strcpy was always wrong; you should use strlen+memcpy or a separate str[lzc...]cpy (or strcpy_s I suppose) instead.)
- saagarjha 6y agoStandard C didn’t even have a good way to do string copying safely until memccpy in C20. Most “safe” functions have unacceptable performance tradeoffs, including most extensions.
- pjmlp 6y agoUsually the unacceptable performance tends to be argued with developer personal point of view, instead of profile data in the actual production deployment.
- rurban 6y agoWell, safeclib memcpy_s (Posix standard, Annex K) is on good compilers faster than glibc memcpy, whilst also being safe. Unacceptable is just gcc, mostly. Haven't seen a safe memset yet, other than mine.
- nitrogen 6y agoit will also check for %s null pointer Glibc will now print something like "(null)" in this case instead of segfaulting.