From 3a2f8f421648cbf6282b66e28f69df0740b76467 Mon Sep 17 00:00:00 2001 From: Daryl McDaniel Date: Fri, 13 Sep 2013 00:46:19 +0000 Subject: [PATCH] StdLib: Fix pointer arithmetic issues in the strncasecmp function. 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 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 | 41 +++++++++++++++++--------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/StdLib/LibC/String/strncasecmp.c b/StdLib/LibC/String/strncasecmp.c index b3f6d05965..9cc1851ee2 100644 --- a/StdLib/LibC/String/strncasecmp.c +++ b/StdLib/LibC/String/strncasecmp.c @@ -1,4 +1,4 @@ -/** @file +/** @file strncasecmp implementation Copyright (c) 2011, Intel Corporation. All rights reserved.
@@ -11,7 +11,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. * Copyright (c) 1987, 1993 - * The Regents of the University of California. All rights reserved. + * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -37,8 +37,8 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. - $NetBSD: strncasecmp.c,v 1.2 2007/06/04 18:19:27 christos Exp $ - strcasecmp.c 8.1 (Berkeley) 6/4/93 + $NetBSD: strncasecmp.c,v 1.2 2007/06/04 18:19:27 christos Exp $ + strcasecmp.c 8.1 (Berkeley) 6/4/93 **/ #include #include @@ -55,25 +55,28 @@ __weak_alias(strncasecmp,_strncasecmp) #else #include #include -#endif +#endif int strncasecmp(const char *s1, const char *s2, size_t n) { + int CompareVal; - _DIAGASSERT(s1 != NULL); - _DIAGASSERT(s2 != NULL); - - if (n != 0) { - const unsigned char *us1 = (const unsigned char *)s1, - *us2 = (const unsigned char *)s2; + _DIAGASSERT(s1 != NULL); + _DIAGASSERT(s2 != NULL); - do { - if (tolower(*us1) != tolower(*us2++)) - return (tolower(*us1) - tolower(*--us2)); - if (*us1++ == '\0') - break; - } while (--n != 0); - } - return (0); + if (n != 0) { + do { + CompareVal = tolower(*s1) - tolower(*s2); + if (CompareVal != 0) { + return (CompareVal); + } + ++s1; + ++s2; + if (*s1 == '\0') { + break; + } + } while (--n != 0); + } + return (0); } -- 2.39.2