public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Randy Dunlap <rdunlap@infradead.org>
To: Rickard Strandqvist <rickard_strandqvist@spectrumdigital.se>
Cc: Chris Metcalf <cmetcalf@tilera.com>,
	"David S. Miller" <davem@davemloft.net>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v2] tile: avoid errors from truncating long strings in mpipe gxio
Date: Mon, 11 Aug 2014 14:30:06 -0700	[thread overview]
Message-ID: <53E935DE.6050201@infradead.org> (raw)
In-Reply-To: <CAFo99gYMtpVo9=dZ-dW5iiiRgKGjwxwaLBe6C5Ci9v6-7o8H+A@mail.gmail.com>

On 08/11/14 13:43, Rickard Strandqvist wrote:
> 2014-08-07 8:43 GMT+02:00 Randy Dunlap <rdunlap@infradead.org>:
>> On 08/06/14 11:16, Chris Metcalf wrote:
>>> Using strncpy() will just silently truncate long strings; we should
>>> instead return an appropriate error.  Using strlcpy() would suffer from
>>> the same problem.  Instead, use strnlen()+memcpy(), and add an
>>> error-checking step to make sure the lengths are reasonable.
>>>
>>> I called the convenience wrapper strscpy(), and a case could be made for
>>> making it more generic (possibly with a better name), but that seems
>>> outside the scope of this initial commit.
>>
>> Well, having looked at the function before I read this comment, my first
>> thought was that it should be added to lib/string.c for general
>> availability.
>>
>>>
>>> Signed-off-by: Chris Metcalf <cmetcalf@tilera.com>
>>> ---
>>> v2: use strnlen instead of strlen
>>>
>>>  arch/tile/gxio/mpipe.c | 30 +++++++++++++++++++++++++-----
>>>  1 file changed, 25 insertions(+), 5 deletions(-)
>>>
>>> diff --git a/arch/tile/gxio/mpipe.c b/arch/tile/gxio/mpipe.c
>>> index 5301a9ffbae1..27a56be8d583 100644
>>> --- a/arch/tile/gxio/mpipe.c
>>> +++ b/arch/tile/gxio/mpipe.c
>>> @@ -29,6 +29,25 @@
>>>  /* HACK: Avoid pointless "shadow" warnings. */
>>>  #define link link_shadow
>>>
>>> +/*
>>> + * Use this routine to avoid copying too-long strings.  Unlike strncpy
>>> + * or strlcpy, we don't enable programmers who don't check return codes;
>>> + * partially-copied strings can be problematic.  The routine returns
>>> + * the total number of bytes copied (including the trailing NUL) or
>>> + * zero if the buffer wasn't big enough.
>>> + */
>>> +static size_t strscpy(char *dest, const char *src, size_t size)
>>> +{
>>> +     size_t ret = strnlen(src, size) + 1;
>>> +     if (ret > size) {
>>> +             if (size)
>>> +                     dest[0] = '\0';
>>> +             return 0;
>>> +     }
>>> +     memcpy(dest, src, ret);
>>> +     return ret;
>>> +}
>>
>> --
>> ~Randy
> 
> 
> Hi
> 
> Sounds like a great idea!
> 
> Do it Chris!  Randy?

Yeah, I don't have a better name for it, but I do think
that it's the right thing to do.

Thanks,

-- 
~Randy

  reply	other threads:[~2014-08-11 21:30 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-26 14:03 [PATCH] arch: tile: gxio: mpipe.c: Cleaning up missing null-terminate in conjunction with strncpy Rickard Strandqvist
2014-08-05 20:24 ` Chris Metcalf
2014-08-05 21:25   ` Rickard Strandqvist
2014-08-06 18:16     ` [PATCH v2] tile: avoid errors from truncating long strings in mpipe gxio Chris Metcalf
2014-08-06 21:14       ` Rickard Strandqvist
2014-08-07  6:43       ` Randy Dunlap
2014-08-07 16:01         ` new generic strscpy API? (was Re: [PATCH v2] tile: avoid errors from truncating long strings in mpipe gxio) Chris Metcalf
2014-08-11 20:43         ` [PATCH v2] tile: avoid errors from truncating long strings in mpipe gxio Rickard Strandqvist
2014-08-11 21:30           ` Randy Dunlap [this message]
2014-08-29 18:31             ` [PATCH 1/2] Add strscpy() as a new string copy primitive Chris Metcalf
2014-08-29 18:34             ` [PATCH 2/2] tile gxio mpipe: use the new strscpy() primitive Chris Metcalf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=53E935DE.6050201@infradead.org \
    --to=rdunlap@infradead.org \
    --cc=cmetcalf@tilera.com \
    --cc=davem@davemloft.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rickard_strandqvist@spectrumdigital.se \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox