File: fuzz.go

package info (click to toggle)
golang-github-pkg-diff 0.0~git20210226.20ebb0f-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, trixie
  • size: 3,528 kB
  • sloc: makefile: 3
file content (47 lines) | stat: -rw-r--r-- 1,109 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// +build gofuzz

package diff

import (
	"bytes"
	"context"
	"io"
	"io/ioutil"

	"github.com/pkg/diff/ctxt"
	"github.com/pkg/diff/myers"
	"github.com/pkg/diff/write"
)

func Fuzz(data []byte) int {
	if len(data) < 2 {
		return -1
	}
	sz := int(data[0])
	data = data[1:]

	nul := bytes.IndexByte(data, 0)
	if nul == -1 {
		nul = len(data) - 1
	}
	a := data[:nul]
	b := data[nul:]
	ab := &IndividualBytes{a: a, b: b}
	s := myers.Diff(context.Background(), ab)
	s = ctxt.Size(s, sz)
	err := write.Unified(s, ioutil.Discard, ab)
	if err != nil {
		panic(err)
	}
	return 0
}

type IndividualBytes struct {
	a, b []byte
}

func (ab *IndividualBytes) LenA() int                                { return len(ab.a) }
func (ab *IndividualBytes) LenB() int                                { return len(ab.b) }
func (ab *IndividualBytes) Equal(ai, bi int) bool                    { return ab.a[ai] == ab.b[bi] }
func (ab *IndividualBytes) WriteATo(w io.Writer, i int) (int, error) { return w.Write([]byte{ab.a[i]}) }
func (ab *IndividualBytes) WriteBTo(w io.Writer, i int) (int, error) { return w.Write([]byte{ab.b[i]}) }