...
Code Block |
---|
#define A_SIZE 16 char *string_data; char a[A_SIZE]; ... if (string_data) { if (strlen(string_data) < sizeof(a)) { strcpy(a, string_data); } else { /* handle string too large condition */ } } else { /* handle null string condition */ } |
Compliant Solution 3
Example using The strncpy_s()
The strncpy_s function copies not more than a maximum number n
of successive characters (characters that
follow a null character are not copied) from the source array pointed to by s2 to the array
pointed to by s1to a destination array. If no null character was copied from s2the source array, then s1n the nth position in the destination array is set to a null
character.character, guaranteeing that the resulting string is null-terminated.
This compliant solution also guarantees that the string is null-terminated.
Code Block |
---|
Exception
An exception to this rule applies if the intent of the programmer was to convert a null-terminated byte string to a character array. To be compliant with this standard, this intent must be made clear statement in comments.
...