Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

index out of range panic in ErrorCode() #9

Open
juhaszp-uhu opened this issue Jan 11, 2022 · 1 comment
Open

index out of range panic in ErrorCode() #9

juhaszp-uhu opened this issue Jan 11, 2022 · 1 comment

Comments

@juhaszp-uhu
Copy link

juhaszp-uhu commented Jan 11, 2022

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/[email protected]/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:

  • check the length in ErrorCode() - I think this is easy and harmless
  • if possible, introduce a new error code to signify that the packet is invalid, or signal the error in some other way.
@vcidst
Copy link

vcidst commented Mar 22, 2022

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

// ErrorCode returns the coded error of the message, if present
func (m Message) ErrorCode() ErrorCode {
if m.Kind() != KindError {
return ErrNone
}
// FIXME: TBD
return ErrorCode(m[3])
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants