[lkml]   [2003]   [Aug]   [12]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
Messages in this thread
SubjectRe: generic strncpy - off-by-one error
On Tue, 2003-08-12 at 23:54, William Gallafent wrote:

On Tue, 12 Aug 2003 wrote:

> On Tue, 12 Aug 2003 23:50:06 +0900, Yoshinori Sato
> <> said:
> > - while (count) {
> > + while (count > 1) {
> Given that count is a size_t, which seems to be derived from 'unsigned
> or 'unsigned long' on every platform, how are these any different?

Er, consider the case of count == 1. Fenceposts can be dangerous things.

Bill Gallafent.

This is the code I got from 2.4.20:
char * strncpy(char * dest,const char *src,size_t count)
char *tmp = dest;

while (count-- && (*dest++ = *src++) != '\0')
/* nothing */;

return tmp;

I don't see any problem with this code, and if we don't need to NULL-pad
the dest string, we do not have to. It is not in the definition of
strncpy(). So we don't need the second while {};
I'm hoping we're looking at the same thing.

Anthony Dominic Truong.

Disclaimer: The information contained in this transmission, including any
attachments, may contain confidential information of Matsushita Avionics
Systems Corporation. This transmission is intended only for the use of the
addressee(s) listed above. Unauthorized review, dissemination or other use
of the information contained in this transmission is strictly prohibited.
If you have received this transmission in error or have reason to believe
you are not authorized to receive it, please notify the sender by return
email and promptly delete the transmission.

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to
More majordomo info at
Please read the FAQ at

 \ /
  Last update: 2005-03-22 13:47    [W:0.038 / U:0.324 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site