string.c: consider old terminator

* string.c (str_fill_term): consider old terminator length, and should
  not use rb_enc_ascget since it depends on the current encoding which
  may not be compatible with the new terminator.  [Bug #8634]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41967 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2013-07-14 17:21:41 +00:00
Родитель 6a7fd8cbe7
Коммит a7481aae3f
2 изменённых файлов: 23 добавлений и 18 удалений

Просмотреть файл

@ -1,4 +1,8 @@
Mon Jul 15 02:21:33 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
Mon Jul 15 02:21:39 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
* string.c (str_fill_term): consider old terminator length, and should
not use rb_enc_ascget since it depends on the current encoding which
may not be compatible with the new terminator. [Bug #8634]
* encoding.c (enc_inspect): use PRIsVALUE to preserve the result
encoding.

Просмотреть файл

@ -1482,6 +1482,15 @@ rb_string_value_ptr(volatile VALUE *ptr)
return RSTRING_PTR(str);
}
static int
zero_filled(const char *s, int n)
{
for (; n > 0; --n) {
if (*s++) return 0;
}
return 1;
}
static const char *
str_null_char(const char *s, long len, const int minlen, rb_encoding *enc)
{
@ -1489,30 +1498,22 @@ str_null_char(const char *s, long len, const int minlen, rb_encoding *enc)
const char *e = s + len;
for (; s + minlen <= e; s += n) {
if (!rb_enc_codepoint_len(s, e, &n, enc)) return s;
if (zero_filled(s, minlen)) return s;
}
return 0;
}
static char *
str_fill_term(VALUE str, char *s, long len, int termlen, rb_encoding *enc)
str_fill_term(VALUE str, char *s, long len, int oldtermlen, int termlen)
{
int oldtermlen = rb_enc_mbminlen(enc);
long capa = rb_str_capacity(str) + oldtermlen;
int n;
long capa = rb_str_capacity(str) + 1;
if (capa < len + termlen) {
rb_str_modify_expand(str, len + termlen - capa);
rb_str_modify_expand(str, termlen);
}
else {
const char *e = s + len;
int diff = 0;
if (termlen > oldtermlen) diff = termlen - oldtermlen;
if (!diff && str_independent(str) &&
!rb_enc_ascget(e, e + oldtermlen, &n, enc)) {
return s;
}
str_make_independent_expand(str, diff);
else if (!str_independent(str)) {
if (zero_filled(s + len, termlen)) return s;
str_make_independent(str);
}
s = RSTRING_PTR(str);
TERM_FILL(s + len, termlen);
@ -1532,7 +1533,7 @@ rb_string_value_cstr(volatile VALUE *ptr)
if (str_null_char(s, len, minlen, enc)) {
rb_raise(rb_eArgError, "string contains null char");
}
return str_fill_term(str, s, len, minlen, enc);
return str_fill_term(str, s, len, minlen, minlen);
}
if (!s || memchr(s, 0, len)) {
rb_raise(rb_eArgError, "string contains null byte");
@ -1551,7 +1552,7 @@ rb_str_fill_terminator(VALUE str, const int newminlen)
char *s = RSTRING_PTR(str);
long len = RSTRING_LEN(str);
rb_encoding *enc = rb_enc_get(str);
str_fill_term(str, s, len, newminlen, enc);
str_fill_term(str, s, len, rb_enc_mbminlen(enc), newminlen);
}
VALUE