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: unassig...@gcc.gnu.org
        ReportedBy: ivan.tub...@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.

Reply via email to