tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

strndup reimplementing strnlen



I noticed that the beginning of strndup effectively reimplements strnlen. Is there a good reason for doing this? Wouldn’t it be preferable to just call strnlen directly, rather than implement it as a for loop?

Below is a patch that converts the for loop into a call to strnlen.

Index: lib/libc/string/strndup.c
===================================================================
RCS file: /cvsroot/src/lib/libc/string/strndup.c,v
retrieving revision 1.4 
diff -u -r1.4 strndup.c
--- lib/libc/string/strndup.c   3 Jul 2007 12:11:09 -0000   1.4
+++ lib/libc/string/strndup.c   15 Oct 2017 19:03:45 -0000
@@ -62,8 +62,7 @@
 
    _DIAGASSERT(str != NULL);
 
-   for (len = 0; len < n && str[len]; len++)
-       continue;
+   len = strnlen(str, n);
 
    if (!(copy = malloc(len + 1)))
        return (NULL);



Home | Main Index | Thread Index | Old Index