mirror of
https://github.com/simh/simh.git
synced 2026-05-16 18:16:53 +00:00
This problem was discovered by Codex initiated by Perry Metzger.
sim_fwrite() in sim_fio.c has a bug in the big-endian/swapped write
path. When a write spans more than one internal flip buffer, the
function advances its source pointer by size * count on every chunk
instead of by size * c, where c is the number of elements actually
written in that chunk.
This causes later chunks to read from the wrong part of the caller's
buffer, which corrupts the file contents.
Why this is wrong
sim_fwrite() may break the caller's write into multiple pieces:
nelem is the number of elements that fit in the flip buffer
nbuf is the number of chunks to write
c is the number of elements in the current chunk
After writing one chunk, the source pointer must advance by the
size of that chunk:
sptr = sptr + size * c;
But the original code advanced by the size of the entire original request
every time:
sptr = sptr + size * count;
That is only correct if there is exactly one chunk.
104 KiB
104 KiB