Repository navigation
vm: keep #ungetc's bytes out of the file, and count them in #pos - #977
Merged
Merged
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>
When more is pushed back than was read, io_unread's lseek fails and the stream stays where its descriptor is. #977's first commit put that at end-of-file whenever anything had been read, which holds only after a buffered read (getc, gets, getbyte, readchar): those fill rbuf ahead, and MRI reads a file of up to 8192 bytes whole. IO#read and #readpartial on an empty rbuf read straight into the string (io_bufread, io_getpartial), so the descriptor is at the cursor: f.read(1); f.ungetc("XYZ"); f.write("Q") writes Q at offset 1 under MRI 4.0.7; rbgo appended it. The stream now keeps fdAt, the descriptor's offset while rbuf holds nothing. Every operation that empties rbuf sets it to the cursor: a discarding unread, a seek, #pos=, #rewind, a write, and a direct read. A buffered read moves the cursor past it, and then the descriptor is at end-of-file. A read that starts with pushback in rbuf is not direct: io_bufread takes the pushback and refills with io_fillbuf. Two neighbours found by the same probes: - File#size on a writable stream runs flush_before_seek, whose io_unread drops the pushback (rb_file_size); rbgo kept it. - #readpartial and #read_nonblock return only what rbuf holds while pushback is left (io_getpartial); rbgo read on into the file. Also the coverage gate that failed #977 on ubuntu and macOS: diskBytes' copy branch was reached by no test. "a pending write is flushed without the pushback" reaches it through #flush. Measured with rubydiff against MRI 4.0.7 (the installed one; the io_unread read is from the 4.0.9 source): 48 new probe rows across 16 read/seek/write sequences, plus the earlier #974 probes, all agree except the known 8192-byte capacity row. 11 rows added to TestUngetcOnAFileIsNotTheFiles. Refs #974 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tannevaled
added a commit
to go-embedded-ruby/docs
that referenced
this pull request
Nov 22, 2026
…the opened file (#55) * conformance: IO rows for #pwrite on disk, #ungetc, flush ranges, opened-file identity The #pwrite row said its bytes are not on disk until a flush; since go-embedded-ruby/ruby#975 they are, as with pwrite(2). It is rewritten to the ruby README's current row, with the two deliberate bounds. Three rows are added, each mirroring the ruby README: - #ungetc keeps pushed-back bytes out of the file (go-embedded-ruby/ruby#977); the divergence left is MRI's 8192-byte read buffer. - a flush writes only the ranges this stream wrote (go-embedded-ruby/ruby#978); the divergence left is the 256-range bound. - a stream writes only into the file it opened, never into what its path names after an unlink, rename or symlink swap (go-embedded-ruby/ruby#978); the divergences left are the orphaned inode and the fifo window. Refs go-embedded-ruby/ruby#974 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * conformance: the opened-file row keeps its inode alive, and #truncate keeps the open's grant Follows go-embedded-ruby/ruby#978: on Unix a file stream holds a descriptor on the file it opened, so a reused inode number cannot pass for it, and #truncate on a file chmodded read-only after the open succeeds as in MRI 4.0.7. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- 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.Second commit: where a failed unread leaves the stream
The first CI run was red on the coverage ratchet: no test reached
diskBytes's copy branch. Writing a test for that branch exposed a defect in the first commit. When the logical position is negative,io_unread'slseekfails and the stream stays where its descriptor is. The first commit put that at end-of-file whenever anything had been read. That holds only after a buffered read (getc,gets,getbyte,readchar), which fillsrbufahead of the cursor.IO#readand#readpartialon an emptyrbufread straight into the string (io_bufread,io_getpartial), so the descriptor is at the cursor. Withf.read(1); f.ungetc("XYZ"); f.write("Q"), MRI writesQat offset 1, and rbgo appended it.fdAt, the descriptor's offset whilerbufholds nothing. A discarding unread, a seek,#pos=,#rewind, a write and a direct read set it to the cursor. A read that starts with pushback inrbufis not direct:io_bufreadtakes the pushback, then refills withio_fillbuf.File#sizeon a writable stream drops the pushback (rb_file_size→flush_before_seek→io_unread).#readpartialand#read_nonblockreturn no more than the pushback while some is left (io_getpartial).Measured:
rubydiffagrees with MRI 4.0.7 on 48 new probe rows (16 sequences × flush, write andSEEK_CUR). Every earlier probe still agrees, apart from the known 8192-byte capacity row.TestUngetcOnAFileIsNotTheFileshas 11 new rows, and every function inio_pushback.gois at 100 % coverage.mutsweepcaught 8/8. A hand-madeif true || wasAtmutant inreadThroughturned exactly its row red. Theinternal/vmsuite isok(457 s), andbuildscan --vetpassed.🤖 Generated with Claude Code