From: Yann Collet Date: Tue, 17 Apr 2018 22:29:17 +0000 (-0700) Subject: fix matchIndex overflow X-Git-Tag: upstream/1.9.3~8^2~33^2~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=152064218361e5762fd67b5de425707fdc47095b;p=platform%2Fupstream%2Flz4.git fix matchIndex overflow can happen with dictCtx --- diff --git a/lib/lz4.c b/lib/lz4.c index b426545..c799596 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -758,9 +758,9 @@ LZ4_FORCE_INLINE int LZ4_compress_generic( forwardH = LZ4_hashPosition(forwardIp, tableType); LZ4_putIndexOnHash(current, h, cctx->hashTable, tableType); - if ((dictIssue == dictSmall) && (matchIndex < prefixIdxLimit)) continue; /* match outside of valid area */ - if ((tableType != byU16) && (matchIndex+MAX_DISTANCE < current)) continue; /* too far */ - if (tableType == byU16) assert((current - matchIndex) <= MAX_DISTANCE); /* too_far presumed impossible with byU16 */ + if ((dictIssue == dictSmall) && (matchIndex < prefixIdxLimit)) continue; /* match outside of valid area */ + if ((tableType != byU16) && (current - matchIndex > MAX_DISTANCE)) continue; /* too far - note: works even if matchIndex overflows */ + if (tableType == byU16) assert((current - matchIndex) <= MAX_DISTANCE); /* too_far presumed impossible with byU16 */ if (LZ4_read32(match) == LZ4_read32(ip)) { if (maybe_extMem) offset = current - matchIndex; @@ -861,7 +861,6 @@ _next_match: /* Fill table */ LZ4_putPosition(ip-2, cctx->hashTable, tableType, base); -#if 1 /* Test next position */ if (tableType == byPtr) { @@ -901,7 +900,7 @@ _next_match: } LZ4_putIndexOnHash(current, h, cctx->hashTable, tableType); if ( ((dictIssue==dictSmall) ? (matchIndex >= prefixIdxLimit) : 1) - && ((tableType==byU16) ? 1 : (matchIndex+MAX_DISTANCE >= current)) + && ((tableType==byU16) ? 1 : (current - matchIndex <= MAX_DISTANCE)) && (LZ4_read32(match) == LZ4_read32(ip)) ) { token=op++; *token=0; @@ -914,13 +913,6 @@ _next_match: /* Prepare next loop */ forwardH = LZ4_hashPosition(++ip, tableType); -#else - - /* Prepare next loop */ - forwardH = LZ4_hashPosition(ip, tableType); - -#endif - } _last_literals: