Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OP-2240] bugfix: Fix reused memory on long files #29

Merged
merged 4 commits into from
Dec 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions pkg/patch/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"io"
"os"
"path/filepath"
"slices"

"github.com/RiskIdent/jelease/pkg/config"
"github.com/RiskIdent/jelease/pkg/util"
Expand All @@ -46,14 +47,16 @@ type TemplateContextRegex struct {
func ApplyMany(repoDir string, patches []config.PackageRepoPatch, tmplCtx TemplateContext) error {
for _, p := range patches {
if err := Apply(repoDir, p, tmplCtx); err != nil {
return err
return fmt.Errorf("file %q: %w", p.File, err)
}
}
return nil
}

// Apply applies a single patch to the repository.
func Apply(repoDir string, patch config.PackageRepoPatch, tmplCtx TemplateContext) error {
log.Debug().Str("file", patch.File).Msg("Patching file.")

// TODO: Check that the patch path doesn't go outside the repo dir.
// For example, reject stuff like "../../../somefile.txt"
path := filepath.Join(repoDir, patch.File)
Expand All @@ -71,7 +74,6 @@ func Apply(repoDir string, patch config.PackageRepoPatch, tmplCtx TemplateContex
return fmt.Errorf("write patch: %w", err)
}

log.Debug().Str("file", patch.File).Msg("Patched file.")
return nil
}

Expand Down Expand Up @@ -175,7 +177,7 @@ func readLinesFromReader(r io.Reader) ([][]byte, error) {
var lines [][]byte
scanner := bufio.NewScanner(r)
for scanner.Scan() {
lines = append(lines, scanner.Bytes())
lines = append(lines, slices.Clone(scanner.Bytes()))
}
if err := scanner.Err(); err != nil {
return nil, err
Expand Down