Skip to content

Commit 1f6dd90

Browse files
jhawthorncomposerinteraliamatthewd
committed
Fix update_coderange for binary strings
Although a binary (aka ASCII-8BIT) string will never have a broken coderange, it still has to differentiate between "valid" and "7bit". On Ruby 3.4/trunk this problem is masked because we now clear the coderange more agressively in rb_str_resize, and we happened to always be strinking this string, but we should not assume that. On Ruby 3.3 this created strings where `ascii_only?` was true in cases it shouldn't be as well as other problems. Fixes [Bug #20883] Co-authored-by: Daniel Colson <[email protected]> Co-authored-by: Matthew Draper <[email protected]>
1 parent 51ffef2 commit 1f6dd90

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

sprintf.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,7 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
247247
}
248248

249249
#define update_coderange(partial) do { \
250-
if (coderange != ENC_CODERANGE_BROKEN && scanned < blen \
251-
&& rb_enc_to_index(enc) /* != ENCINDEX_ASCII_8BIT */) { \
250+
if (coderange != ENC_CODERANGE_BROKEN && scanned < blen) { \
252251
int cr = coderange; \
253252
scanned += rb_str_coderange_scan_restartable(buf+scanned, buf+blen, enc, &cr); \
254253
ENC_CODERANGE_SET(result, \

test/ruby/test_sprintf.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,4 +546,12 @@ def test_negative_width_overflow
546546
sprintf("%*s", RbConfig::LIMITS["INT_MIN"], "")
547547
end
548548
end
549+
550+
def test_binary_format_coderange
551+
1.upto(500) do |i|
552+
str = sprintf("%*s".b, i, "\xe2".b)
553+
refute_predicate str, :ascii_only?
554+
assert_equal i, str.bytesize
555+
end
556+
end
549557
end

0 commit comments

Comments
 (0)