4 ms·
strcpy_s does this. It has a template variant to infer the length of the destination buffer and it will terminate the program if the source string is too large.
by brucedawson 8y ago
strcpy_s does this. It has a template variant to infer the length of the destination buffer and it will terminate the program if the source string is too large.
If the destination buffer doesn't have a fixed length then it will fail to compile.
This is similar to your suggestion with these improvements:
- Template deduction of the destination size is done - this is essential to avoid bugs
- Detection of overwrites is done in release builds as well as during testing
- Detection of overwrites is automatic - it doesn't require manually adding asserts to call sites
- strcpy_s exists in some standard libraries