LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Roel Kluin <12o3l@tiscali.nl>
To: "H. Peter Anvin" <hpa@zytor.com>
Cc: elf@buici.com,
	Linux-arm <linux-arm-kernel@lists.arm.linux.org.uk>,
	lkml <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] locomo.c: convert strncpy(x, y, sizeof(x)) to strlcpy
Date: Fri, 07 Mar 2008 00:35:56 +0100	[thread overview]
Message-ID: <47D07FDC.2010701@tiscali.nl> (raw)
In-Reply-To: <47D065DB.7070708@zytor.com>

H. Peter Anvin wrote:
> Roel Kluin wrote:
>> This patch was not yet tested. Please confirm it's right.
>> ---
>> strncpy does not append '\0' if the length of the source string equals
>> the size parameter, strlcpy does.
>>
> 
> Are you sure it's safe to not zero out the contents of the buffer (no
> information leak)?
> 
>     -hpa

As I understand it, please correct me if I'm wrong:

Of the three variants: strcpy, strncpy and strlcpy.
- strcpy does not append \0 (unless the source string already contained it)
- strncpy appends \0's if the source string is smaller than the size
  parameter (for all remaining characters)
- strlcpy always appends a single \0 (unless size parameter was 0)

char *strcpy(char *dest, const char *src);
char *strncpy(char *dest, const char *src, size_t n);
size_t strlcpy(char *dst, const char *src, size_t n);

In the original code strncpy was used and the size parameter was equal
to the source string size:

strncpy(dev->dev.bus_id, info->name, sizeof(dev->dev.bus_id));

Since this the size was equal there was no \0 termination. To \0
terminate using strncpy we could write:

strncpy(dev->dev.bus_id, info->name, sizeof(dev->dev.bus_id) - 1);
dev->dev.bus_id[sizeof(dev->dev.bus_id) - 1] = '\0';

or using strlcpy, which does the same thing:

strlcpy(dev->dev.bus_id, info->name, sizeof(dev->dev.bus_id));

Roel

  reply	other threads:[~2008-03-06 23:36 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-03-06 13:08 Roel Kluin
2008-03-06 21:44 ` H. Peter Anvin
2008-03-06 23:35   ` Roel Kluin [this message]
2008-03-07  1:51     ` David Wagner

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=47D07FDC.2010701@tiscali.nl \
    --to=12o3l@tiscali.nl \
    --cc=elf@buici.com \
    --cc=hpa@zytor.com \
    --cc=linux-arm-kernel@lists.arm.linux.org.uk \
    --cc=linux-kernel@vger.kernel.org \
    --subject='Re: [PATCH] locomo.c: convert strncpy(x, y, sizeof(x)) to strlcpy' \
    /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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).