aboutsummaryrefslogtreecommitdiffstats
path: root/routers/web/repo/compare.go
diff options
context:
space:
mode:
authorKN4CK3R <admin@oldschoolhack.me>2021-10-25 00:42:32 +0200
committerGitHub <noreply@github.com>2021-10-25 00:42:32 +0200
commit44f2c27d28eec4ba14b91b0729cbee831357b09f (patch)
tree9034fe81931413fca3e00eabb2d25f22a8f00bbf /routers/web/repo/compare.go
parentf99d50fc9f8baf406f32a491b214f8a13617d086 (diff)
downloadgitea-44f2c27d28eec4ba14b91b0729cbee831357b09f.tar.gz
gitea-44f2c27d28eec4ba14b91b0729cbee831357b09f.zip
Fix CSV render error (#17406)
closed #17378 Both errors from #17378 were caused by #15175. Problem 1 (error with added file): `ToUTF8WithFallbackReader` creates a `MultiReader` from a `byte[2048]` and the remaining reader. `CreateReaderAndGuessDelimiter` tries to read 10000 bytes from this reader but only gets 2048 because that's the first reader in the `MultiReader`. Then the `if size < 1e4` thinks the input is at EOF and just returns that. Problem 2 (error with changed file): The blob reader gets defer closed. That was fine because the old version reads the whole file into memory. Now with the streaming version the close needs to defer after the method.
Diffstat (limited to 'routers/web/repo/compare.go')
-rw-r--r--routers/web/repo/compare.go23
1 files changed, 15 insertions, 8 deletions
diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go
index 33b95838c7..9557b79bdf 100644
--- a/routers/web/repo/compare.go
+++ b/routers/web/repo/compare.go
@@ -10,6 +10,7 @@ import (
"errors"
"fmt"
"html"
+ "io"
"net/http"
"path"
"path/filepath"
@@ -105,30 +106,36 @@ func setCsvCompareContext(ctx *context.Context) {
errTooLarge := errors.New(ctx.Locale.Tr("repo.error.csv.too_large"))
- csvReaderFromCommit := func(c *git.Commit) (*csv.Reader, error) {
+ csvReaderFromCommit := func(c *git.Commit) (*csv.Reader, io.Closer, error) {
blob, err := c.GetBlobByPath(diffFile.Name)
if err != nil {
- return nil, err
+ return nil, nil, err
}
if setting.UI.CSV.MaxFileSize != 0 && setting.UI.CSV.MaxFileSize < blob.Size() {
- return nil, errTooLarge
+ return nil, nil, errTooLarge
}
reader, err := blob.DataAsync()
if err != nil {
- return nil, err
+ return nil, nil, err
}
- defer reader.Close()
- return csv_module.CreateReaderAndGuessDelimiter(charset.ToUTF8WithFallbackReader(reader))
+ csvReader, err := csv_module.CreateReaderAndGuessDelimiter(charset.ToUTF8WithFallbackReader(reader))
+ return csvReader, reader, err
}
- baseReader, err := csvReaderFromCommit(baseCommit)
+ baseReader, baseBlobCloser, err := csvReaderFromCommit(baseCommit)
+ if baseBlobCloser != nil {
+ defer baseBlobCloser.Close()
+ }
if err == errTooLarge {
return CsvDiffResult{nil, err.Error()}
}
- headReader, err := csvReaderFromCommit(headCommit)
+ headReader, headBlobCloser, err := csvReaderFromCommit(headCommit)
+ if headBlobCloser != nil {
+ defer headBlobCloser.Close()
+ }
if err == errTooLarge {
return CsvDiffResult{nil, err.Error()}
}