Bug #22547
closed`String.new("", encoding: Encoding::CESU_8)` fails the STR_EMBEDDABLE_P assertion “too big termlen: 6” on debug builds
Description
The Rails nightly CI against Ruby master (debug build) started aborting in Active Support with Assertion Failed: STR_EMBEDDABLE_P:termlen <= 4: too big termlen: 6. It bisects to 346d1f63b7 ("Ignore negative capacity", https://github.com/ruby/ruby/pull/19887).
https://buildkite.com/rails/rails-nightly/builds/4922#01a1d6b6-024b-49f3-95b6-6afe9c6328e8/L2134
The full log of that Active Support job is attached (rails-nightly_build_4922_activesupport-master-debug.log.zip).
It can be reproduced with a single Active Support test:
cd activesupport && bin/test test/transliterate_test.rb -n test_transliterate_handles_strings_with_incompatible_encodings
Steps to reproduce¶
Build Ruby with cppflags=-DRUBY_DEBUG=1, then:
Expected behavior¶
$ ruby -v
ruby 4.1.0dev (2026-11-13T22:47:35Z HEAD 293c4f9246) +PRISM [x86_64-linux]
$ ruby rep.rb
OK CESU-8
Actual behavior¶
$ ruby -v
ruby 4.1.0dev (2026-11-13T22:58:20Z HEAD 346d1f63b7) +PRISM [x86_64-linux]
$ ruby rep.rb
string.c:281: Assertion Failed: STR_EMBEDDABLE_P:termlen <= 4: too big termlen: 6
ruby 4.1.0dev (2026-11-13T22:58:20Z HEAD 346d1f63b7) +PRISM [x86_64-linux]
-- Control frame information -----------------------------------------------
c:0002 p:0016 s:0011 E:002280 l:n b:---- r:(nil) EVAL rep.rb:1 [FINISH]
c:0001 p:0000 s:0003 E:000f38 l:y b:---- r:(nil) DUMMY [FINISH]
Files
Updated by yahonda (Yasuo Honda) 9 days ago
346d1f63b7 only added the assertion. To find where the terminator length of 6 comes from, I added the same check to STR_EMBEDDABLE_P by hand and built the two commits around 14285dee21 ("Optimize String.new(capacity:)", https://github.com/ruby/ruby/pull/19674) with cppflags=-DRUBY_DEBUG=1:
| Commit | Result of String.new("", encoding: Encoding::CESU_8) |
|---|---|
| af71c7aeb7 (parent of 14285dee21) | OK CESU-8 |
| 14285dee21 | Assertion Failed: STR_EMBEDDABLE_P:termlen <= 4: too big termlen: 6 |
rb_str_alloc_enc_capa, added by 14285dee21, takes the terminator length from rb_enc_mbmaxlen(enc), and CESU-8 has a max byte length of 6.
Updated by nobu (Nobuyoshi Nakada) 8 days ago
- Status changed from Open to Closed
Applied in changeset git|98c72aae69408150820bf91b1dff07ab2b026e68.
[Bug #22547] Remove the unnecessary assertion