From: Yann Collet Date: Tue, 7 Feb 2023 04:36:37 +0000 (-0800) Subject: copy fix for v0.3 to v0.4 X-Git-Tag: v1.5.4^2~6^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b20e4e95f2a9295adf82c4fdb13bcb1864983fd9;p=thirdparty%2Fzstd.git copy fix for v0.3 to v0.4 in case it would be applicable for this legacy version too. --- diff --git a/lib/legacy/zstd_v03.c b/lib/legacy/zstd_v03.c index d247d5d55..b0d7f521e 100644 --- a/lib/legacy/zstd_v03.c +++ b/lib/legacy/zstd_v03.c @@ -2710,7 +2710,7 @@ static size_t ZSTD_execSequence(BYTE* op, if (seqLength > (size_t)(oend - op)) return ERROR(dstSize_tooSmall); if (sequence.litLength > (size_t)(litLimit - *litPtr)) return ERROR(corruption_detected); - /* Now we know there are no overflow in literal nor match lengths, can use the pointer check */ + /* Now we know there are no overflow in literal nor match lengths, can use pointer checks */ if (oLitEnd > oend_8) return ERROR(dstSize_tooSmall); if (sequence.offset > (U32)(oLitEnd - base)) return ERROR(corruption_detected); diff --git a/lib/legacy/zstd_v04.c b/lib/legacy/zstd_v04.c index 23735443a..f820134b7 100644 --- a/lib/legacy/zstd_v04.c +++ b/lib/legacy/zstd_v04.c @@ -2828,13 +2828,20 @@ static size_t ZSTD_execSequence(BYTE* op, const BYTE* const litEnd = *litPtr + sequence.litLength; const BYTE* match = oLitEnd - sequence.offset; - /* check */ - if (oLitEnd > oend_8) return ERROR(dstSize_tooSmall); /* last match must start at a minimum distance of 8 from oend */ + /* checks */ + size_t const seqLength = sequence.litLength + sequence.matchLength; + + if (seqLength > (size_t)(oend - op)) return ERROR(dstSize_tooSmall); + if (sequence.litLength > (size_t)(litLimit - *litPtr)) return ERROR(corruption_detected); + /* Now we know there are no overflow in literal nor match lengths, can use pointer checks */ + if (oLitEnd > oend_8) return ERROR(dstSize_tooSmall); + if (sequence.offset > (U32)(oLitEnd - base)) return ERROR(corruption_detected); + if (oMatchEnd > oend) return ERROR(dstSize_tooSmall); /* overwrite beyond dst buffer */ - if (litEnd > litLimit) return ERROR(corruption_detected); /* risk read beyond lit buffer */ + if (litEnd > litLimit) return ERROR(corruption_detected); /* overRead beyond lit buffer */ /* copy Literals */ - ZSTD_wildcopy(op, *litPtr, sequence.litLength); /* note : oLitEnd <= oend-8 : no risk of overwrite beyond oend */ + ZSTD_wildcopy(op, *litPtr, (ptrdiff_t)sequence.litLength); /* note : oLitEnd <= oend-8 : no risk of overwrite beyond oend */ op = oLitEnd; *litPtr = litEnd; /* update for next sequence */