Discard unread data of git cat-file (#29297)

Fixes #29101
Related #29298

Discard all read data to prevent misinterpreting existing data. Some
discard calls were missing in error cases.

---------

Co-authored-by: yp05327 <576951401@qq.com>
(cherry picked from commit d6811baf88ca6d58b92d4dc12b1f2a292198751f)
This commit is contained in:
KN4CK3R 2024-02-22 04:48:19 +01:00 committed by Earl Warren
parent 3591d9bff9
commit 9debff3b71
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
10 changed files with 66 additions and 80 deletions

View file

@ -9,7 +9,6 @@ import (
"bufio"
"bytes"
"io"
"math"
"code.gitea.io/gitea/modules/log"
)
@ -104,25 +103,6 @@ func (b *blobReader) Read(p []byte) (n int, err error) {
// Close implements io.Closer
func (b *blobReader) Close() error {
defer b.cancel()
if b.n > 0 {
for b.n > math.MaxInt32 {
n, err := b.rd.Discard(math.MaxInt32)
b.n -= int64(n)
if err != nil {
return err
}
b.n -= math.MaxInt32
}
n, err := b.rd.Discard(int(b.n))
b.n -= int64(n)
if err != nil {
return err
}
}
if b.n == 0 {
_, err := b.rd.Discard(1)
b.n--
return err
}
return nil
return DiscardFull(b.rd, b.n+1)
}