osv.dev icon indicating copy to clipboard operation
osv.dev copied to clipboard

panic: index out of range in VendorProduct.UnmarshalText when input lacks separator

Open Alearner12 opened this issue 10 months ago • 0 comments

The VendorProduct.UnmarshalText function in vulnfeeds/cves/versions.go panics if the input string does not contain a colon. Specifically, it splits the input string by ":" and immediately accesses index [1], which leads to a runtime panic when processing malformed data lacking a colon (e.g., due to malformed input in a source file).

To Reproduce Steps to reproduce the behaviour:

  1. Call UnmarshalText with a byte slice that does not contain a colon.
  2. Observe the panic.

Example code to reproduce:

package main

import (
	"fmt"
	"strings"
)

type VendorProduct struct {
	Vendor  string
	Product string
}

func (vp *VendorProduct) UnmarshalText(text []byte) error {
	s := strings.Split(string(text), ":")
	vp.Vendor = s[0]
	vp.Product = s[1] // Panic occurs here
	return nil
}

func main() {
	vp := &VendorProduct{}
	// Trigger panic
	_ = vp.UnmarshalText([]byte("malformed_input"))
}

Stack trace when run:

panic: runtime error: index out of range [1] with length 1

goroutine 1 [running]:
main.(*VendorProduct).UnmarshalText(...)
        /path/to/repro.go:16 +0x...

Proposed Fix Add a length check before accessing the slice indices.

Additional context I am happy to submit a PR for this if you would like!

Alearner12 avatar Jan 25 '26 18:01 Alearner12