From a7481aae3fc2d9e83dcf548e614209025c09fc83 Mon Sep 17 00:00:00 2001 From: nobu Date: Sun, 14 Jul 2013 17:21:41 +0000 Subject: 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 --- ChangeLog | 6 +++++- string.c | 35 ++++++++++++++++++----------------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/ChangeLog b/ChangeLog index ca07a9b9ab..8b3a72bdfc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,8 @@ -Mon Jul 15 02:21:33 2013 Nobuyoshi Nakada +Mon Jul 15 02:21:39 2013 Nobuyoshi Nakada + + * 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. diff --git a/string.c b/string.c index abfdaa3067..3e677c4a9e 100644 --- a/string.c +++ b/string.c @@ -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 -- cgit v1.2.3