commit 3eea668052db201fe3d0453324a6b909e3568d38
Author: Oswald Buddenhagen <o...@users.sf.net>
Date:   Thu Dec 29 15:08:26 2016 +0100

    make use of memchr() in strnlen() replacement after all
    
    turns out the comment advising against it was bogus - unlike for
    memcmp(), the standard does indeed prescribe that the memchr()
    implementation may not read past the first occurrence of the searched
    char.

 src/util.c |    7 ++-----
 1 files changed, 2 insertions(+), 5 deletions(-)

diff --git a/src/util.c b/src/util.c
index c226c1b..ae4d6b6 100644
--- a/src/util.c
+++ b/src/util.c
@@ -225,11 +225,8 @@ memrchr( const void *s, int c, size_t n )
 size_t
 strnlen( const char *str, size_t maxlen )
 {
-       size_t len;
-
-       /* It's tempting to use memchr(), but it's allowed to read past the end 
of the actual string. */
-       for (len = 0; len < maxlen && str[len]; len++) {}
-       return len;
+       const char *estr = memchr( str, 0, maxlen );
+       return estr ? (size_t)(estr - str) : maxlen;
 }
 
 #endif

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most 
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
isync-devel mailing list
isync-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/isync-devel

Reply via email to