Skip to content

GEMDRIVE: return the right last byte on an odd-sized read - #24

Open
Chati-SidecarTridge wants to merge 1 commit into
sidecartridge:mainfrom
Chati-SidecarTridge:fix/gemdrive-odd-read-last-byte
Open

Chati-SidecarTridge wants to merge 1 commit into
sidecartridge:mainfrom
Chati-SidecarTridge:fix/gemdrive-odd-read-last-byte

Conversation

@Chati-SidecarTridge

Copy link
Copy Markdown

Fixes #21.

The bug

handleReadBuffCall() swaps the chunk's byte pairs for the m68k, but rounds the count down:

CHANGE_ENDIANESS_BLOCK16(dst, bytesRead & ~1u);

The ST copies bytesRead bytes from the window. On an odd count, its last byte (even offset bytesRead - 1) comes from the other half of the final, unswapped pair, i.e. dst[bytesRead] on the RP, which f_read() did not write. So Fread() returns the right count but a stale last byte, left over from an earlier read. Even-sized reads are fine.

Effect: any odd-length file read to its end gets a wrong last byte. Programs (.PRG) of odd length crash with bombs. A customer reported the same thing by email (3 bombs on odd-length PRGs, workaround: pad the file with one $00 byte).

The fix

Round up instead, as handleWriteBuffCall() already does for the other direction:

CHANGE_ENDIANESS_BLOCK16(dst, (bytesRead + 1) & ~1u);

The extra byte swapped is dst[bytesRead], still inside the window: an odd bytesRead is at most GEMDRIVE_READ_BUFFER_SIZE - 1 (4095), and the buffer is 4096 bytes. The ST only copies bytesRead bytes, so the garbage now in the unused half of the last pair is never read.

I checked the other swaps in gemdrive.c: writeAppFreeBytesSwapped() is only called with 14 bytes, and readPayloadAtariPath() rounds down on purpose for a NUL-terminated string. Neither has the problem.

Checked

  • rp/build.sh pico_w release builds clean with ARM GNU Toolchain 14.2.rel1, no new warnings. target_firmware.h unchanged (RP-side change only).
  • Not tested on hardware. Suggested check: the reproduction in GEMDRIVE: Fread() returns a wrong last byte for odd-sized reads #21 (Fread 2 bytes of B.BIN, then 1 byte of A.BIN, expect A.BIN's first byte), and running an odd-length PRG from GEMDRIVE without padding.

handleReadBuffCall() swapped the chunk's byte pairs rounding the count
down, so on an odd count the pair holding the ST's last byte stayed
unswapped and Fread() returned a stale byte from an earlier read.
Round up, as handleWriteBuffCall() already does.

Fixes sidecartridge#21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GEMDRIVE: Fread() returns a wrong last byte for odd-sized reads

1 participant