3 ms·
You should read the article. The issue is not that strncpy is not safer, the issue is that strncpy is not a string operation, but a memory operation
by dnautics 10y ago
You should read the article. The issue is not that strncpy is not safer, the issue is that strncpy is not a string operation, but a memory operation
- mjevans 10y agoOnly it isn't. strncpy will /terminate/ at the end of the string if it's shorter than the allocated buffer. If, somehow, the source string is invalid it will then terminate at the specified cutoff point. However one should argue that if it has overflowed bounds, that if the source isn't a valid string, you've already violated the contract of a secure environment and thus something has gone wrong and should be handled at a level higher than string copy preferences. Use of strncpy, however, assumes that the source string /is/ valid, but might overflow the target buffer. In /that/ case truncation may be desired and if not the program flow reacting accordingly is probably desired. (This is C, you don't /throw/ errors, you write explicit paths.)