Skip to content

Commit

Permalink
bugfix: Fix reused memory on long files
Browse files Browse the repository at this point in the history
  • Loading branch information
applejag committed Dec 11, 2023
1 parent 55c4ba5 commit 25c1b14
Showing 1 changed file with 5 additions and 3 deletions.
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

0 comments on commit 25c1b14

Please sign in to comment.