homeport/dyff

Dyff panics when using --exclude on reports with documents removed

lambchr opened this issue · 2 comments

I received this panic when running this dyff command:

➜ dyff between \
  --use-go-patch-style \
  ./a.yaml \
  ./b.yaml \
  --exclude "/metadata/labels/test"
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x77a7d7]

goroutine 1 [running]:
github.com/homeport/dyff/pkg/dyff.Report.Exclude.func1(0x0)
	github.com/homeport/dyff/pkg/dyff/reports.go:51 +0x137
github.com/homeport/dyff/pkg/dyff.Report.filter({{{0x7ffe80b8113b, 0x8}, {0x0, 0x0}, {0xc0000a63f0, 0x2, 0x2}, {0xc0000aa2a0, 0x2, 0x2}}, ...}, ...)
	github.com/homeport/dyff/pkg/dyff/reports.go:16 +0x11c
github.com/homeport/dyff/pkg/dyff.Report.Exclude({{{0x7ffe80b8113b, 0x8}, {0x0, 0x0}, {0xc0000a63f0, 0x2, 0x2}, {0xc0000aa2a0, 0x2, 0x2}}, ...}, ...)
	github.com/homeport/dyff/pkg/dyff/reports.go:48 +0x125
github.com/homeport/dyff/internal/cmd.glob..func1(0xb27020, {0xc00007e7d0, 0x5, 0x5})
	github.com/homeport/dyff/internal/cmd/between.go:103 +0x55e
github.com/spf13/cobra.(*Command).execute(0xb27020, {0xc00007e780, 0x5, 0x5})
	github.com/spf13/cobra@v1.4.0/command.go:856 +0x60e
github.com/spf13/cobra.(*Command).ExecuteC(0xb277a0)
	github.com/spf13/cobra@v1.4.0/command.go:974 +0x3bc
github.com/spf13/cobra.(*Command).Execute(...)
	github.com/spf13/cobra@v1.4.0/command.go:902
github.com/homeport/dyff/internal/cmd.Execute()
	github.com/homeport/dyff/internal/cmd/root.go:110 +0x9a
main.main()
	./main.go:35 +0x2b

here are the two YAMLs being compared, the major difference is that the Namespace resource was removed from the second:

# a.yaml
---
apiVersion: v1
kind: Namespace
metadata:
  name: test
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    test: label1
    app: test
  name: test
spec:
  replicas: 2
  selector:
    matchLabels:
      app: test
  template:
    metadata:
      labels:
        app: test
    spec:
      containers:
      - image: k8s.gcr.io/serve_hostname
        imagePullPolicy: Always
        name: test

# b.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    test: label2
    app: test
  name: test2
spec:
  replicas: 2
  selector:
    matchLabels:
      app: test
  template:
    metadata:
      labels:
        app: test
    spec:
      containers:
      - image: k8s.gcr.io/serve_hostname
        imagePullPolicy: Always
        name: test

I think it is because filterPath here is nil when the Diff object is for a removed document. So a check would need to be added to this if statement to ignore the case when filterPath is nil.