bincopy icon indicating copy to clipboard operation
bincopy copied to clipboard

`fill()` silently corrupts data if fill pattern is larger than any gap

Open dorkeline opened this issue 2 months ago • 0 comments

Given a simple test file bfp as:

import bincopy

bfp = bincopy.BinFile(word_size_bits=8)
bfp.add_binary(b"cafe", 0)
bfp.add_binary(b"babe", 5)

our segments look like

Segment(address=0, data=bytearray(b'cafe'))
Segment(address=5, data=bytearray(b'babe'))
bytearray(b'cafe\xffbabe')

Filling the single byte gap works as expected:

bfp.fill(b" ")
Segment(address=0, data=bytearray(b'cafe babe'))
bytearray(b'cafe babe')

but trying to fill with a value larger than a gap will corrupt the file by filling the non-empty region too.

For example:

bfp.fill(b" -")

will push the second segment and truncate it silently:

Segment(address=0, data=bytearray(b'cafe -babe'))
bytearray(b'cafe -bab')

Version

20.1.0

Expected behaviour

I'm not entirely sure, for my specific usecase (filling empty regions in a microchip pic30-series hex file with the b"\xff\xff\xff\x00" pattern that represents it written to flash so a checksum on the python side can be calculated even across gaps) i would want fill to behave as if there was a magic first layer across the entire region that would start at 0 and repeat the pattern over and over - and then the gaps (the ones smaller than max_words) would just be filled with the part of the pattern that appears at this location. So in the above example i would expect the gap to be just b" " because that would be what would be at the gap's location if the fill pattern was actually a repeating segment underneath.

I can also understand if this would be considered out of scope/too messy to do/different expectations for different people and in that case i believe an assertion should be thrown instead of silent data corruption.

dorkeline avatar Dec 11 '25 14:12 dorkeline