Repository navigation
vm: keep #ungetc's bytes out of the file, and count them in #pos - #977
Open
tannevaled wants to merge 1 commit into
Open
tannevaled wants to merge 1 commit into
tannevaled wants to merge 1 commit into
Conversation
A File holds its content in buf, and #ungetc spliced the pushed-back bytes into it with no record of where they were. A later buffered write flushed buf whole, so the bytes reached the disk, and the cursor ignored them: MRI's position is the descriptor's offset minus what the read buffer still holds, so every write after an ungetc landed late. io_pushback.go records the region the bytes occupy and implements io.c's io_unread: writes, #flush, #seek, #rewind, #pos=, #truncate, #syswrite and #pwrite drop the pushback and move to the logical position; #pos (io_tell) drops it when that position is valid and answers it negative otherwise; #sysread and #sysseek refuse "for buffered IO"; flushes, #pread's fallback and File#size see the file without it. IO#reopen and set_encoding_by_bom, which replace the buffer, reset the region, and reopen(io) adopts the file's bytes, not the other stream's pushback. StringIO keeps its own semantics, now MRI's: strio_unget_bytes writes the pushback into the string over the bytes just read, where it was spliced in like a file's. Refs #974 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
IO#ungetc/#ungetbyteon aFilenow behave as MRI's read buffer does: the pushed-back bytes are never written to the file, and the position counts them. Refs #974 (itsungetcitem; thepreadflush item and the TOCTOU item stay open).Why
A
Fileholds its content inbuf, and#ungetcspliced the bytes into it with no record of where they were. Two defects followed.bufwhole, so the pushed-back bytes reached the disk.getc; ungetc "Z"; seek(0, :END); write "x"left"0Z123…x"in the file.rb_io_tell:pos -= fptr->rbuf.len), so every write after anungetclanded late.getc; ungetc "Z"; write "AB"gave"0AB23456789", where MRI 4.0.7 gives"AB23456789".How
internal/vm/io_pushback.gorecords the region[ungetLo, ungetHi)the bytes occupy and implementsio_unreadfromio.c:rb_io_check_writable),#flush,#seek,#rewind,#pos=,#truncate,#syswriteand#pwritedrop the pushback. The cursor then moves to the logical position. When that position is negative, it moves to where the read-ahead stopped.#pos(io_tell,discard_rbuf = false) drops the pushback only when the position is valid. Otherwise it keeps the pushback and answers the negative position, as MRI does.#sysreadand#sysseekraiseIOError … for buffered IO(READ_DATA_BUFFERED).#pread's buffer fallback andFile#sizesee the file without the pushback.#reopenandset_encoding_by_bomreplace the buffer, so they reset the region.reopen(io)adopts the file's bytes, not the other stream's pushback.StringIO keeps its own semantics, and those are now MRI's.
ext/stringiostrio_unget_byteswrites the pushback into the string, over the bytes just read. rbgo had spliced it in like a file's:StringIO.new("0123"), thengetc,ungetc "Z",write "AB"gave"0AB23"at pos 1 instead of"AB23"at pos 0.Measured
rubydiffon 44 probe rows (33 File, 8 StringIO, 3 reopen/empty), all AGREE. One exception, documented in the README divergences table: on a file larger than 8192 bytes, MRI refuses pushback beyond what was read (IOError: ungetbyte failed,io_ungetbyte's capacity check). rbgo accepts it.TestUngetcOnAFileIsNotTheFiles(22 rows),TestStringIOUngetcWritesIntoTheString(9),TestReopenDropsThePushback, plus two internal tests. One holds the defensive guard incompactPushback, the other assertsdiskBytesdoes not allocate without pushback.internal/vmsuite:ok, 286 s.io_pushback.go:mutsweepcaught 7/7. The first sweep's five survivors were read one by one: two redundant guards were removed, and three got tests.for buffered IOrefusals:mutsweepdoes not list them as refusals (body is araise(...)call). A hand-madefalse &&mutant of each turned exactly its own subtest red.buildscan --vet: go vet passed under all 19 CI configurations.🤖 Generated with Claude Code