Skip to content

Commit d248339

Browse files
committed
[Bug #18956] Negative codepoints are invalid characters
1 parent 8f4a53d commit d248339

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

sprintf.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,8 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
447447
goto format_s1;
448448
}
449449
else {
450-
c = NUM2INT(val);
451-
n = rb_enc_codelen(c, enc);
450+
n = NUM2INT(val);
451+
if (n >= 0) n = rb_enc_codelen((c = n), enc);
452452
}
453453
if (n <= 0) {
454454
rb_raise(rb_eArgError, "invalid character");

test/ruby/test_sprintf.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ def test_char
368368
assert_equal(" " * (BSIZ - 1) + "a", sprintf(" " * (BSIZ - 1) + "%-1c", ?a))
369369
assert_equal(" " * BSIZ + "a", sprintf("%#{ BSIZ + 1 }c", ?a))
370370
assert_equal("a" + " " * BSIZ, sprintf("%-#{ BSIZ + 1 }c", ?a))
371+
assert_raise(ArgumentError) { sprintf("%c", -1) }
371372
end
372373

373374
def test_string

0 commit comments

Comments
 (0)