The Microsoft C Runtime Library introduced a set of security-enhanced functions in Visual Studio 2005, all bearing theDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/MicrosoftDocs/cpp-docs/llms.txt
Use this file to discover all available pages before exploring further.
_s suffix. These functions address well-known vulnerabilities in classic C string and I/O APIs — chiefly buffer overruns, NULL pointer dereferences, and malformed format strings. Understanding how they work, and how to migrate existing code to use them, is essential for writing safe and audit-compliant C and C++ applications with MSVC.
Why Classic CRT Functions Are Unsafe
Many foundational CRT functions were designed without buffer-size parameters, making it impossible for the runtime to detect overflows. When a source string is larger than the destination buffer, the function silently corrupts adjacent memory._s functions were designed to eliminate them.
How Security-Enhanced Functions Work
Secure functions take an explicit buffer-size parameter and perform three categories of validation before writing:NULL Pointer Check
If any required pointer is
NULL, the function immediately invokes the invalid parameter handler rather than dereferencing it.Buffer Size Check
The function computes whether the requested operation would exceed the declared buffer size. If it would, it sets
errno to ERANGE or EINVAL and invokes the invalid parameter handler.Before and After: Migrating to Safe Functions
- String Copy
- String Concatenation
- Formatted Output
- Tokenization
Common Secure Function Pairs
| Unsafe Function | Secure Replacement | Key Change |
|---|---|---|
strcpy | strcpy_s | Adds destSize parameter |
strcat | strcat_s | Adds destSize parameter |
sprintf | sprintf_s | Adds destSize parameter |
vsprintf | vsprintf_s | Adds destSize parameter |
sscanf | sscanf_s | Adds bufSize for %s/%c |
gets | gets_s | Adds bufSize parameter |
strtok | strtok_s | Adds context pointer (thread-safe) |
localtime | localtime_s | Output buffer passed by caller |
gmtime | gmtime_s | Output buffer passed by caller |
fopen | fopen_s | Returns errno_t, takes FILE** |
tmpnam | tmpnam_s | Adds buffer size parameter |
memcpy | memcpy_s | Adds destSize parameter |
memmove | memmove_s | Adds destSize parameter |
The Invalid Parameter Handler
When a secure function detects an invalid parameter (NULL pointer, buffer too small, invalid format), it does not simply return an error code — it calls the invalid parameter handler. By default, this handler terminates the process. You can install a custom handler using_set_invalid_parameter_handler.
Why You Should NOT Use _CRT_SECURE_NO_WARNINGS
When you first encounter deprecation warnings like C4996: 'strcpy': This function or variable may be unsafe, it is tempting to silence them with a preprocessor definition:
The correct approach is to migrate to
_s variants. In C++ codebases, defining _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES as 1 activates secure template overloads that automatically redirect calls to unsafe functions to their safe equivalents, eliminating many warnings without manual code changes.Debug Fill Behavior
In debug builds, security-enhanced CRT functions fill the portion of the output buffer that was not written with the value0xFE. This helps detect cases where the caller passed an incorrect buffer size.
_CrtSetDebugFillThreshold: