1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

string.c: check null char

* string.c (rb_string_value_cstr): check null char in char, not in
  byte.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41918 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2013-07-11 11:17:54 +00:00
parent be698dbc66
commit db3ddad3a7
3 changed files with 44 additions and 0 deletions

View file

@ -1,3 +1,8 @@
Thu Jul 11 20:17:51 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
* string.c (rb_string_value_cstr): check null char in char, not in
byte.
Thu Jul 11 14:48:35 2013 Zachary Scott <e@zzak.io>
* array.c: Replace confusing example for #reverse_each in overview

View file

@ -1459,13 +1459,33 @@ rb_string_value_ptr(volatile VALUE *ptr)
return RSTRING_PTR(str);
}
static const char *
str_null_char(const char *s, long len, rb_encoding *enc)
{
int n;
const char *e = s + len;
for (; s < e; s += n) {
if (!rb_enc_codepoint_len(s, e, &n, enc)) return s;
}
return 0;
}
char *
rb_string_value_cstr(volatile VALUE *ptr)
{
VALUE str = rb_string_value(ptr);
char *s = RSTRING_PTR(str);
long len = RSTRING_LEN(str);
rb_encoding *enc = rb_enc_get(str);
const int minlen = rb_enc_mbminlen(enc);
if (minlen > 1) {
if (str_null_char(s, len, enc)) {
rb_raise(rb_eArgError, "string contains null char");
}
}
else
if (!s || memchr(s, 0, len)) {
rb_raise(rb_eArgError, "string contains null byte");
}

View file

@ -14,4 +14,23 @@ class Test_StringCStr < Test::Unit::TestCase
s = Bug::String.new("abcdef")*100000
assert_equal(0, s.cstr_term, Bug4319)
end
WCHARS = [Encoding::UTF_16BE, Encoding::UTF_16LE, Encoding::UTF_32BE, Encoding::UTF_32LE]
def test_wchar_embed
WCHARS.each do |enc|
s = Bug::String.new("ab".encode(enc))
assert_nothing_raised(ArgumentError) {s.cstr_term}
end
end
def test_wchar_long
str = "abcdef"
n = 100
len = str.size * n
WCHARS.each do |enc|
s = Bug::String.new(str.encode(enc))*n
assert_nothing_raised(ArgumentError) {s.cstr_term}
end
end
end