I'm using clang
on a Debian 8 system. I have the standard C++ headers. And yet, there is no header defining strcpy_s
. Why is this?
# grep -iRHI 'strcpy_s' /usr 2>/dev/null
/usr/include/x86_64-linux-gnu/bits/string2.h: ? __strcpy_small (dest, __strcpy_args (src), \
/usr/include/x86_64-linux-gnu/bits/string2.h:__STRING_INLINE char *__strcpy_small (char *, __uint16_t, __uint16_t,
/usr/include/x86_64-linux-gnu/bits/string2.h:__strcpy_small (char *__dest,
/usr/include/x86_64-linux-gnu/bits/string2.h:__STRING_INLINE char *__strcpy_small (char *, __STRING2_COPY_ARR2,
/usr/include/x86_64-linux-gnu/bits/string2.h:__strcpy_small (char *__dest,
/usr/src/broadcom-sta-6.30.223.248/src/include/bcmutils.h:#define bcm_strcpy_s(dst, noOfElements, src) strcpy((dst), (src))
The strcpy_s() and strcat_s() functions are defined in ISO/IEC TR 24731 as a close replacement for strcpy() and strcat(). These functions have an additional argument that specifies the maximum size of the destination and also include a return value that indicates whether the operation was successful.
The semantics of strcpy_s() are similar to the semantics of strcpy() . When there are no input validation errors, the strcpy_s() function copies characters from a source string to a destination character array up to and including the terminating null character. The function returns 0 on success.
strcpy depends on a trailing '\0' that also makes it unsafe. Note: The strncpy function is a safer version of strcpy to copy a string from a source to a destination buffer.
Why does strcpy_s not exist anywhere on my system?
strcpy_s
is not provided by the C++ standard library.
strcpy_s
is however, specified by the C standard standard since C11. But even in C11, strcpy_s
is optional. All standard library implementations do not provide all optional features. So, the answer seems to be: Because none of the C standard libraries you have installed declare it.
How to I get strcpy_s, strtok_s, and all the other safe string functions onto my system?
You will need to either find a library that implements them all, or implement them yourself.
Apparently, these don't exist because they don't exist. And I can find and install them by finding and installing them; or, alternatively, by coding them myself. Apparently this is a helpful solution, so let the upvotes begin.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With