Skip to content

index out of range panic in ErrorCode() #9

@juhaszp-uhu

Description

@juhaszp-uhu

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:

  • 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions