1
1
mirror of https://github.com/wader/fq.git synced 2024-12-23 05:13:30 +03:00

fuzz: Fuzz all formats

Fix string readers to not panic on make slice with bogus sizes
This commit is contained in:
Mattias Wadman 2022-01-24 16:56:24 +01:00
parent 6fcfa77300
commit b8efd8e549
2 changed files with 36 additions and 1 deletions

View File

@ -51,7 +51,11 @@ func (ft *fuzzTest) Stderr() interp.Output { return fuzzTestOutput{os.Std
func (ft *fuzzTest) InterruptChan() chan struct{} { return nil }
func (ft *fuzzTest) Environ() []string { return nil }
func (ft *fuzzTest) Args() []string {
return []string{}
return []string{
`fq`,
`-d`, `raw`,
`(_registry.groups | keys[] | select(. != "all")) as $f | decode($f)?`,
}
}
func (ft *fuzzTest) ConfigDir() (string, error) { return "/config", nil }
func (ft *fuzzTest) FS() fs.FS { return fuzzFS{} }

View File

@ -127,6 +127,14 @@ var UTF16BE = unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM)
var UTF16LE = unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM)
func (d *D) tryText(nBytes int, e encoding.Encoding) (string, error) {
if nBytes < 0 {
return "", fmt.Errorf("tryText nBytes must be >= 0 (%d)", nBytes)
}
bytesLeft := d.BitsLeft() / 8
if int64(nBytes) > bytesLeft {
return "", fmt.Errorf("tryText nBytes %d outside buffer, %d bytes left", nBytes, bytesLeft)
}
bs, err := d.bitBuf.BytesLen(nBytes)
if err != nil {
return "", err
@ -139,6 +147,17 @@ func (d *D) tryText(nBytes int, e encoding.Encoding) (string, error) {
// fixedBytes if != -1 read nBytes but trim to length
//nolint:unparam
func (d *D) tryTextLenPrefixed(lenBits int, fixedBytes int, e encoding.Encoding) (string, error) {
if lenBits < 0 {
return "", fmt.Errorf("tryTextLenPrefixed lenBits must be >= 0 (%d)", lenBits)
}
if fixedBytes < 0 {
return "", fmt.Errorf("tryTextLenPrefixed fixedBytes must be >= 0 (%d)", fixedBytes)
}
bytesLeft := d.BitsLeft() / 8
if int64(fixedBytes) > bytesLeft {
return "", fmt.Errorf("tryTextLenPrefixed fixedBytes %d outside, %d bytes left", fixedBytes, bytesLeft)
}
p := d.Pos()
l, err := d.bits(lenBits)
if err != nil {
@ -163,6 +182,10 @@ func (d *D) tryTextLenPrefixed(lenBits int, fixedBytes int, e encoding.Encoding)
}
func (d *D) tryTextNull(nullBytes int, e encoding.Encoding) (string, error) {
if nullBytes < 1 {
return "", fmt.Errorf("tryTextNull nullBytes must be >= 1 (%d)", nullBytes)
}
p := d.Pos()
peekBits, _, err := d.TryPeekFind(nullBytes*8, 8, -1, func(v uint64) bool { return v == 0 })
if err != nil {
@ -179,6 +202,14 @@ func (d *D) tryTextNull(nullBytes int, e encoding.Encoding) (string, error) {
}
func (d *D) tryTextNullLen(fixedBytes int, e encoding.Encoding) (string, error) {
if fixedBytes < 0 {
return "", fmt.Errorf("tryTextNullLen fixedBytes must be >= 0 (%d)", fixedBytes)
}
bytesLeft := d.BitsLeft() / 8
if int64(fixedBytes) > bytesLeft {
return "", fmt.Errorf("tryTextNullLen fixedBytes %d outside, %d bytes left", fixedBytes, bytesLeft)
}
bs, err := d.bitBuf.BytesLen(fixedBytes)
if err != nil {
return "", err