fix up some OSC string parsing issues and test for them

This commit is contained in:
Joel Wetzell
2026-04-13 18:17:43 -05:00
parent fbd2bf3905
commit ad3c449149
3 changed files with 31 additions and 7 deletions
+8 -3
View File
@@ -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)
+16 -2
View File
@@ -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 {
+7 -2
View File
@@ -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:]