Artifact 36c908a8e9e31cd9857b3b2079391577ba6abf0bad5fc821255b9fe2791bea08:
- File
cli/repo_delta.go
— part of check-in
[38eddf43f1]
at
2026-10-11 23:34:08
on branch main
— fix(delta): refuse a source or target of 2^32 bytes or more in Create (#279) (#320)
The delta format holds integers below 2^32 only, so a producer must not write a target length or copy offset at or past that. Create now returns ErrTooLarge for such inputs; Deltify then stores the content whole, as fossil does when a delta is not worthwhile, and the other callers propagate the error. Apply's header check becomes >= so it refuses exactly what Create will not write. The bound is tested on lengths, as a direct test needs a 4 GiB input. (user: [email protected], size: 1729) [annotate] [blame] [check-ins using]
package cli import ( "fmt" "os" "github.com/danmestas/go-libfossil/internal/delta" ) // RepoDeltaCmd groups delta create/apply operations. type RepoDeltaCmd struct { Create RepoDeltaCreateCmd `cmd:"" help:"Create a delta between two files"` Apply RepoDeltaApplyCmd `cmd:"" help:"Apply a delta to a source file"` } // RepoDeltaCreateCmd creates a delta between two files. type RepoDeltaCreateCmd struct { Source string `arg:"" help:"Source (original) file"` Target string `arg:"" help:"Target (new) file"` Output string `short:"o" help:"Output file (default: stdout)"` } func (c *RepoDeltaCreateCmd) Run(g *Globals) error { src, err := os.ReadFile(c.Source) if err != nil { return fmt.Errorf("reading source: %w", err) } tgt, err := os.ReadFile(c.Target) if err != nil { return fmt.Errorf("reading target: %w", err) } d, err := delta.Create(src, tgt) if err != nil { return err } if c.Output != "" { return os.WriteFile(c.Output, d, 0o644) } os.Stdout.Write(d) return nil } // RepoDeltaApplyCmd applies a delta to a source file. type RepoDeltaApplyCmd struct { Source string `arg:"" help:"Source (original) file"` Delta string `arg:"" help:"Delta file"` Output string `short:"o" help:"Output file (default: stdout)"` } func (c *RepoDeltaApplyCmd) Run(g *Globals) error { src, err := os.ReadFile(c.Source) if err != nil { return fmt.Errorf("reading source: %w", err) } d, err := os.ReadFile(c.Delta) if err != nil { return fmt.Errorf("reading delta: %w", err) } result, err := delta.Apply(src, d) if err != nil { return fmt.Errorf("applying delta: %w", err) } if c.Output != "" { return os.WriteFile(c.Output, result, 0o644) } os.Stdout.Write(result) return nil }