Skip to content

pack/unpack when the string, format or buffer changes during the call #10022

Open

Description

@zev

CRuby 4.1 added checks for strings that change during pack/unpack. Most of them fix crashes in C: use-after-free bugs, HackerOne reports and Bug #22315. JRuby doesn't crash in any of these cases. It keeps working on the bytes it started with, so it returns a different result instead of raising:

Case CRuby 4.1 JRuby
#to_str clears the format during pack RuntimeError: format string modified uses the old format
the unpack block clears or grows the string RuntimeError: string modified decodes the old bytes
the unpack block clears the format RuntimeError: format string modified uses the old format
[s].pack("a*", buffer: s) ArgumentError: cannot pack buffer object into itself "abab"
#to_int calls buf.clear during pack(buffer: buf) output goes into the cleared buffer: "\x01*" output is lost: ""

The last case happens because packCommon keeps buffer.getByteList() from the start, and String#clear gives the string a new ByteList. Matching CRuby would mean fetching the buffer's ByteList again after every call into Ruby code (to_int/to_str/to_f), which touches most directives.

Syncing test_pack.rb from CRuby master needs these five excludes in test/mri/excludes/TestPack.rb. The sync will be its own PR, after the pack/unpack feature PRs for #9746 (#10023, #10035, #10036, #10037) land:

  • test_format_string_modified_by_to_str
  • test_unpack_with_block_modifying_string
  • test_unpack_with_block_modifying_format
  • test_pack_with_buffer
  • test_pack_modify_buffer_r_R

test_pack_with_buffer passed before the test sync, which added the "pack into itself" assertions to it. Excluding the whole test also hides its older buffer: assertions, which JRuby still passes. If we add the "into itself" check, that exclude can go.

Do we want CRuby's errors here, or is the snapshot behavior fine for JRuby? If we want them, the four RuntimeError/ArgumentError checks are cheap: compare the byte array and length after each callback. The buffer case is the larger change.

Activity

  1. zev commented on Nov 12, 2026

    @zev
    ContributorAuthor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions