LWN.net Logo

"fast AND correct strncpy" not working by itself?

"fast AND correct strncpy" not working by itself?

Posted Aug 28, 2003 13:08 UTC (Thu) by IkeTo (subscriber, #2122)
In reply to: "fast AND correct strncpy" not working by itself? by akukula
Parent article: [PATCH] fast AND correct strncpy

> I'm not a kernel hacker but why all of those functions doesn't
> terminate 'dest' if strlen(src) > count ???

You don't need to be kernel hacker to understand that in POSIX strncpy is defined like that. In contrast, strncat does terminate the resulting string in such cases.


(Log in to post comments)

"fast AND correct strncpy" not working by itself?

Posted Aug 28, 2003 15:44 UTC (Thu) by dwheeler (subscriber, #1216) [Link]

Exactly right. strncpy _must_, by spec, \0 out the rest of the string. Which means that, in most code, you don't really want strncpy(); it wastes time zero'ing out bytes you probably don't care about.

If you want limited-length string operations, I suggest using the strlcpy() / strlcat() functions, which aren't in the official C standard but ARE widely available.

"fast AND correct strncpy" not working by itself?

Posted Aug 28, 2003 20:09 UTC (Thu) by akukula (guest, #3862) [Link]

Thanks for suggestions. Indeed, I found a table comparing performance of different 'str*cpy()'. Worth noting!
http://www.courtesan.com/todd/papers/strlcpy.html

> it wastes time zero'ing out bytes you probably don't care about.
Maybe someone then explain why the heck it is in kernel which I presume should use the most efficient algorithms?

Copyright © 2008, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds