@LiveForIt
Quote:
well strlen() is not used to write strings into buffer ... memcpy is not meant for string operating in general, as the name suggest meant for copy memory.
True, it would be silly to use strlen() and then memcpy() when strcpy() would do the same thing more efficiently. But Flawfinder still considers strlen() a potential security flaw, so I was trying to give a simple example to demonstrate why. A little too simple, perhaps.
Quote:
Strncpy should be used instead, like sprintf is unsafe, while snprintf is safe.
Unfortunately, the standard C library does not have a good length-limited string copy. strncpy() sounds like it is, but if the source string is longer than the specified length then the destination string is not NUL-terminated. That keeps the destination buffer from overflowing, but results in an unterminated string, which rightly earns strncpy() a warning from Flawfinder.
Depending on how portable you want your code to be, there are non-standard alternatives such as strnlen() and strlcpy(). Both newlib and clib2 have these, but other C libraries may not.