[Bug #20883] Fix coderange for sprintf on binary strings (#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 <danieljamescolson@gmail.com>
Co-authored-by: Matthew Draper <matthew@trebex.net>
This commit is contained in:
John Hawthorn 2024-11-09 01:00:13 -08:00 committed by GitHub
parent 75015d4c1f
commit a51a6bf692
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 9 additions and 2 deletions

View file

@ -247,8 +247,7 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
}
#define update_coderange(partial) do { \
if (coderange != ENC_CODERANGE_BROKEN && scanned < blen \
&& rb_enc_to_index(enc) /* != ENCINDEX_ASCII_8BIT */) { \
if (coderange != ENC_CODERANGE_BROKEN && scanned < blen) { \
int cr = coderange; \
scanned += rb_str_coderange_scan_restartable(buf+scanned, buf+blen, enc, &cr); \
ENC_CODERANGE_SET(result, \