Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions pythonosc/parsing/osc_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,21 +309,25 @@ def get_blob(dgram: bytes, start_index: int) -> Tuple[bytes, int]:

Args:
dgram: A datagram packet.
start_index: An index where the float starts in the datagram.
start_index: An index where the blob starts in the datagram.

Returns:
A tuple containing the blob and the new end index.

Raises:
ParseError if the datagram could not be parsed.
"""
size, int_offset = get_int(dgram, start_index)
# Make the size a multiple of 32 bits.
total_size = size + (-size % _BLOB_DGRAM_PAD)
end_index = int_offset + size
if end_index - start_index > len(dgram[start_index:]):
if start_index < 0:
raise ParseError("start_index < 0")
size, payload_start = get_int(dgram, start_index)
if size < 0:
raise ParseError("Blob length cannot be negative")

payload_end = payload_start + size
next_index = payload_end + (-size % _BLOB_DGRAM_PAD)
if next_index > len(dgram):
raise ParseError("Datagram is too short.")
return dgram[int_offset : int_offset + size], int_offset + total_size
return dgram[payload_start:payload_end], next_index


def write_blob(val: bytes) -> bytes:
Expand Down
14 changes: 14 additions & 0 deletions pythonosc/test/parsing/test_osc_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,20 @@ def test_get_blob(self):
for dgram, expected in cases.items():
self.assertEqual(expected, osc_types.get_blob(dgram, 0))

def test_get_blob_rejects_negative_lengths_and_missing_padding(self):
cases = [
b"\xff\xff\xff\xff",
b"\xff\xff\xff\xfc",
b"\x80\x00\x00\x00",
b"\x00\x00\x00\x01a",
b"\x00\x00\x00\x02ab\x00",
b"\x00\x00\x00\x03abc",
]
for encoded in cases:
with self.subTest(encoded=encoded):
with self.assertRaises(osc_types.ParseError):
osc_types.get_blob(b"skip" + encoded, 4)

def test_get_blob_raises_on_wrong_dgram(self):
cases = [b"", True, b"\x00\x00\x00\x08"]

Expand Down
5 changes: 5 additions & 0 deletions pythonosc/test/test_osc_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@


class TestOscMessage(unittest.TestCase):
def test_blob_cannot_rewind_to_the_previous_argument(self):
packet = b"/b\x00\x00,ibi\x00\x00\x00\x00\x00\x00\x00*\xff\xff\xff\xf8"
with self.assertRaises(osc_message.ParseError):
osc_message.OscMessage(packet)

def test_switch_goes_off(self):
msg = osc_message.OscMessage(_DGRAM_SWITCH_GOES_OFF)
self.assertEqual("/SYNC", msg.address)
Expand Down
Loading