Skip to content

Return nil from String#unpack1 when nothing is unpacked - #10034

Merged
headius merged 1 commit into
jruby:jruby-10.0from
zev:unpack1-nil-no-value
Nov 11, 2026
Merged

headius merged 1 commit into
jruby:jruby-10.0from
zev:unpack1-nil-no-value

Conversation

@zev

@zev zev commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

String#unpack1 hands Java null back to Ruby when the format has no directives. CRuby returns nil. This was split out of #10023 during review, and it targets jruby-10.0 because 10.0 has the same bug.

Script CRuby 4.0 JRuby before JRuby after
puts "abc".unpack1("").inspect nil NullPointerException nil

Changes

  • unpackInternal returns null in UNPACK_1 mode when no directive produced a value. unpack1WithBlock now returns nil in that case.
  • The existing directives already return nil themselves when they unpack nothing: "".unpack1("C*") was nil before this change too. So only a format with no directives, such as "" or " ", reached this path. On ruby-4.1, the new R* directive (Add LEB128 pack and unpack directives R and r (Feature #21785) #10036) reaches it as well.
  • The bug went unnoticed because the interpreter treats the null as nil. Only compiled code fails: -X-C prints nil, while the default mode and -X+C raise.

Testing

  • spec/ruby/core/string/unpack1_spec.rb has a new example, "returns nil if the format unpacks no value":
    • Without this fix it errors under forced JIT (-X+C, as in spec:ruby:fast:jit) and passes in the interpreter.
    • With the fix it passes in both modes.
  • spec/ruby/core/array/pack, spec/ruby/core/string/unpack, unpack_spec.rb, unpack1_spec.rb: 3188 examples, 0 failures, 0 errors.
  • test/mri/ruby/test_pack.rb: 41 tests, 0 failures.

Not in this PR

  • String#unpack with a block returns Java null the same way. On ruby-4.1, x = "AB".unpack("C") { }; x.inspect raises a NullPointerException. RubyString#unpack is declared to return RubyArray, so fixing it changes a public Java signature. I'll raise that separately.

🤖 Generated with Claude Code

unpackInternal returns Java null in UNPACK_1 mode when no directive
produced a value, and unpack1 passed that null on to Ruby. The existing
directives return nil themselves when they unpack nothing, so this only
happened for a format with no directives, such as "abc".unpack1("") or
a blank format. CRuby returns nil.

The interpreter treats the null as nil, so the bug only shows in
compiled code: "abc".unpack1("").inspect raises a NullPointerException
there. The new spec fails under forced JIT without this fix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@headius
headius merged commit c6aa865 into jruby:jruby-10.0 Nov 11, 2026
127 checks passed
@headius headius added this to the JRuby 10.2.0.0 milestone Nov 11, 2026
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.

2 participants