Skip to content

Commit 6e866fe

Browse files
AlexanderYastrebovgopherbot
authored andcommitted
internal/zstd: handle match extending past window
For #62513 Change-Id: I59c24b254d5073140811b41497eabb91fb0046e9 GitHub-Last-Rev: 4dd16fc GitHub-Pull-Request: #63248 Reviewed-on: https://go-review.googlesource.com/c/go/+/531255 LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Than McIntosh <[email protected]>
1 parent 6a4a596 commit 6e866fe

File tree

2 files changed

+18
-26
lines changed

2 files changed

+18
-26
lines changed

src/internal/zstd/block.go

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -388,46 +388,38 @@ func (r *Reader) copyFromWindow(rbr *reverseBitReader, offset, match uint32) err
388388
return rbr.makeError("invalid zero offset")
389389
}
390390

391+
// Offset may point into the buffer or the window and
392+
// match may extend past the end of the initial buffer.
393+
// |--r.window--|--r.buffer--|
394+
// |<-----offset------|
395+
// |------match----------->|
396+
bufferOffset := uint32(0)
391397
lenBlock := uint32(len(r.buffer))
392398
if lenBlock < offset {
393399
lenWindow := r.window.len()
394-
windowOffset := offset - lenBlock
395-
if windowOffset > lenWindow {
400+
copy := offset - lenBlock
401+
if copy > lenWindow {
396402
return rbr.makeError("offset past window")
397403
}
398-
from := lenWindow - windowOffset
399-
if from+match <= lenWindow {
400-
r.buffer = r.window.appendTo(r.buffer, from, from+match)
401-
return nil
402-
}
403-
r.buffer = r.window.appendTo(r.buffer, from, lenWindow)
404-
copied := lenWindow - from
405-
offset -= copied
406-
match -= copied
407-
408-
if offset == 0 && match > 0 {
409-
return rbr.makeError("invalid offset")
404+
windowOffset := lenWindow - copy
405+
if copy > match {
406+
copy = match
410407
}
411-
}
412-
413-
from := lenBlock - offset
414-
if offset >= match {
415-
r.buffer = append(r.buffer, r.buffer[from:from+match]...)
416-
return nil
408+
r.buffer = r.window.appendTo(r.buffer, windowOffset, windowOffset+copy)
409+
match -= copy
410+
} else {
411+
bufferOffset = lenBlock - offset
417412
}
418413

419414
// We are being asked to copy data that we are adding to the
420415
// buffer in the same copy.
421416
for match > 0 {
422-
var copy uint32
423-
if offset >= match {
417+
copy := uint32(len(r.buffer)) - bufferOffset
418+
if copy > match {
424419
copy = match
425-
} else {
426-
copy = offset
427420
}
428-
r.buffer = append(r.buffer, r.buffer[from:from+copy]...)
421+
r.buffer = append(r.buffer, r.buffer[bufferOffset:bufferOffset+copy]...)
429422
match -= copy
430-
from += copy
431423
}
432424
return nil
433425
}
Binary file not shown.

0 commit comments

Comments
 (0)