3 ms·
> Technically C11 has strcpy_s and strcat_s "Theoretically" is the word you're looking for: they're part of the optional Annex K so technically you can't rely
by masklinn 6y ago
> Technically C11 has strcpy_s and strcat_s
"Theoretically" is the word you're looking for: they're part of the optional Annex K so technically you can't rely on them being available in a portable program.
And they're basically not implemented by anyone but microsoft (which created them and lobbied for their inclusion).
- jchw 6y agoI didn’t know that it was Microsoft that lobbied for them; that perplexes me since I thought Microsoft’s version of them were a bit different (for example, I think C11’s explicitly fail on overlapping inputs where Microsoft specifies undefined behavior) and because Microsoft didn’t bother supporting C99 for the longest time. (Probably still don’t, since VLA was not optional in C99, IIRC. I think Microsoft was right to avoid VLA, though.)
- astrange 6y agoVLA syntax can be useful because you can cast other pointers to them - for instance you can cast int* to int[w][h] and then access it as [y][x] instead of [y*w+x]. As a bonus this crashes icc if you do it.
- jchw 6y ago>As a bonus this crashes icc if you do it. I thought this was a pretty funny thing but unfortunately when I tried this on ICC it seemed to compile just fine. Though I am amused by one thing: the VLA version generates worse code on all compilers I've tried. Seems to validate the common refrain that VLAs tend to break optimizations. (Surely it's worse when you have an on-stack VLA though.)
- astrange 6y agoThis does it: https://gcc.godbolt.org/z/5fz8sM https://gcc.godbolt.org/z/5fz8sM I'm not sure if they take bug reports if you're not a customer, but this one goes back at least 8 years.
- julian55 6y agoThey are also implemented by embedded compilers such as IAR and Keil.
- moonchild 6y agoMicrosoft's implementations are distinct and incompatible (and they haven't changed to be compatible with the standard versions because of backwards compatibility).
- loeg 6y agoMicrosoft doesn't actually implement Annex K! Annex K is based on MSFT's routines, but they diverged. So Annex K is portable nowhere, in addition to having largely awful APIs. http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1967.htm#:~:text=Microsoft%20Visual%20Studio%20implements%20an%20early%20version http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1967.htm#:~... > Microsoft Visual Studio implements an early version of the APIs. However, the implementation is incomplete and conforms neither to C11 nor to the original TR 24731-1. For example, it doesn't provide the set_constraint_handler_s function but instead defines a _invalid_parameter_handler _set_invalid_parameter_handler(_invalid_parameter_handler) function with similar behavior but a slightly different and incompatible signature. It also doesn't define the abort_handler_s and ignore_handler_s functions, the memset_s function (which isn't part of the TR), or the RSIZE_MAX macro.The Microsoft implementation also doesn't treat overlapping source and destination sequences as runtime-constraint violations and instead has undefined behavior in such cases. > As a result of the numerous deviations from the specification the Microsoft implementation cannot be considered conforming or portable.
- Tempest1981 6y agoImportant to note that strcpy_s doesn't truncate, it aborts your app if it fails: > "if the destination string size dest_size is too small, the invalid parameter handler is invoked" > "The invalid parameter handler dispatch function calls the currently assigned invalid parameter handler. By default, the invalid parameter calls _invoke_watson, which causes the application to close and generate a mini-dump." https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/strcpy-s-wcscpy-s-mbscpy-s?view=msvc-160 https://docs.microsoft.com/en-us/cpp/c-runtime-library/refer...