Skip to content

Commit 33cb851

Browse files
committed
decompress: changed final memcpy() into memmove()
for compatibility with in-place decompression scenarios.
1 parent 4eec64e commit 33cb851

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

lib/lz4.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
# pragma warning(disable : 4293) /* disable: C4293: too large shift (32-bits) */
121121
#endif /* _MSC_VER */
122122

123+
#define LZ4_FORCE_INLINE static
123124
#ifndef LZ4_FORCE_INLINE
124125
# ifdef _MSC_VER /* Visual Studio */
125126
# define LZ4_FORCE_INLINE static __forceinline
@@ -707,18 +708,19 @@ static const BYTE* LZ4_getPositionOnHash(U32 h, const void* tableBase, tableType
707708
{ const U16* const hashTable = (const U16*) tableBase; return hashTable[h] + srcBase; } /* default, to ensure a return */
708709
}
709710

710-
LZ4_FORCE_INLINE const BYTE* LZ4_getPosition(const BYTE* p,
711-
const void* tableBase, tableType_t tableType,
712-
const BYTE* srcBase)
711+
LZ4_FORCE_INLINE const BYTE*
712+
LZ4_getPosition(const BYTE* p,
713+
const void* tableBase, tableType_t tableType,
714+
const BYTE* srcBase)
713715
{
714716
U32 const h = LZ4_hashPosition(p, tableType);
715717
return LZ4_getPositionOnHash(h, tableBase, tableType, srcBase);
716718
}
717719

718-
LZ4_FORCE_INLINE void LZ4_prepareTable(
719-
LZ4_stream_t_internal* const cctx,
720-
const int inputSize,
721-
const tableType_t tableType) {
720+
LZ4_FORCE_INLINE void
721+
LZ4_prepareTable(LZ4_stream_t_internal* const cctx,
722+
const int inputSize,
723+
const tableType_t tableType) {
722724
/* If compression failed during the previous step, then the context
723725
* is marked as dirty, therefore, it has to be fully reset.
724726
*/
@@ -733,9 +735,10 @@ LZ4_FORCE_INLINE void LZ4_prepareTable(
733735
* out if it's safe to leave as is or whether it needs to be reset.
734736
*/
735737
if (cctx->tableType != clearedTable) {
738+
assert(inputSize >= 0);
736739
if (cctx->tableType != tableType
737-
|| (tableType == byU16 && cctx->currentOffset + inputSize >= 0xFFFFU)
738-
|| (tableType == byU32 && cctx->currentOffset > 1 GB)
740+
|| ((tableType == byU16) && cctx->currentOffset + (unsigned)inputSize >= 0xFFFFU)
741+
|| ((tableType == byU32) && cctx->currentOffset > 1 GB)
739742
|| tableType == byPtr
740743
|| inputSize >= 4 KB)
741744
{
@@ -1850,7 +1853,7 @@ LZ4_decompress_generic(
18501853
if ((!endOnInput) && (cpy != oend)) { goto _output_error; } /* Error : block decoding must stop exactly there */
18511854
if ((endOnInput) && ((ip+length != iend) || (cpy > oend))) { goto _output_error; } /* Error : input must be consumed */
18521855
}
1853-
memcpy(op, ip, length);
1856+
memmove(op, ip, length); /* supports overlapping memory regions, which only matters for in-place decompression scenarios */
18541857
ip += length;
18551858
op += length;
18561859
if (!partialDecoding || (cpy == oend)) {

0 commit comments

Comments
 (0)