]> git.proxmox.com Git - mirror_edk2.git/commitdiff
StdLib: Fix pointer arithmetic issues in the strncasecmp function.
authorDaryl McDaniel <daryl.mcdaniel@intel.com>
Fri, 13 Sep 2013 00:46:19 +0000 (00:46 +0000)
committerdarylm503 <darylm503@6f19259b-4bc3-4df7-8a09-765794883524>
Fri, 13 Sep 2013 00:46:19 +0000 (00:46 +0000)
The original Linux code tried to be too fancy so the internal pointers got out of sync.
Rewrote the function to at least be more clear.
Regardless, it now works properly.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Daryl McDaniel <daryl.mcdaniel@intel.com>
Reviewed by: matthew.stanbro@intel.com
Reviewed by: erik.c.bjorge@intel.com

git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@14664 6f19259b-4bc3-4df7-8a09-765794883524

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