Re: generic strncpy - off-by-one error

Anthony Truong
2003-08-12T16:17:03Z

On Tue, 2003-08-12 at 23:54, William Gallafent wrote:

On Tue, 12 Aug 2003 Valdis.Kletnieks@vt.edu wrote:

> On Tue, 12 Aug 2003 23:50:06 +0900, Yoshinori Sato
> <ysato@users.sourceforge.jp> said:
> > - while (count) {
> > + while (count > 1) {
>
> Given that count is a size_t, which seems to be derived from 'unsigned
int'
> 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.
-

Hello,
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.

Regards,
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.