[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <554A3161.3030609@ezchip.com>
Date: Wed, 6 May 2015 11:21:05 -0400
From: Chris Metcalf <cmetcalf@...hip.com>
To: Dan Carpenter <dan.carpenter@...cle.com>
CC: Linus Torvalds <torvalds@...ux-foundation.org>,
Al Viro <viro@...iv.linux.org.uk>,
Fabian Frederick <fabf@...net.be>,
Randy Dunlap <rdunlap@...radead.org>,
Rickard Strandqvist <rickard_strandqvist@...ctrumdigital.se>,
<gregkh@...uxfoundation.org>,
Peter Zijlstra <peterz@...radead.org>,
"David S. Miller" <davem@...emloft.net>,
Frederic Weisbecker <fweisbec@...il.com>,
Andrew Morton <akpm@...ux-foundation.org>,
Sam Ravnborg <sam@...nborg.org>,
Stephen Rothwell <sfr@...b.auug.org.au>,
Theodore Ts'o <tytso@....edu>,
Grant Likely <grant.likely@...aro.org>,
Linux Kernel Mailing List <linux-kernel@...r.kernel.org>,
<linux-arch@...r.kernel.org>
Subject: Re: [PATCH 2/3] string: provide strscpy() and strscpy_truncate()
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-kernel" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Powered by blists - more mailing lists