Skip to contentSkip to conversation

generic strncpy - off-by-one error

DISCUSSION Anthony Truong 2 participants 2 messages Aug 12, 2003

lore
Share & tools
lore
#0 lore raw

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

Quoted 6 lines · On Tue, 12 Aug 2003 [email protected] wrote:

Review

Quoted 2 lines · Yoshinori Sato
(quoted diff) +1 -1
-   while (count) {
+   while (count > 1) {

Given that count is a size_t, which seems to be derived from 'unsigned

int'

Quoted 1 line

or 'unsigned long' on every platform, how are these any different?

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

Signature · 35 lines
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.
#1 lore raw
Quoted 3 lines

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().

Wrong: http://www.opengroup.org/onlinepubs/007908799/xsh/strncpy.html

Bernd

Signature · 3 lines
Firmix Software GmbH                   http://www.firmix.at/
mobil: +43 664 4416156                 fax: +43 1 7890849-55
          Embedded Linux Development and Services