[ovs-dev] [strings 1/3] string: Implement strnlen() if it is missing.

Ben Pfaff blp at nicira.com
Tue Feb 22 19:01:41 UTC 2011


---
 configure.ac               |    2 +-
 lib/{string.h => string.c} |   26 ++++++++++----------------
 lib/string.h               |    8 +++++++-
 3 files changed, 18 insertions(+), 18 deletions(-)
 copy lib/{string.h => string.c} (50%)

diff --git a/configure.ac b/configure.ac
index 28af2fe..47d0907 100644
--- a/configure.ac
+++ b/configure.ac
@@ -56,7 +56,7 @@ OVS_CHECK_IF_PACKET
 OVS_CHECK_STRTOK_R
 AC_CHECK_MEMBERS([struct stat.st_mtim.tv_nsec, struct stat.st_mtimensec],
   [], [], [[#include <sys/stat.h>]])
-AC_CHECK_FUNCS([mlockall strsignal getloadavg statvfs setmntent])
+AC_CHECK_FUNCS([mlockall strnlen strsignal getloadavg statvfs setmntent])
 AC_CHECK_HEADERS([mntent.h sys/statvfs.h])
 
 OVS_CHECK_PKIDIR
diff --git a/lib/string.h b/lib/string.c
similarity index 50%
copy from lib/string.h
copy to lib/string.c
index 779a9e6..f9ce788 100644
--- a/lib/string.h
+++ b/lib/string.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009 Nicira Networks.
+ * Copyright (c) 2009, 2011 Nicira Networks.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -14,21 +14,15 @@
  * limitations under the License.
  */
 
-#ifndef STRING_WRAPPER_H
-#define STRING_WRAPPER_H 1
+#include <config.h>
 
-#include_next <string.h>
+#include <string.h>
 
-/* Glibc 2.7 has a bug in strtok_r when compiling with optimization that can
- * cause segfaults if the delimiters argument is a compile-time constant that
- * has exactly 1 character:
- *
- *      http://sources.redhat.com/bugzilla/show_bug.cgi?id=5614
- *
- * The bug is only present in the inline version of strtok_r(), so force the
- * out-of-line version to be used instead. */
-#if HAVE_STRTOK_R_BUG
-#undef strtok_r
+#ifndef HAVE_STRNLEN
+size_t
+strnlen(const char *s, size_t maxlen)
+{
+    const char *end = memchr(s, '\0', maxlen);
+    return end ? end - s : maxlen;
+}
 #endif
-
-#endif /* string.h wrapper */
diff --git a/lib/string.h b/lib/string.h
index 779a9e6..5e372bb 100644
--- a/lib/string.h
+++ b/lib/string.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009 Nicira Networks.
+ * Copyright (c) 2009, 2011 Nicira Networks.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -31,4 +31,10 @@
 #undef strtok_r
 #endif
 
+#ifndef HAVE_STRNLEN
+#undef strnlen
+#define strnlen rpl_strnlen
+size_t strnlen(const char *, size_t maxlen);
+#endif
+
 #endif /* string.h wrapper */
-- 
1.7.1





More information about the dev mailing list