[Bug other/50277] New: strndup should use strnlen instead of strlen
ivan.tubert at gmail dot com
gcc-bugzilla@gcc.gnu.org
Fri Sep 2 16:40:00 GMT 2011
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50277
Bug #: 50277
Summary: strndup should use strnlen instead of strlen
Classification: Unclassified
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: other
AssignedTo: unassigned@gcc.gnu.org
ReportedBy: ivan.tubert@gmail.com
Regarding strndup (const char *s, size_t n):
strndup is using strlen to get the length of s, in order to decide whether to
allocate n+1 characters or strlen(s)+1.
This is inefficient. Imagine that s is one googol characters long, and I call
strndup(s,3) because I want a duplicate of up to the first three characters.
Yet strlen will go all the way to the end of s, just to find that it is longer
than 3 characters!
I suggest using strnlen instead.
More information about the Gcc-bugs
mailing list