diff --git a/message.go b/message.go index 7a670ff..bd0051f 100644 --- a/message.go +++ b/message.go @@ -44,6 +44,11 @@ func MessageFromBytes(bytes []byte) (*OSCMessage, error) { Args: []OSCArg{}, } + if len(typeAndArgBytes) == 0 { + // NOTE(jwetzell): no type string return early. + return &oscMessage, nil + } + typeString, argBytes, err := readOSCString(typeAndArgBytes) if err != nil { @@ -56,9 +61,9 @@ func MessageFromBytes(bytes []byte) (*OSCMessage, error) { return nil, errors.New("type string is malformed") } } else { - oscArg, remainingBytes, error := readOSCArg(argBytes, string(oscType)) - if error != nil { - return nil, error + oscArg, remainingBytes, err := readOSCArg(argBytes, string(oscType)) + if err != nil { + return nil, err } argBytes = remainingBytes oscMessage.Args = append(oscMessage.Args, oscArg) diff --git a/message_test.go b/message_test.go index 7295afe..0a5fb44 100644 --- a/message_test.go +++ b/message_test.go @@ -298,18 +298,32 @@ func TestBadOSCMessageDecoding(t *testing.T) { { name: "address string not padded", bytes: []byte{47, 104, 101, 108, 108, 111, 0}, - errorString: "string data is not properly padded", + errorString: "OSC string is not properly padded", }, { name: "type string not padded", bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 0}, - errorString: "string data is not properly padded", + errorString: "OSC string is not properly padded", }, { name: "type string does not start with ,", bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 45, 0, 0, 0}, errorString: "type string is malformed", }, + { + name: "string arg not null-terminated", + bytes: []byte{ + 47, 104, 101, 108, 108, 111, 0, 0, 44, 115, 0, 0, 97, 114, 103, 49, + }, + errorString: "OSC string must be null-terminated", + }, + { + name: "string arg not padded", + bytes: []byte{ + 47, 104, 101, 108, 108, 111, 0, 0, 44, 115, 0, 0, 104, 105, 0, + }, + errorString: "OSC string is not properly padded", + }, } for _, testCase := range testCases { diff --git a/osc.go b/osc.go index 3e454fc..3b80921 100644 --- a/osc.go +++ b/osc.go @@ -172,18 +172,23 @@ func argsToBuffer(args []OSCArg) []byte { } func readOSCString(bytes []byte) (string, []byte, error) { - //TODO(jwetzell): add error handling oscString := "" stringEndIndex := 0 + nullByteFound := false for index, byteIn := range bytes { if byteIn == 0 { + nullByteFound = true oscString = string(bytes[0:index]) stringEndIndex = index + 1 break } } + if !nullByteFound { + return "", bytes, errors.New("OSC string must be null-terminated") + } + stringPadding := 4 - (stringEndIndex % 4) if stringPadding < 4 { @@ -191,7 +196,7 @@ func readOSCString(bytes []byte) (string, []byte, error) { } if stringEndIndex > len(bytes) { - return "", bytes, errors.New("string data is not properly padded") + return "", bytes, errors.New("OSC string is not properly padded") } remainingBytes := bytes[stringEndIndex:]