From db3ddad3a710de4f0c6ef5d47493c5f7ba9b7384 Mon Sep 17 00:00:00 2001 From: nobu Date: Thu, 11 Jul 2013 11:17:54 +0000 Subject: [PATCH] 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 --- ChangeLog | 5 +++++ string.c | 20 ++++++++++++++++++++ test/-ext-/string/test_cstr.rb | 19 +++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/ChangeLog b/ChangeLog index f093150913..ee399d68d6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Thu Jul 11 20:17:51 2013 Nobuyoshi Nakada + + * string.c (rb_string_value_cstr): check null char in char, not in + byte. + Thu Jul 11 14:48:35 2013 Zachary Scott * array.c: Replace confusing example for #reverse_each in overview diff --git a/string.c b/string.c index 9824ccb520..ec870eb6b9 100644 --- a/string.c +++ b/string.c @@ -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"); } diff --git a/test/-ext-/string/test_cstr.rb b/test/-ext-/string/test_cstr.rb index 8c0bb136ad..8ea86aa6da 100644 --- a/test/-ext-/string/test_cstr.rb +++ b/test/-ext-/string/test_cstr.rb @@ -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