Changeset 3762


Ignore:
Timestamp:
Mar 15, 2012, 2:40:59 AM (13 years ago)
Author:
bird
Message:

wcsncmp,wcscmp: don't assume that wchar_t is the same as int. Would cause two extra bytes to be compared on mismatch. Fixes #219.

Location:
trunk/libc/src/fbsdlibc/string
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/libc/src/fbsdlibc/string/wcscmp.c

    r1748 r3762  
    5454        const wchar_t *s1, *s2;
    5555{
    56 
     56#if 0 /* bird: Original FreeBSD code */
    5757        while (*s1 == *s2++)
    5858                if (*s1++ == 0)
     
    6060        /* XXX assumes wchar_t = int */
    6161        return (*(const unsigned int *)s1 - *(const unsigned int *)--s2);
     62#else /* bird: Should be safe and correct for all wchar_t types. */
     63        wchar_t wc1;
     64        while ((wc1 = *s1) == *s2)
     65        {
     66                if (!wc1)
     67                        return 0;
     68                s1++;
     69                s2++;
     70        }
     71        return wc1 < *s2 ? -1 : 1;
     72#endif
    6273}
  • trunk/libc/src/fbsdlibc/string/wcsncmp.c

    r1748 r3762  
    4949        size_t n;
    5050{
    51 
     51#if 0 /* Original FreeBSD code. */
    5252        if (n == 0)
    5353                return (0);
     
    6161                        break;
    6262        } while (--n != 0);
     63#else /* bird: Should be safe and correct for all wchar_t types. */
     64        while (n > 0)
     65        {
     66                wchar_t wc1 = *s1;
     67                if (wc1 != *s2)
     68                        return wc1 < *s2 ? -1 : 1;
     69                if (!wc1)
     70                        break;
     71                s2++;
     72                s1++;
     73                n--;
     74        }
     75#endif
    6376        return (0);
    6477}
Note: See TracChangeset for help on using the changeset viewer.