]> git.proxmox.com Git - mirror_edk2.git/blobdiff - StdLib/LibC/String/strncasecmp.c
StdLib: Fix pointer arithmetic issues in the strncasecmp function.
[mirror_edk2.git] / StdLib / LibC / String / strncasecmp.c
index b3f6d0596555580562b6dda143b3c33620ba99ba..9cc1851ee2021505ddef3ca81b8cc68e1dec7af9 100644 (file)
@@ -1,4 +1,4 @@
-/** @file \r
+/** @file\r
   strncasecmp implementation\r
 \r
   Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r
@@ -11,7 +11,7 @@
   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 \r
  * Copyright (c) 1987, 1993\r
- *     The Regents of the University of California.  All rights reserved.\r
+ *  The Regents of the University of California.  All rights reserved.\r
  *\r
  * Redistribution and use in source and binary forms, with or without\r
  * modification, are permitted provided that the following conditions\r
@@ -37,8 +37,8 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r
  * SUCH DAMAGE.\r
 \r
-       $NetBSD: strncasecmp.c,v 1.2 2007/06/04 18:19:27 christos Exp $\r
-  strcasecmp.c 8.1 (Berkeley) 6/4/93\r
+  $NetBSD: strncasecmp.c,v 1.2 2007/06/04 18:19:27 christos Exp $\r
+  strcasecmp.c  8.1 (Berkeley) 6/4/93\r
 **/\r
 #include <LibConfig.h>\r
 #include <sys/cdefs.h>\r
@@ -55,25 +55,28 @@ __weak_alias(strncasecmp,_strncasecmp)
 #else\r
 #include <lib/libkern/libkern.h>\r
 #include <machine/limits.h>\r
-#endif \r
+#endif\r
 \r
 int\r
 strncasecmp(const char *s1, const char *s2, size_t n)\r
 {\r
+  int   CompareVal;\r
 \r
-       _DIAGASSERT(s1 != NULL);\r
-       _DIAGASSERT(s2 != NULL);\r
-\r
-       if (n != 0) {\r
-               const unsigned char *us1 = (const unsigned char *)s1,\r
-                               *us2 = (const unsigned char *)s2;\r
+  _DIAGASSERT(s1 != NULL);\r
+  _DIAGASSERT(s2 != NULL);\r
 \r
-               do {\r
-                       if (tolower(*us1) != tolower(*us2++))\r
-                               return (tolower(*us1) - tolower(*--us2));\r
-                       if (*us1++ == '\0')\r
-                               break;\r
-               } while (--n != 0);\r
-       }\r
-       return (0);\r
+  if (n != 0) {\r
+    do {\r
+      CompareVal = tolower(*s1) - tolower(*s2);\r
+      if (CompareVal != 0) {\r
+        return (CompareVal);\r
+      }\r
+      ++s1;\r
+      ++s2;\r
+      if (*s1 == '\0') {\r
+        break;\r
+      }\r
+    } while (--n != 0);\r
+  }\r
+  return (0);\r
 }\r