Repository navigation
Return nil from String#unpack1 when nothing is unpacked - #10034
Merged
Merged
Conversation
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>
This was referenced Nov 11, 2026
headius
approved these changes
Nov 11, 2026
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.
String#unpack1hands Javanullback to Ruby when the format has no directives. CRuby returnsnil. This was split out of #10023 during review, and it targetsjruby-10.0because 10.0 has the same bug.puts "abc".unpack1("").inspectnilNullPointerExceptionnilChanges
unpackInternalreturnsnullinUNPACK_1mode when no directive produced a value.unpack1WithBlocknow returnsnilin that case.nilthemselves when they unpack nothing:"".unpack1("C*")wasnilbefore this change too. So only a format with no directives, such as""or" ", reached this path. Onruby-4.1, the newR*directive (Add LEB128 pack and unpack directives R and r (Feature #21785) #10036) reaches it as well.nullasnil. Only compiled code fails:-X-Cprintsnil, while the default mode and-X+Craise.Testing
spec/ruby/core/string/unpack1_spec.rbhas a new example, "returns nil if the format unpacks no value":-X+C, as inspec:ruby:fast:jit) and passes in the interpreter.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#unpackwith a block returns Javanullthe same way. Onruby-4.1,x = "AB".unpack("C") { }; x.inspectraises aNullPointerException.RubyString#unpackis declared to returnRubyArray, so fixing it changes a public Java signature. I'll raise that separately.🤖 Generated with Claude Code