1
1
mirror of https://github.com/wader/fq.git synced 2024-11-26 10:33:53 +03:00

midi: fixed fuzzing errors

This commit is contained in:
twystd 2024-08-25 20:06:41 -07:00
parent 3105b7d27b
commit 2a65036680
3 changed files with 185 additions and 53 deletions

View File

@ -203,9 +203,11 @@ func decodeMetaEvent(d *decode.D, event uint8, ctx *context) {
func decodeSequenceNumber(d *decode.D) { func decodeSequenceNumber(d *decode.D) {
d.FieldUintFn("sequenceNumber", func(d *decode.D) uint64 { d.FieldUintFn("sequenceNumber", func(d *decode.D) uint64 {
data := vlf(d)
seqno := uint64(0) seqno := uint64(0)
if data, err := vlf(d); err != nil {
d.Errorf("%v", err)
} else {
if len(data) > 0 { if len(data) > 0 {
seqno += uint64(data[0]) seqno += uint64(data[0])
} }
@ -214,6 +216,7 @@ func decodeSequenceNumber(d *decode.D) {
seqno <<= 8 seqno <<= 8
seqno += uint64(data[1]) seqno += uint64(data[1])
} }
}
return seqno return seqno
}) })
@ -221,67 +224,124 @@ func decodeSequenceNumber(d *decode.D) {
func decodeText(d *decode.D) { func decodeText(d *decode.D) {
d.FieldStrFn("text", func(d *decode.D) string { d.FieldStrFn("text", func(d *decode.D) string {
return string(vlf(d)) if data, err := vlf(d); err != nil {
d.Errorf("%v", err)
} else {
return string(data)
}
return ""
}) })
} }
func decodeCopyright(d *decode.D) { func decodeCopyright(d *decode.D) {
d.FieldStrFn("copyright", func(d *decode.D) string { d.FieldStrFn("copyright", func(d *decode.D) string {
return string(vlf(d)) if data, err := vlf(d); err != nil {
d.Errorf("%v", err)
} else {
return string(data)
}
return ""
}) })
} }
func decodeTrackName(d *decode.D) { func decodeTrackName(d *decode.D) {
d.FieldStrFn("name", func(d *decode.D) string { d.FieldStrFn("name", func(d *decode.D) string {
return string(vlf(d)) if data, err := vlf(d); err != nil {
d.Errorf("%v", err)
} else {
return string(data)
}
return ""
}) })
} }
func decodeInstrumentName(d *decode.D) { func decodeInstrumentName(d *decode.D) {
d.FieldStrFn("instrument", func(d *decode.D) string { d.FieldStrFn("instrument", func(d *decode.D) string {
return string(vlf(d)) if data, err := vlf(d); err != nil {
d.Errorf("%v", err)
} else {
return string(data)
}
return ""
}) })
} }
func decodeLyric(d *decode.D) { func decodeLyric(d *decode.D) {
d.FieldStrFn("lyric", func(d *decode.D) string { d.FieldStrFn("lyric", func(d *decode.D) string {
return string(vlf(d)) if data, err := vlf(d); err != nil {
d.Errorf("%v", err)
} else {
return string(data)
}
return ""
}) })
} }
func decodeMarker(d *decode.D) { func decodeMarker(d *decode.D) {
d.FieldStrFn("marker", func(d *decode.D) string { d.FieldStrFn("marker", func(d *decode.D) string {
return string(vlf(d)) if data, err := vlf(d); err != nil {
d.Errorf("%v", err)
} else {
return string(data)
}
return ""
}) })
} }
func decodeCuePoint(d *decode.D) { func decodeCuePoint(d *decode.D) {
d.FieldStrFn("cue", func(d *decode.D) string { d.FieldStrFn("cue", func(d *decode.D) string {
return string(vlf(d)) if data, err := vlf(d); err != nil {
d.Errorf("%v", err)
} else {
return string(data)
}
return ""
}) })
} }
func decodeProgramName(d *decode.D) { func decodeProgramName(d *decode.D) {
d.FieldStrFn("program", func(d *decode.D) string { d.FieldStrFn("program", func(d *decode.D) string {
return string(vlf(d)) if data, err := vlf(d); err != nil {
d.Errorf("%v", err)
} else {
return string(data)
}
return ""
}) })
} }
func decodeDeviceName(d *decode.D) { func decodeDeviceName(d *decode.D) {
d.FieldStrFn("device", func(d *decode.D) string { d.FieldStrFn("device", func(d *decode.D) string {
return string(vlf(d)) if data, err := vlf(d); err != nil {
d.Errorf("%v", err)
} else {
return string(data)
}
return ""
}) })
} }
func decodeMIDIChannelPrefix(d *decode.D) { func decodeMIDIChannelPrefix(d *decode.D) {
d.FieldUintFn("channel", func(d *decode.D) uint64 { d.FieldUintFn("channel", func(d *decode.D) uint64 {
channel := uint64(0) channel := uint64(0)
data := vlf(d)
if data, err := vlf(d); err != nil {
d.Errorf("%v", err)
} else {
for _, b := range data { for _, b := range data {
channel <<= 8 channel <<= 8
channel |= uint64(b & 0x00ff) channel |= uint64(b & 0x00ff)
} }
}
return channel return channel
}) })
@ -290,12 +350,15 @@ func decodeMIDIChannelPrefix(d *decode.D) {
func decodeMIDIPort(d *decode.D) { func decodeMIDIPort(d *decode.D) {
d.FieldUintFn("port", func(d *decode.D) uint64 { d.FieldUintFn("port", func(d *decode.D) uint64 {
channel := uint64(0) channel := uint64(0)
data := vlf(d)
if data, err := vlf(d); err != nil {
d.Errorf("%v", err)
} else {
for _, b := range data { for _, b := range data {
channel <<= 8 channel <<= 8
channel |= uint64(b & 0x00ff) channel |= uint64(b & 0x00ff)
} }
}
return channel return channel
}) })
@ -304,12 +367,15 @@ func decodeMIDIPort(d *decode.D) {
func decodeTempo(d *decode.D) { func decodeTempo(d *decode.D) {
d.FieldUintFn("tempo", func(d *decode.D) uint64 { d.FieldUintFn("tempo", func(d *decode.D) uint64 {
tempo := uint64(0) tempo := uint64(0)
data := vlf(d)
if data, err := vlf(d); err != nil {
d.Errorf("%v", err)
} else {
for _, b := range data { for _, b := range data {
tempo <<= 8 tempo <<= 8
tempo |= uint64(b & 0x00ff) tempo |= uint64(b & 0x00ff)
} }
}
return tempo return tempo
}) })
@ -318,10 +384,16 @@ func decodeTempo(d *decode.D) {
func decodeSMPTEOffset(d *decode.D) { func decodeSMPTEOffset(d *decode.D) {
d.FieldStruct("offset", func(d *decode.D) { d.FieldStruct("offset", func(d *decode.D) {
var data []uint8 var data []uint8
d.FieldStrFn("bytes", func(d *decode.D) string { var err error
data = vlf(d)
d.FieldStrFn("bytes", func(d *decode.D) string {
if data, err = vlf(d); err != nil {
d.Errorf("%v", err)
} else {
return fmt.Sprintf("%v", data) return fmt.Sprintf("%v", data)
}
return "[]"
}) })
if len(data) > 0 { if len(data) > 0 {
@ -353,10 +425,16 @@ func decodeSMPTEOffset(d *decode.D) {
func decodeTimeSignature(d *decode.D) { func decodeTimeSignature(d *decode.D) {
d.FieldStruct("signature", func(d *decode.D) { d.FieldStruct("signature", func(d *decode.D) {
var data []uint8 var data []uint8
d.FieldStrFn("bytes", func(d *decode.D) string { var err error
data = vlf(d)
d.FieldStrFn("bytes", func(d *decode.D) string {
if data, err = vlf(d); err != nil {
d.Errorf("%v", err)
} else {
return fmt.Sprintf("%v", data) return fmt.Sprintf("%v", data)
}
return "[]"
}) })
if len(data) > 0 { if len(data) > 0 {
@ -384,9 +462,11 @@ func decodeTimeSignature(d *decode.D) {
func decodeKeySignature(d *decode.D) { func decodeKeySignature(d *decode.D) {
d.FieldUintFn("key", func(d *decode.D) uint64 { d.FieldUintFn("key", func(d *decode.D) uint64 {
data := vlf(d)
key := uint64(0) key := uint64(0)
if data, err := vlf(d); err != nil {
d.Errorf("%v", err)
} else {
if len(data) > 0 { if len(data) > 0 {
key <<= 8 key <<= 8
key |= uint64(data[0]) & 0x00ff key |= uint64(data[0]) & 0x00ff
@ -396,6 +476,7 @@ func decodeKeySignature(d *decode.D) {
key <<= 8 key <<= 8
key |= uint64(data[1]) & 0x00ff key |= uint64(data[1]) & 0x00ff
} }
}
return key return key
@ -404,17 +485,31 @@ func decodeKeySignature(d *decode.D) {
func decodeEndOfTrack(d *decode.D) { func decodeEndOfTrack(d *decode.D) {
d.FieldUintFn("length", func(d *decode.D) uint64 { d.FieldUintFn("length", func(d *decode.D) uint64 {
return uint64(len(vlf(d))) length := 0
if data, err := vlf(d); err != nil {
d.Errorf("%v", err)
} else {
length = len(data)
}
return uint64(length)
}) })
} }
func decodeSequencerSpecificEvent(d *decode.D) { func decodeSequencerSpecificEvent(d *decode.D) {
d.FieldStruct("info", func(d *decode.D) { d.FieldStruct("info", func(d *decode.D) {
var data []uint8 var data []uint8
d.FieldStrFn("bytes", func(d *decode.D) string { var err error
data = vlf(d)
d.FieldStrFn("bytes", func(d *decode.D) string {
if data, err = vlf(d); err != nil {
d.Errorf("%v", err)
} else {
return fmt.Sprintf("%v", data) return fmt.Sprintf("%v", data)
}
return "[]"
}) })
if len(data) > 2 && data[0] == 0x00 { if len(data) > 2 && data[0] == 0x00 {

View File

@ -38,14 +38,19 @@ func decodeMIDI(d *decode.D) any {
d.Endian = decode.BigEndian d.Endian = decode.BigEndian
// ... decode header // ... decode header
println(">> 1")
if err := skipTo(d, "MThd"); err != nil { if err := skipTo(d, "MThd"); err != nil {
d.Errorf("%v", err) d.Errorf("%v", err)
} else { } else {
println(">> 2")
d.FieldStruct("header", decodeMThd) d.FieldStruct("header", decodeMThd)
// ... decode tracks // ... decode tracks
println(">> 3")
d.FieldArray("tracks", func(d *decode.D) { d.FieldArray("tracks", func(d *decode.D) {
println(">> 4")
for d.BitsLeft() > 0 { for d.BitsLeft() > 0 {
println(">> 5", d.BitsLeft())
if err := skipTo(d, "MTrk"); err != nil { if err := skipTo(d, "MTrk"); err != nil {
d.Errorf("%v", err) d.Errorf("%v", err)
} else { } else {
@ -188,10 +193,14 @@ func vlq(d *decode.D) uint64 {
return vlq return vlq
} }
func vlf(d *decode.D) []uint8 { func vlf(d *decode.D) ([]uint8, error) {
N := int(vlq(d)) N := int(vlq(d))
return d.BytesLen(N) if int64(N*8) > d.BitsLeft() {
return nil, fmt.Errorf("invalid field length")
} else {
return d.BytesLen(N), nil
}
} }
func flush(d *decode.D, format string, args ...any) { func flush(d *decode.D, format string, args ...any) {

View File

@ -67,10 +67,16 @@ func decodeSysExEvent(d *decode.D, status uint8, ctx *context) {
func decodeSysExMessage(d *decode.D, ctx *context) { func decodeSysExMessage(d *decode.D, ctx *context) {
var bytes []uint8 var bytes []uint8
var err error
d.FieldStrFn("bytes", func(d *decode.D) string { d.FieldStrFn("bytes", func(d *decode.D) string {
bytes = vlf(d) if bytes, err = vlf(d); err != nil {
d.Errorf("%v", err)
} else {
return fmt.Sprintf("%v", bytes) return fmt.Sprintf("%v", bytes)
}
return "[]"
}) })
if len(bytes) < 1 { if len(bytes) < 1 {
@ -102,7 +108,18 @@ func decodeSysExMessage(d *decode.D, ctx *context) {
func decodeSysExContinuation(d *decode.D, ctx *context) { func decodeSysExContinuation(d *decode.D, ctx *context) {
d.FieldStrFn("data", func(d *decode.D) string { d.FieldStrFn("data", func(d *decode.D) string {
data := vlf(d) var data []uint8
var err error
d.FieldStrFn("bytes", func(d *decode.D) string {
if data, err = vlf(d); err != nil {
d.Errorf("%v", err)
} else {
return fmt.Sprintf("%v", data)
}
return "[]"
})
if len(data) > 0 && data[len(data)-1] == 0xf7 { if len(data) > 0 && data[len(data)-1] == 0xf7 {
ctx.casio = false ctx.casio = false
@ -120,7 +137,18 @@ func decodeSysExContinuation(d *decode.D, ctx *context) {
func decodeSysExEscape(d *decode.D, ctx *context) { func decodeSysExEscape(d *decode.D, ctx *context) {
d.FieldStrFn("data", func(d *decode.D) string { d.FieldStrFn("data", func(d *decode.D) string {
data := vlf(d) var data []uint8
var err error
d.FieldStrFn("bytes", func(d *decode.D) string {
if data, err = vlf(d); err != nil {
d.Errorf("%v", err)
} else {
return fmt.Sprintf("%v", data)
}
return "[]"
})
return fmt.Sprintf("%v", data) return fmt.Sprintf("%v", data)
}) })