Skip to content

Commit a51a6bf

Browse files
jhawthorncomposerinteraliamatthewd
authored
[Bug #20883] Fix coderange for sprintf on binary strings (ruby#12040)
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 75015d4 commit a51a6bf

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
@@ -543,4 +543,12 @@ def test_negative_width_overflow
543543
sprintf("%*s", RbConfig::LIMITS["INT_MIN"], "")
544544
end
545545
end
546+
547+
def test_binary_format_coderange
548+
1.upto(500) do |i|
549+
str = sprintf("%*s".b, i, "\xe2".b)
550+
refute_predicate str, :ascii_only?
551+
assert_equal i, str.bytesize
552+
end
553+
end
546554
end

0 commit comments

Comments
 (0)