cb589a
commit 5fdabb72fdcba6bcf788eaa19c1ee557c13b8a7a
cb589a
Author: Mark Wielaard <mark@klomp.org>
cb589a
Date:   Sat Dec 1 23:54:40 2018 +0100
cb589a
cb589a
    Bug 401627 - Add wcsncmp override and testcase.
cb589a
    
cb589a
    glibc 2.28 added an avx2 optimized variant of wstrncmp which memcheck
cb589a
    cannot proof correct. Add a simple override in vg_replace_strmem.c.
cb589a
cb589a
diff --git a/memcheck/tests/wcs.c b/memcheck/tests/wcs.c
cb589a
index 15730ad..538304b 100644
cb589a
--- a/memcheck/tests/wcs.c
cb589a
+++ b/memcheck/tests/wcs.c
cb589a
@@ -1,5 +1,6 @@
cb589a
-// Uses various wchar_t * functions that have hand written SSE assembly
cb589a
-// implementations in glibc. wcslen, wcscpy, wcscmp, wcsrchr, wcschr.
cb589a
+// Uses various wchar_t * functions that have hand written SSE and/or AVX2
cb589a
+// assembly implementations in glibc.
cb589a
+// wcslen, wcscpy, wcscmp, wcsncmp, wcsrchr, wcschr.
cb589a
 
cb589a
 #include <stdio.h>
cb589a
 #include <stdlib.h>
cb589a
@@ -18,6 +19,8 @@ int main(int argc, char **argv)
cb589a
   c = wcscpy (b, a);
cb589a
 
cb589a
   fprintf (stderr, "wcscmp equal: %d\n", wcscmp (a, b)); // wcscmp equal: 0
cb589a
+  fprintf (stderr,
cb589a
+	   "wcsncmp equal: %d\n", wcsncmp (a, b, l)); // wcsncmp equal: 0
cb589a
 
cb589a
   d = wcsrchr (a, L'd');
cb589a
   e = wcschr (a, L'd');
cb589a
diff --git a/memcheck/tests/wcs.stderr.exp b/memcheck/tests/wcs.stderr.exp
cb589a
index 41d74c8..d5b5959 100644
cb589a
--- a/memcheck/tests/wcs.stderr.exp
cb589a
+++ b/memcheck/tests/wcs.stderr.exp
cb589a
@@ -1,3 +1,4 @@
cb589a
 wcslen: 53
cb589a
 wcscmp equal: 0
cb589a
+wcsncmp equal: 0
cb589a
 wcsrchr == wcschr: 1
cb589a
diff --git a/shared/vg_replace_strmem.c b/shared/vg_replace_strmem.c
cb589a
index d6927f0..89a7dcc 100644
cb589a
--- a/shared/vg_replace_strmem.c
cb589a
+++ b/shared/vg_replace_strmem.c
cb589a
@@ -103,6 +103,7 @@
cb589a
    20420 STPNCPY
cb589a
    20430 WMEMCHR
cb589a
    20440 WCSNLEN
cb589a
+   20450 WSTRNCMP
cb589a
 */
cb589a
 
cb589a
 #if defined(VGO_solaris)
cb589a
@@ -1927,6 +1928,36 @@ static inline void my_exit ( int x )
cb589a
  WCSCMP(VG_Z_LIBC_SONAME,          wcscmp)
cb589a
 #endif
cb589a
 
cb589a
+/*---------------------- wcsncmp ----------------------*/
cb589a
+
cb589a
+// This is a wchar_t equivalent to strncmp.  We don't
cb589a
+// have wchar_t available here, but in the GNU C Library
cb589a
+// wchar_t is always 32 bits wide and wcsncmp uses signed
cb589a
+// comparison, not unsigned as in strncmp function.
cb589a
+
cb589a
+#define WCSNCMP(soname, fnname) \
cb589a
+   int VG_REPLACE_FUNCTION_EZU(20450,soname,fnname) \
cb589a
+          ( const Int* s1, const Int* s2, SizeT nmax ); \
cb589a
+   int VG_REPLACE_FUNCTION_EZU(20450,soname,fnname) \
cb589a
+          ( const Int* s1, const Int* s2, SizeT nmax ) \
cb589a
+   { \
cb589a
+      SizeT n = 0; \
cb589a
+      while (True) { \
cb589a
+         if (n >= nmax) return 0; \
cb589a
+         if (*s1 == 0 && *s2 == 0) return 0; \
cb589a
+         if (*s1 == 0) return -1; \
cb589a
+         if (*s2 == 0) return 1; \
cb589a
+         \
cb589a
+         if (*s1 < *s2) return -1; \
cb589a
+         if (*s1 > *s2) return 1; \
cb589a
+         \
cb589a
+         s1++; s2++; n++; \
cb589a
+      } \
cb589a
+   }
cb589a
+#if defined(VGO_linux)
cb589a
+ WCSNCMP(VG_Z_LIBC_SONAME,          wcsncmp)
cb589a
+#endif
cb589a
+
cb589a
 /*---------------------- wcscpy ----------------------*/
cb589a
 
cb589a
 // This is a wchar_t equivalent to strcpy.  We don't