CyCoreSystems / audiosocket

Simple bidirectional audio protocol
Apache License 2.0
75 stars 36 forks source link

index out of range panic in ErrorCode() #9

Open juhaszp-uhu opened 2 years ago

juhaszp-uhu commented 2 years ago

I've got a runtime panic while running some code that was essentially identical to the example server in this repo:

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

goroutine 10 [running]:
github.com/CyCoreSystems/audiosocket.Message.ErrorCode(...)
        /home/pjuhasz/pkg/mod/github.com/!cy!core!systems/audiosocket@v0.2.0/audiosocket.go:75

My analysis:

For some reason audiosocket.NextMessage() returned a zero length message. I don't know if there was an error, unfortunately I haven't checked it, but the example code doesn't either. Then that empty message is passed to m.Kind(), which returns KindError. This is problem number one, that a legitimate packet type is conflated with something that is more like a condition on the receiving site. Then, believing that the message is valid, it is passed to m.ErrorCode(), which then panics, because it blindly accesses m[3] without making sure that the message is long enough for that.

Suggestions:

vcidst commented 2 years ago

This is marked with a //FIXME comment already in the code,

https://github.com/CyCoreSystems/audiosocket/blob/807c189aae2b651b779b1e2ce1a17e1ecd364973/audiosocket.go#L68-L76