Skip to contentSkip to conversation

generic strncpy - off-by-one error

DISCUSSION Anthony Truong 3 participants 3 messages Aug 12, 2003

lore
Share & tools
lore
#0 lore raw
Quoted 3 lines · Bernd Petrovitsch

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

Hello,
Thanks for pointing that out to me. However, I don't think the kernel strncpy implementation is exactly the same as that of Standard C lib implementation. Iwas just looking at it from the kernel code context. There's a point in doing it the "kernel" way, to save precious CPU cycles from being wasted otherwise.

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 2 lines · Anthony Truong

Thanks for pointing that out to me. However, I don't think the kernel strncpy implementation is exactly the same as that of Standard C lib

It is true it doesnt need to be

Quoted 3 lines

implementation. Iwas just looking at it from the kernel code context. There's a point in doing it the "kernel" way, to save precious CPU cycles from being wasted otherwise.

CPU cycles, got lots of those 8). If its going to do anything it might be to reference an extra cache line. For people who dont need padding 2.6 has strlcpy. Lots of drivers assume strncpy fills the entire block

#2 lore raw
Quoted 5 lines · Alan Cox
Quoted 2 lines · Anthony Truong

Thanks for pointing that out to me. However, I don't think the kernel strncpy implementation is exactly the same as that of Standard C lib

It is true it doesnt need to be

Nevertheless it is defined the inefficient way by the C-Standard and lots of people actually really know this.
So IMHO (re)implementing a function called "strncpy" differently doesn't make much sense.

Quoted 1 line
Quoted 1 line

implementation. Iwas just looking at it from the kernel code context.

The kernel is implemented in C. And the function strncpy() _is_ already defined by C. So please use at least another name. Given the existence of strlcpy() - http://www.courtesan.com/todd/papers/strlcpy.html- this problem already has been solved.

Quoted 6 lines
Quoted 2 lines

There's a point in doing it the "kernel" way, to save precious CPU cycles from being wasted otherwise.

CPU cycles, got lots of those 8). If its going to do anything it might be to reference an extra cache line. For people who dont need padding 2.6 has strlcpy. Lots of drivers assume strncpy fills the entire block

ACK. To use strlcpy() is correct solution (if padding is not required).

Bernd