1
0
mirror of https://github.com/facebook/zstd.git synced 2025-08-05 19:15:58 +03:00

fixed asan

This commit is contained in:
Yann Collet
2015-11-20 12:46:08 +01:00
parent 55aa7f94e3
commit 402fdcf1a3
2 changed files with 119 additions and 120 deletions

View File

@@ -751,49 +751,49 @@ size_t ZSTD_compressBlock_fast_generic(ZSTD_CCtx* ctx,
if (MEM_read32(ip+1-offset_1) == MEM_read32(ip+1)) if (MEM_read32(ip+1-offset_1) == MEM_read32(ip+1))
{ {
matchLength = ZSTD_count(ip+1+MINMATCH, ip+1+MINMATCH-offset_1, iend); matchLength = ZSTD_count(ip+1+MINMATCH, ip+1+MINMATCH-offset_1, iend);
ip++; ip++;
offset = 0; offset = 0;
} }
else else
{ {
if ( (match < lowest) || if ( (match < lowest) ||
(MEM_read32(match) != MEM_read32(ip)) ) (MEM_read32(match) != MEM_read32(ip)) )
{ {
ip += ((ip-anchor) >> g_searchStrength) + 1; ip += ((ip-anchor) >> g_searchStrength) + 1;
continue; continue;
} }
matchLength = ZSTD_count(ip+MINMATCH, match+MINMATCH, iend); matchLength = ZSTD_count(ip+MINMATCH, match+MINMATCH, iend);
while ((ip>anchor) && (match>lowest) && (ip[-1] == match[-1])) { ip--; match--; matchLength++; } /* catch up */ while ((ip>anchor) && (match>lowest) && (ip[-1] == match[-1])) { ip--; match--; matchLength++; } /* catch up */
offset = ip-match; offset = ip-match;
offset_2 = offset_1; offset_2 = offset_1;
offset_1 = offset; offset_1 = offset;
} }
/* match found */ /* match found */
ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, offset, matchLength); ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, offset, matchLength);
ip += matchLength + MINMATCH; ip += matchLength + MINMATCH;
anchor = ip; anchor = ip;
if (ip <= ilimit) if (ip <= ilimit)
{ {
/* Fill Table */ /* Fill Table */
hashTable[ZSTD_hashPtr(ip-2-matchLength, hBits, mls)] = (U32)(ip-2-matchLength-base); hashTable[ZSTD_hashPtr(ip-2-matchLength, hBits, mls)] = (U32)(ip-2-matchLength-base);
hashTable[ZSTD_hashPtr(ip-2, hBits, mls)] = (U32)(ip-2-base); hashTable[ZSTD_hashPtr(ip-2, hBits, mls)] = (U32)(ip-2-base);
/* check immediate repcode */ /* check immediate repcode */
while ( (ip <= ilimit) while ( (ip <= ilimit)
&& (MEM_read32(ip) == MEM_read32(ip - offset_2)) ) && (MEM_read32(ip) == MEM_read32(ip - offset_2)) )
{ {
/* store sequence */ /* store sequence */
size_t ml = ZSTD_count(ip+MINMATCH, ip+MINMATCH-offset_2, iend); size_t ml = ZSTD_count(ip+MINMATCH, ip+MINMATCH-offset_2, iend);
size_t tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff; /* swap offset_2 <=> offset_1 */ size_t tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff; /* swap offset_2 <=> offset_1 */
hashTable[ZSTD_hashPtr(ip, hBits, mls)] = (U32)(ip-base); hashTable[ZSTD_hashPtr(ip, hBits, mls)] = (U32)(ip-base);
ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, ml); ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, ml);
ip += ml+MINMATCH; ip += ml+MINMATCH;
anchor = ip; anchor = ip;
continue; /* faster when present ... (?) */ continue; /* faster when present ... (?) */
} }
} }
} }
/* Last Literals */ /* Last Literals */
@@ -874,7 +874,7 @@ size_t ZSTD_compressBlock_fast_extDict_generic(ZSTD_CCtx* ctx,
const BYTE* lowMatchPtr = matchIndex < dictLimit ? dictBase + lowLimit : lowPrefixPtr; const BYTE* lowMatchPtr = matchIndex < dictLimit ? dictBase + lowLimit : lowPrefixPtr;
const U32 current = (U32)(ip-base); const U32 current = (U32)(ip-base);
const U32 repIndex = current + 1 - offset_1; const U32 repIndex = current + 1 - offset_1;
const BYTE* repBase = repIndex < dictLimit ? dictBase : base; const BYTE* repBase = repIndex < dictLimit ? dictBase : base;
const BYTE* repMatch = repBase + repIndex; const BYTE* repMatch = repBase + repIndex;
size_t matchLength; size_t matchLength;
U32 offset; U32 offset;
@@ -883,67 +883,67 @@ size_t ZSTD_compressBlock_fast_extDict_generic(ZSTD_CCtx* ctx,
if ( ((repIndex <= dictLimit-4) || (repIndex >= dictLimit)) if ( ((repIndex <= dictLimit-4) || (repIndex >= dictLimit))
&& (MEM_read32(repMatch) == MEM_read32(ip+1)) ) && (MEM_read32(repMatch) == MEM_read32(ip+1)) )
{ {
const BYTE* repMatchEnd = repIndex < dictLimit ? dictEnd : iend; const BYTE* repMatchEnd = repIndex < dictLimit ? dictEnd : iend;
const BYTE* iEndCount = (repMatchEnd - repMatch < iend - ip - 1) ? ip + 1 + (repMatchEnd - repMatch) : iend; const BYTE* iEndCount = (repMatchEnd - repMatch < iend - ip - 1) ? ip + 1 + (repMatchEnd - repMatch) : iend;
matchLength = ZSTD_count(ip+1+MINMATCH, repMatch+MINMATCH, iEndCount); matchLength = ZSTD_count(ip+1+MINMATCH, repMatch+MINMATCH, iEndCount);
if (match + matchLength + MINMATCH == dictEnd) if (match + matchLength + MINMATCH == dictEnd)
matchLength += ZSTD_count(ip + matchLength + MINMATCH, lowPrefixPtr, iend); matchLength += ZSTD_count(ip + matchLength + MINMATCH, lowPrefixPtr, iend);
ip++; ip++;
offset = 0; offset = 0;
} }
else else
{ {
if ( (matchIndex < lowLimit) || if ( (matchIndex < lowLimit) ||
(MEM_read32(match) != MEM_read32(ip)) ) (MEM_read32(match) != MEM_read32(ip)) )
{ ip += ((ip-anchor) >> g_searchStrength) + 1; continue; } { ip += ((ip-anchor) >> g_searchStrength) + 1; continue; }
{ {
const BYTE* matchEnd = matchIndex < dictLimit ? dictEnd : iend; const BYTE* matchEnd = matchIndex < dictLimit ? dictEnd : iend;
const BYTE* iEndCount = (matchEnd - match < iend - ip) ? ip + (matchEnd - match) : iend; const BYTE* iEndCount = (matchEnd - match < iend - ip) ? ip + (matchEnd - match) : iend;
matchLength = ZSTD_count(ip+MINMATCH, match+MINMATCH, iEndCount); matchLength = ZSTD_count(ip+MINMATCH, match+MINMATCH, iEndCount);
if (match + matchLength + MINMATCH == dictEnd) if (match + matchLength + MINMATCH == dictEnd)
matchLength += ZSTD_count(ip + matchLength + MINMATCH, lowPrefixPtr, iend); matchLength += ZSTD_count(ip + matchLength + MINMATCH, lowPrefixPtr, iend);
while ((ip>anchor) && (match>lowMatchPtr) && (ip[-1] == match[-1])) { ip--; match--; matchLength++; } /* catch up */ while ((ip>anchor) && (match>lowMatchPtr) && (ip[-1] == match[-1])) { ip--; match--; matchLength++; } /* catch up */
offset = current - matchIndex; offset = current - matchIndex;
offset_2 = offset_1; offset_2 = offset_1;
offset_1 = offset; offset_1 = offset;
} }
} }
/* found a match */
ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, offset, matchLength);
ip += matchLength + MINMATCH;
anchor = ip;
if (ip <= ilimit) /* found a match */
{ ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, offset, matchLength);
/* Fill Table */ ip += matchLength + MINMATCH;
hashTable[ZSTD_hashPtr(ip-2-matchLength, hBits, mls)] = (U32)(ip-2-matchLength-base); anchor = ip;
hashTable[ZSTD_hashPtr(ip-2, hBits, mls)] = (U32)(ip-2-base);
/* check immediate repcode */ if (ip <= ilimit)
while (ip <= ilimit) {
{ /* Fill Table */
U32 current2 = (U32)(ip-base); hashTable[ZSTD_hashPtr(ip-2-matchLength, hBits, mls)] = (U32)(ip-2-matchLength-base);
U32 repIndex2 = current2 - offset_2; hashTable[ZSTD_hashPtr(ip-2, hBits, mls)] = (U32)(ip-2-base);
const BYTE* repMatch2 = repIndex2 < dictLimit ? dictBase + repIndex2 : base + repIndex2; /* check immediate repcode */
while (ip <= ilimit)
{
U32 current2 = (U32)(ip-base);
const U32 repIndex2 = current2 - offset_2;
const BYTE* repMatch2 = repIndex2 < dictLimit ? dictBase + repIndex2 : base + repIndex2;
if ( ((repIndex2 <= dictLimit-4) || (repIndex2 >= dictLimit)) if ( ((repIndex2 <= dictLimit-4) || (repIndex2 >= dictLimit))
&& (MEM_read32(repMatch2) == MEM_read32(ip)) ) && (MEM_read32(repMatch2) == MEM_read32(ip)) )
{ {
size_t maxIlength = iend - ip; size_t maxIlength = iend - ip;
size_t maxMlength = repIndex2 < dictLimit ? dictLimit - repIndex2 : iend - repMatch2; size_t maxMlength = repIndex2 < dictLimit ? (size_t)(dictLimit - repIndex2) : (size_t)(iend - repMatch2);
size_t maxML = MIN(maxMlength, maxIlength); size_t maxML = MIN(maxMlength, maxIlength);
size_t ml = ZSTD_count(ip+MINMATCH, repMatch2+MINMATCH, ip + maxML); size_t ml = ZSTD_count(ip+MINMATCH, repMatch2+MINMATCH, ip + maxML);
U32 tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff; /* swap offset_2 <=> offset_1 */ U32 tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff; /* swap offset_2 <=> offset_1 */
if (ml+MINMATCH == maxMlength) /* reached end of extDict */ if (ml+MINMATCH == maxMlength) /* reached end of extDict */
ml += ZSTD_count(ip+MINMATCH+ml, lowPrefixPtr, iend); ml += ZSTD_count(ip+MINMATCH+ml, lowPrefixPtr, iend);
hashTable[ZSTD_hashPtr(ip, hBits, mls)] = (U32)(ip-base); hashTable[ZSTD_hashPtr(ip, hBits, mls)] = (U32)(ip-base);
ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, ml); ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, ml);
ip += ml+MINMATCH; ip += ml+MINMATCH;
anchor = ip; anchor = ip;
continue; continue;
} }
break; break;
} }
} }
} }
/* Last Literals */ /* Last Literals */
@@ -1323,7 +1323,7 @@ size_t ZSTD_compressBlock_lazy_generic(ZSTD_CCtx* ctx,
continue; continue;
} }
/* search first solution */ /* search first solution */
matchLength = searchMax(ctx, ip, iend, &offset, maxSearches, mls); matchLength = searchMax(ctx, ip, iend, &offset, maxSearches, mls);
if (matchLength < MINMATCH) if (matchLength < MINMATCH)
{ {
@@ -1463,24 +1463,24 @@ size_t ZSTD_compressBlock_greedy(ZSTD_CCtx* ctx, void* dst, size_t maxDstSize, c
/* Match Loop */ /* Match Loop */
while (ip < ilimit) while (ip < ilimit)
{ {
size_t matchLength; size_t matchLength;
size_t offset; size_t offset;
/* priority to repcode at ip+1 */ /* priority to repcode at ip+1 */
if (MEM_read32(ip+1) == MEM_read32(ip+1 - offset_1)) if (MEM_read32(ip+1) == MEM_read32(ip+1 - offset_1))
{ {
matchLength = ZSTD_count(ip+1+MINMATCH, ip+1+MINMATCH-offset_1, iend) + MINMATCH; matchLength = ZSTD_count(ip+1+MINMATCH, ip+1+MINMATCH-offset_1, iend) + MINMATCH;
ip ++; ip ++;
offset = 0; offset = 0;
} }
else else
{ {
/* search */ /* search */
offset = 99999999; /* init to high value */ offset = 99999999; /* init to high value */
matchLength = ZSTD_HcFindBestMatch_selectMLS(ctx, ip, iend, &offset, maxSearches, mls); matchLength = ZSTD_HcFindBestMatch_selectMLS(ctx, ip, iend, &offset, maxSearches, mls);
if (matchLength < MINMATCH) if (matchLength < MINMATCH)
{ {
/* not found */ /* not found */
ip += ((ip-anchor) >> g_searchStrength) + 1; /* jump faster over incompressible sections */ ip += ((ip-anchor) >> g_searchStrength) + 1; /* jump faster over incompressible sections */
continue; continue;
} }
@@ -1488,14 +1488,14 @@ size_t ZSTD_compressBlock_greedy(ZSTD_CCtx* ctx, void* dst, size_t maxDstSize, c
while ((ip>anchor) && (ip-offset>ctx->base) && (ip[-1] == ip[-1-offset])) { ip--; matchLength++; } /* catch up */ while ((ip>anchor) && (ip-offset>ctx->base) && (ip[-1] == ip[-1-offset])) { ip--; matchLength++; } /* catch up */
offset_2 = offset_1, offset_1 = offset; offset_2 = offset_1, offset_1 = offset;
} }
/* store found sequence */ /* store found sequence */
{ {
size_t litLength = ip-anchor; size_t litLength = ip-anchor;
ZSTD_storeSeq(seqStorePtr, litLength, anchor, offset, matchLength-MINMATCH); ZSTD_storeSeq(seqStorePtr, litLength, anchor, offset, matchLength-MINMATCH);
ip += matchLength; ip += matchLength;
anchor = ip; anchor = ip;
} }
/* check immediate repcode */ /* check immediate repcode */
while ( (ip <= ilimit) while ( (ip <= ilimit)
@@ -1511,7 +1511,7 @@ size_t ZSTD_compressBlock_greedy(ZSTD_CCtx* ctx, void* dst, size_t maxDstSize, c
anchor = ip; anchor = ip;
continue; /* faster when present ... (?) */ continue; /* faster when present ... (?) */
} }
} }
/* Last Literals */ /* Last Literals */

View File

@@ -81,7 +81,6 @@ typedef BYTE litDistribTable[LTSIZE];
/********************************************************* /*********************************************************
* Local Functions * Local Functions
*********************************************************/ *********************************************************/
@@ -130,7 +129,7 @@ static BYTE RDG_genChar(U32* seed, const litDistribTable lt)
} }
#define RDG_RAND15BITS ((RDG_rand(seed) >> 3) & 32767) #define RDG_RAND15BITS ((RDG_rand(seed) >> 3) & 0x7FFF)
#define RDG_RANDLENGTH ( ((RDG_rand(seed) >> 7) & 7) ? (RDG_rand(seed) & 15) : (RDG_rand(seed) & 511) + 15) #define RDG_RANDLENGTH ( ((RDG_rand(seed) >> 7) & 7) ? (RDG_rand(seed) & 15) : (RDG_rand(seed) & 511) + 15)
void RDG_genBlock(void* buffer, size_t buffSize, size_t prefixSize, double matchProba, litDistribTable lt, unsigned* seedPtr) void RDG_genBlock(void* buffer, size_t buffSize, size_t prefixSize, double matchProba, litDistribTable lt, unsigned* seedPtr)
{ {
@@ -168,16 +167,16 @@ void RDG_genBlock(void* buffer, size_t buffSize, size_t prefixSize, double match
{ {
/* Copy (within 32K) */ /* Copy (within 32K) */
size_t match; size_t match;
size_t length = RDG_RANDLENGTH + 4; size_t d;
int length = RDG_RANDLENGTH + 4;
U32 offset = RDG_RAND15BITS + 1; U32 offset = RDG_RAND15BITS + 1;
U32 repeatOffset = (RDG_rand(seed) & 15) == 2; U32 repeatOffset = (RDG_rand(seed) & 15) == 2;
if (repeatOffset) offset = prevOffset; if (repeatOffset) offset = prevOffset;
if (offset > pos) offset = (U32)pos; if (offset > pos) offset = (U32)pos;
match = pos - offset; match = pos - offset;
if (length > buffSize-pos) length = buffSize-pos; d = pos + length;
memcpy(buffPtr+pos, buffPtr+match, length); if (d > buffSize) d = buffSize;
pos += length; while (pos < d) buffPtr[pos++] = buffPtr[match++]; /* correctly manages overlaps */
prevOffset = offset;
} }
else else
{ {