Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions mrbgems/mruby-encoding/test/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@
assert_false "\xfe".valid_encoding?
assert_false "あ\xfe".valid_encoding?
assert_true "あ\xfe".b.valid_encoding?
# RFC 3629 restrictions
assert_false "\xC0\x80".valid_encoding? # overlong NUL
assert_false "\xC1\xBF".valid_encoding? # overlong (< U+0080)
assert_false "\xE0\x9F\xBF".valid_encoding? # overlong (< U+0800)
assert_false "\xED\xA0\x80".valid_encoding? # surrogate U+D800
assert_false "\xED\xBF\xBF".valid_encoding? # surrogate U+DFFF
assert_false "\xF0\x8F\xBF\xBF".valid_encoding? # overlong (< U+10000)
assert_false "\xF4\x90\x80\x80".valid_encoding? # above U+10FFFF
assert_false "\xF5\x80\x80\x80".valid_encoding? # above U+10FFFF
assert_true "\u{D7FF}".valid_encoding? # last code point before surrogates
assert_true "\u{E000}".valid_encoding? # first code point after surrogates
assert_true "\u{10FFFF}".valid_encoding? # largest valid code point
else
assert_true "\xfe".valid_encoding?
end
Expand Down
20 changes: 20 additions & 0 deletions src/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,26 @@ mrb_utf8len(const char* p, const char* e)
case 2:
if (utf8_islead(p[1])) return 1;
}
/* Reject overlong sequences, UTF-16 surrogates, and code points above
U+10FFFF (RFC 3629, Unicode D93b). */
switch ((unsigned char)p[0]) {
case 0xC0: case 0xC1: /* overlong (< U+0080) */
return 1;
case 0xE0: /* overlong (< U+0800) */
if ((unsigned char)p[1] < 0xA0) return 1;
break;
case 0xED: /* surrogate (U+D800..U+DFFF) */
if ((unsigned char)p[1] > 0x9F) return 1;
break;
case 0xF0: /* overlong (< U+10000) */
if ((unsigned char)p[1] < 0x90) return 1;
break;
case 0xF4: /* above U+10FFFF */
if ((unsigned char)p[1] > 0x8F) return 1;
break;
case 0xF5: case 0xF6: case 0xF7: /* above U+10FFFF */
return 1;
}
return len;
}

Expand Down
14 changes: 14 additions & 0 deletions test/t/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,20 @@ def [](*args)
assert_equal 2, str[1, 2].size
end if UTF8STRING

assert('String#size(UTF-8) counts invalid sequences per byte') do
# RFC 3629: overlong forms, UTF-16 surrogates, and code points above
# U+10FFFF are not characters, so each of their bytes counts on its own
assert_equal 2, "\xC0\x80".size # overlong NUL
assert_equal 3, "\xE0\x9F\xBF".size # overlong (< U+0800)
assert_equal 3, "\xED\xA0\x80".size # surrogate U+D800
assert_equal 4, "\xF0\x8F\xBF\xBF".size # overlong (< U+10000)
assert_equal 4, "\xF4\x90\x80\x80".size # above U+10FFFF
assert_equal 4, "\xF5\x80\x80\x80".size # above U+10FFFF
assert_equal 1, "\u{D7FF}".size # last code point before surrogates
assert_equal 1, "\u{E000}".size # first code point after surrogates
assert_equal 1, "\u{10FFFF}".size # largest valid code point
end if UTF8STRING

assert('String#slice', '15.2.10.5.34') do
# length of args is 1
a = 'abc'.slice(0)
Expand Down
Loading