public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug other/50277] New: strndup should use strnlen instead of strlen
@ 2011-09-02 16:40 ivan.tubert at gmail dot com
  2011-09-02 16:45 ` [Bug other/50277] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: ivan.tubert at gmail dot com @ 2011-09-02 16:40 UTC (permalink / raw)
  To: gcc-bugs

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.


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2011-09-02 16:59 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-09-02 16:40 [Bug other/50277] New: strndup should use strnlen instead of strlen ivan.tubert at gmail dot com
2011-09-02 16:45 ` [Bug other/50277] " pinskia at gcc dot gnu.org
2011-09-02 16:52 ` ivan.tubert at gmail dot com
2011-09-02 16:55 ` ivan.tubert at gmail dot com
2011-09-02 16:59 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).