pierrec / lz4

LZ4 compression and decompression in pure Go
BSD 3-Clause "New" or "Revised" License
869 stars 141 forks source link

Data race when using concurrency > 1 #192

Open mYmNeo opened 2 years ago

mYmNeo commented 2 years ago

In v4 branch, the lz library may cause data corrupt if you have set concurrency > 1. The race problem happens at

https://github.com/pierrec/lz4/blob/v4/writer.go#L100 https://github.com/pierrec/lz4/blob/v4/writer.go#L159

Suppose this scenario, the calling sequences are Write, Flush, Write, Flush repeatedly, this is a very common scenario in web server. When w.data is filled with data but doesn't reach the buffer size, calling Flush will send w.data[:w.idx] to channel and reset w.idx to zero. Here comes the problem because w.data[:w.idx] is a shadow copy of slice, that means a new call of Write and compression gorouting writes and reads the same underlaying byte array with an overlap index.