Repository navigation
Add the buffer_utf8 data type and an optional size for buffer_read and buffer_peek (GameMaker-Bugs#16485) - #809
Conversation
…d buffer_peek (GameMaker-Bugs#16485) buffer_utf8 reads and peeks one UTF-8 character as its code point (U+FFFD and one byte for a malformed sequence) and writes and pokes a code point as UTF-8. A size for buffer_text / buffer_string reads exactly that many bytes, up to the end of the buffer. The text decode is shared through yyDecodeUTF8, which also stops it reading past the end of the data. Claude-Session: https://claude.ai/code/session_01EgTASfpMoBvX7TBDy119t5
Automated reviewThis PR adds the Checked against the code:
Malformed bytes in an ordinary text read now come out as U+FFFD for each bad byte. The description states this change openly. No new problems were found and the change looks ready to merge. Nothing open. Reviewed 895e59b Advisory: this check doesn't block merging. It runs again on each push. Reply in a comment's thread if it is wrong, and resolve the thread once it is dealt with: the next review reads both. |
iampremo
left a comment
There was a problem hiding this comment.
Reviewed at b70b9c6.
I went through the read, peek, write and poke paths for buffer_utf8 and the new yyb_decodestring. The decoder looks right to me. Bounds are checked against _end. A bad lead or continuation byte takes 1 byte, so a truncated sequence before a zero byte still stops at the terminator instead of swallowing it. Sized and unsized reads advance as the description says.
The bot's buffer_fill finding is correct and still open. With buffer_utf8, typesize is 0 from BufferSizeOf, so in yyb_fill neither byteswritten nor virtualoffset ever moves forward and the while (byteswritten < _size) loop never ends. buffer_fill always passes stride -1, so every call with the new type hangs the page. This needs fixing before merge. Set typesize from yyEncodeUTF8(_value).length and bail out when typesize <= 0. That guard would also cover the existing hang when you fill with an empty buffer_text string.
I also have one question about how a wrap buffer behaves once a character no longer fits at the end (inline).
…ap buffer, argument count checks, size 0 at the end buffer_fill with buffer_utf8 does nothing (as on the C++ runner and GMRT), and a type with no size (an empty buffer_text) no longer loops forever. buffer_utf8 writes carry on at the start of a wrap buffer like the other writes. buffer_read and buffer_peek raise an error outside 2-3 and 3-4 arguments. A size of 0 reads an empty string without moving, even at the end of the buffer. Claude-Session: https://claude.ai/code/session_01EgTASfpMoBvX7TBDy119t5
As on the C++ runner, a poke into a wrap buffer carries on at offset 0; on other buffers a character that doesn't fit isn't written. Claude-Session: https://claude.ai/code/session_01EgTASfpMoBvX7TBDy119t5
iampremo
left a comment
There was a problem hiding this comment.
Reviewed at e0ad0db.
Re-reviewed at e0ad0db.
The earlier points are dealt with:
buffer_fillhang (my blocker, and the bot's): fixed.yyb_fillnow returns forbuffer_utf8and whenevertypesize <= 0, so an emptybuffer_textfill no longer hangs either.- Wrap buffer question: answered, and the code matches the reply.
yyb_writeandyyb_pokenow continue writing the character's bytes at offset 0. Tracing a 3-byte character written at offset 2 of a 4-byte wrap buffer gives[0xAC, 0, 0xE2, 0x82]with the position at 1, as you said.
The size-0 early return, the argument-count errors and the poke-wrap used-size handling all look right. No internal callers of buffer_read/buffer_peek pass the wrong number of arguments.
One new problem, inline: the wrap path in yyb_write resets the index to 0 before the used size is updated, so the used size misses the bytes written at the tail. yyb_poke already gets this right.
…nly at or before the end A buffer_utf8 write that wraps now updates the used size to the end of the buffer before going back to 0, and only wraps when bytes are still to be written, so a character that ends exactly at the end leaves buffer_tell at the buffer size like the other types. A size of 0 reads an empty string only at or before the end of the buffer; past it the read is out of bounds like any other. Claude-Session: https://claude.ai/code/session_01EgTASfpMoBvX7TBDy119t5
iampremo
left a comment
There was a problem hiding this comment.
Reviewed at 895e59b.
Re-reviewed at 895e59b. The used-size problem from my last review is fixed, and the reply's three points match the code:
- In
yyb_write, the wrap path now callsyyb_UpdateUsedSize(this.m_Size)before setting the index back to 0, asyyb_pokedoes. I traced both of my cases again:- Four
buffer_utf8writes of "A" into a new 4-byte wrap buffer leave the used size at 4, the same asbuffer_u8. - Seeking to 2 and writing U+20AC puts bytes at 2, 3 and 0, and the used size stays at 4.
- With "A" at offset 1, an unsized text read from 1 gives "A" followed by two U+FFFD for the cut-off bytes, as the reply says.
- Four
- The mid-character wrap now only happens when another byte is still to be written (
u + 1 < UTF8_Bytes.length). A character that ends exactly at the end leavesbuffer_tellat the buffer size, and the next write wraps it in the existing loop at the top ofyyb_write. That's how the fixed-size types behave. - A size-0 read now returns "" only on a wrap buffer or when the position is at or before the end. Past the end (reachable with
buffer_seek), it falls through to the normal out-of-bounds path: "" forbuffer_string,eBuffer_OutOfBoundsforbuffer_text, the same as an unsized read there. A size-0 peek past the end of a non-wrap buffer hits the existing_offset > m_Size - 0check and returnsundefined.
The bot's review is accurate on these points, and I have nothing new to raise. Looks good to merge.
HTML5 part of YoYoGames/GameMaker-Bugs#16485: the
buffer_utf8data type and an optionalsizeforbuffer_read/buffer_peek. Needs the asset compiler from YoYoGames/GameMaker#5591 (thebuffer_utf8constant, andbuffer_read/buffer_peektaking 2-3 / 3-4 arguments).Changes (
scripts/yyBuffer.js)eBuffer_UTF8 = 14.yyDecodeUTF8(dataView, offset, end): decodes one character without reading at or pastend, returning the code point, or U+FFFD with a length of 1 for a malformed sequence (a continuation byte or a byte that can't start a character, a missing or bad continuation byte, an overlong encoding, a surrogate, a value above U+10FFFF, or a sequence cut off byend).yyEncodeUTF8(value)encodes a code point, giving U+FFFD for a value that isn't one.yyb_read/yyb_peek:eBuffer_UTF8returns the code point;buffer_readadvances by the character's length. It never reads past the end of the buffer, so never across the end of a wrap buffer. At or past the end it returns what the other types return (eBuffer_OutOfBoundsfor a read,undefinedfor a peek).yyb_write/yyb_poke:eBuffer_UTF8writes the code point as 1 to 4 bytes. In a wrap buffer a write or poke carries on at the start (offset 0), as every other write does there and as the C++ runner does: the bytes at the tail count toward the used size, and a character that ends exactly at the end leavesbuffer_tellat the buffer size until the next write wraps it, like the other types; on other buffers a poke that doesn't fit isn't written. Reads still never decode across the end.yyb_fill:buffer_fillwithbuffer_utf8does nothing, as on the C++ runner and GMRT, and a type with no size (an emptybuffer_text) returns instead of looping forever.buffer_text/buffer_stringreads and peeks now shareyyb_decodestring(start, end), built onyyDecodeUTF8. With asize(0 or more) the span is[position, min(position + size, buffer size)), the string stops at the first zero byte, andbuffer_readadvances by the whole span. Without one (or with a negative one) the behaviour is as before: up to the zero byte or the used size, andbuffer_readmoves past the zero byte. A size of 0 returns an empty string without moving at or before the end of the buffer (past it, it's out of bounds like any other read); any other sized read at or past the end returns what an unsized one does there, as the C++ runner does.buffer_read(buffer, type, [size])andbuffer_peek(buffer, offset, type, [size])pass the size on (ignored for the other types), and raise an error outside 2-3 and 3-4 arguments (buffer_read: expected 2 or 3 arguments, got N, as the C++ runner words it).buffer_sizeof(buffer_utf8)is 0, likebuffer_text.Behaviour change for malformed bytes in an ordinary text read: the old decoder assumed every byte from 0xF0 up started a 4-byte character and read continuation bytes without checking them (or the end of the data). A malformed sequence now gives U+FFFD per bad byte, as browsers' own decoders do. Valid text decodes exactly as before.
Testing
GM-TestFramework
BasicBufferUtf8TestSuite(branchbuffers/gmb-16485at f83ef14, which adds the size-0-at-the-end test) on HTML5: xUnit built for HTML5 on macOS with Igor from runtime 9.9.1.1404 and the asset compiler from GameMaker#5591 (ac52c2d62e), served locally, run in headless Chrome:developrunner: 4 of 18 pass.BasicBufferTestSuitehassuite_filter: platform_not_browser, so it doesn't run on HTML5. In its place I compareddevelop's and this branch's text reads in Node on 3000 random scenarios (1 to 4 strings of mixed 1 to 4 byte characters written withbuffer_text/buffer_string, read back, plus peeks at random offsets). All reads are identical. The only differences are the 40 peeks that start on a continuation byte, wheredevelopdecoded garbage and this branch gives U+FFFD. A separate Node check of the new code (116 checks: every case in the suite, plus past-the-end reads, writes and pokes across the end of a wrap buffer, the used size andbuffer_tellafter wrapping writes, a size of 0 past the end, a poke that doesn't fit a fixed buffer,buffer_fillwithbuffer_utf8and with an emptybuffer_text, and the argument count errors) passes.