Skip to content

Commit

Permalink
Fix word hash collision and function parameter type mismatches
Browse files Browse the repository at this point in the history
- Fixes the word comparison problem from http://issue.cc/r3/2113
- Clean up REBINT vs REBCNT type mismatches in UTF decoding.
  • Loading branch information
BrianHawley committed Feb 27, 2014
1 parent 484526b commit 7e92920
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/core/s-crc.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,17 @@ static REBCNT *CRC_Table;
{
REBINT m, n;
REBINT hash;
REBCNT ulen;

if (len < 0) len = LEN_BYTES(str);

hash = (REBINT)len + (REBINT)((REBYTE)LO_CASE(*str));

for (; len > 0; str++, len--) {
ulen = (REBCNT)len; // so the & operation later isn't for the wrong type

for (; ulen > 0; str++, ulen--) {
n = *str;
if (n > 127 && NZ(m = Decode_UTF8_Char(&str, &len))) n = m; // mods str, len
if (n > 127 && NZ(m = Decode_UTF8_Char(&str, &ulen))) n = m; // mods str, ulen
if (n < UNICODE_CASES) n = LO_CASE(n);
n = (REBYTE)((hash >> CRCSHIFTS) ^ (REBYTE)n); // drop upper 8 bits
hash = MASK_CRC(hash << 8) ^ (REBINT)CRC_Table[n];
Expand Down
5 changes: 3 additions & 2 deletions src/core/s-find.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@
** -3: no match, s2 > s1
** -1: no match, s1 > s2
** 0: exact match
** 1: non-case match, s2 > s1
** 1: non-case match, s2 > s1
** 3: non-case match, s1 > s2
**
** So, result + 2 for no-match gives proper sort order.
Expand All @@ -271,7 +271,7 @@
REBCNT l1 = LEN_BYTES(s1);
REBINT result = 0;

for (; l2 > 0; s1++, s2++, l2--) {
for (; l1 > 0 && l2 > 0; s1++, s2++, l1--, l2--) {
c1 = (REBYTE)*s1;
c2 = (REBYTE)*s2;
if (c1 > 127) c1 = Decode_UTF8_Char(&s1, &l1); //!!! can return 0 on error!
Expand All @@ -284,6 +284,7 @@
if (!result) result = (c1 > c2) ? 3 : 1;
}
}
if (l1 != l2) result = (l1 > l2) ? -1 : -3;

return result;
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/s-unicode.c
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ ConversionResult ConvertUTF8toUTF32 (

/***********************************************************************
**
*/ REBCNT Decode_UTF8_Char(REBYTE **str, REBINT *len)
*/ REBCNT Decode_UTF8_Char(REBYTE **str, REBCNT *len)
/*
** Converts a single UTF8 code-point (to 32 bit).
** Errors are returned as zero. (So prescan source for null.)
Expand Down Expand Up @@ -817,7 +817,7 @@ ConversionResult ConvertUTF8toUTF32 (

/***********************************************************************
**
*/ int Decode_UTF8(REBUNI *dst, REBYTE *src, REBINT len, REBFLG ccr)
*/ int Decode_UTF8(REBUNI *dst, REBYTE *src, REBCNT len, REBFLG ccr)
/*
** Decode UTF8 byte string into a 16 bit preallocated array.
**
Expand Down Expand Up @@ -853,7 +853,7 @@ ConversionResult ConvertUTF8toUTF32 (

/***********************************************************************
**
*/ int Decode_UTF16(REBUNI *dst, REBYTE *src, REBINT len, REBFLG lee, REBFLG ccr)
*/ int Decode_UTF16(REBUNI *dst, REBYTE *src, REBCNT len, REBFLG lee, REBFLG ccr)
/*
** dst: the desination array, must always be large enough!
** src: source binary data
Expand Down

0 comments on commit 7e92920

Please sign in to comment.