Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does strcpy_s not exist anywhere on my system?

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))
like image 200
Sod Almighty Avatar asked Apr 19 '16 16:04

Sod Almighty


People also ask

Where is strcpy_s defined?

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.

How does strcpy_s work in C++?

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.

What is the safe version of the strcpy () function in C?

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.


2 Answers

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.

like image 89
eerorika Avatar answered Oct 03 '22 18:10

eerorika


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.

like image 39
Sod Almighty Avatar answered Oct 03 '22 18:10

Sod Almighty