On 5/6/2015 11:01 AM, Dan Carpenter wrote:
On Thu, Apr 30, 2015 at 12:01:16PM -0400, Chris Metcalf wrote:
>+ssize_t strscpy(char *dest, const char *src, size_t count)
>+{
>+ ssize_t res = strscpy_truncate(dest, src, count);
>+ if (res < 0 && count != 0)
>+ dest[0] = '\0';
How is this better than returning a truncated string? Is it just
because the caller was naughty so we give them a spanking?
There are basically two issues here:
1. A truncated string with an error return may still cause program errors,
even if the caller checks for the error return, if the buffer is later interpreted
as a valid string due to some other program error. It's defensive programming.
2. Programmers are fond of ignoring error returns. My experience with
truncated strings is that in too many cases, truncation causes program
errors down the line. It's better to ensure that no partial string is returned
in this case.
In a perfect world, all error returns would be checked, and there would be
no need for this, but we definitely don't live in that world :-)
That said, although I think my approach is correct, I'm open to a consensus
that having strscpy() leave a truncated string in the dest buffer is better.
--
Chris Metcalf, EZChip Semiconductor
http://www.ezchip.com
--
To unsubscribe from this list: send the line "unsubscribe linux-arch" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html