Skip to content

Commit 9380287

Browse files
committed
allow non-null post tree pre block padding
1 parent 9149ab6 commit 9380287

2 files changed

Lines changed: 37 additions & 7 deletions

File tree

asdf/_block/reader.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,27 @@ def _read_blocks_serially(fd, memmap=False, lazy_load=False, validate_checksums=
124124
blocks = []
125125
buff = b""
126126
magic_len = len(constants.BLOCK_MAGIC)
127+
first_magic = constants.BLOCK_MAGIC[0]
128+
129+
if not after_magic:
130+
# seek until the first magic is found
131+
# since the first magic has not been found
132+
# check for padding
133+
while True:
134+
buff += fd.read(magic_len - len(buff))
135+
if len(buff) != magic_len:
136+
# ran out of bytes
137+
return blocks
138+
if buff == constants.BLOCK_MAGIC:
139+
# we found the block magic
140+
buff = b""
141+
after_magic = True
142+
break
143+
# strip first byte
144+
buff = buff[1:]
145+
# if the first magic byte is not in the buffer clear it
146+
if first_magic not in buff:
147+
buff = b""
127148
while True:
128149
# the expectation is that this will begin PRIOR to the block magic
129150
# read 4 bytes
@@ -164,12 +185,12 @@ def _read_blocks_serially(fd, memmap=False, lazy_load=False, validate_checksums=
164185
buff = b""
165186
after_magic = False
166187
else:
167-
if len(blocks) or buff[0] != 0:
168-
# if this is not the first block or we haven't found any
169-
# blocks and the first byte is non-zero
188+
if len(blocks):
189+
# padding between blocks is not allowed
170190
msg = f"Invalid bytes while reading blocks {buff}"
171191
raise OSError(msg)
172-
# this is the first block, allow empty bytes before block
192+
193+
# this is the first block, allow padding before the block
173194
buff = buff.strip(b"\0")
174195
return blocks
175196

asdf/_tests/_block/test_reader.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,18 @@ def test_read(tmp_path, lazy_load, memmap, with_index, validate_checksums, paddi
7878
assert r[0].cached_data is r[0].cached_data
7979

8080

81-
def test_read_invalid_padding():
82-
with gen_blocks(padding=1, padding_byte=b"\1") as (fd, check):
83-
with pytest.raises(OSError, match="Invalid bytes.*"):
81+
@pytest.mark.parametrize("padding", (1, 4, 7))
82+
@pytest.mark.parametrize("padding_byte", (b"\1", b"\0", b" ", b"\xd3", b"B", b"L", b"K", b"\xd3BL"))
83+
def test_read_valid_padding(padding, padding_byte):
84+
"""Test that reader allows padding bytes before the first block"""
85+
with gen_blocks(padding=padding, padding_byte=padding_byte) as (fd, check):
86+
check(read_blocks(fd))
87+
88+
89+
@pytest.mark.parametrize("padding_byte", (b"\xd3BLK", b" \xd3BLK"))
90+
def test_read_invalid_padding(padding_byte):
91+
with gen_blocks(padding=1, padding_byte=padding_byte) as (fd, check):
92+
with pytest.raises(ValueError, match="buffer is smaller than requested size"):
8493
check(read_blocks(fd))
8594

8695

0 commit comments

Comments
 (0)