ruby-changes:13476
From: naruse <ko1@a...>
Date: Wed, 7 Oct 2009 09:35:45 +0900 (JST)
Subject: [ruby-changes:13476] Ruby:r25251 (trunk): * string.c (rb_str_inspect): don't assign -1 to unsigned int.
naruse 2009-10-07 09:35:24 +0900 (Wed, 07 Oct 2009) New Revision: 25251 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=25251 Log: * string.c (rb_str_inspect): don't assign -1 to unsigned int. Modified files: trunk/ChangeLog trunk/string.c Index: ChangeLog =================================================================== --- ChangeLog (revision 25250) +++ ChangeLog (revision 25251) @@ -1,3 +1,7 @@ +Wed Oct 7 09:23:49 2009 NARUSE, Yui <naruse@r...> + + * string.c (rb_str_inspect): don't assign -1 to unsigned int. + Wed Oct 7 00:27:01 2009 Tanaka Akira <akr@f...> * lib/resolv.rb (Resolv::DNS.bind_random_port): bind to "::" for IPv6. Index: string.c =================================================================== --- string.c (revision 25250) +++ string.c (revision 25251) @@ -4069,6 +4069,8 @@ { rb_encoding *enc = STR_ENC_GET(str); char *p, *pend; +#define CHAR_ESC_LEN 12 /* sizeof(\x{ hex of 32bit unsigned int }) */ + char buf[CHAR_ESC_LEN + 1]; VALUE result = rb_str_buf_new(0); rb_encoding *resenc = rb_default_internal_encoding(); int unicode_p = rb_enc_unicode_p(enc); @@ -4080,15 +4082,16 @@ p = RSTRING_PTR(str); pend = RSTRING_END(str); while (p < pend) { - unsigned int c = -1, cc; + unsigned int c, cc; int n; n = rb_enc_precise_mbclen(p, pend, enc); if (!MBCLEN_CHARFOUND_P(n)) { + snprintf(buf, CHAR_ESC_LEN, "\\x%02X", *p & 0377); + str_buf_cat(result, buf, strlen(buf)); p++; - n = 1; - goto escape_codepoint; - } + continue; + } n = MBCLEN_CHARFOUND_LEN(n); c = rb_enc_mbc_to_codepoint(p, pend, enc); p += n; @@ -4130,31 +4133,21 @@ str_buf_cat(result, p-n, n); } else { - char buf[11]; - escape_codepoint: - - if (c == -1) { - char *q; - for (q = p-n; q < p; q++) { - sprintf(buf, "\\x%02X", *q & 0377); - str_buf_cat(result, buf, strlen(buf)); - } - } - else if (unicode_p) { + if (unicode_p) { if (c < 0x10000) { - sprintf(buf, "\\u%04X", c); + snprintf(buf, CHAR_ESC_LEN, "\\u%04X", c); } else { - sprintf(buf, "\\u{%X}", c); + snprintf(buf, CHAR_ESC_LEN, "\\u{%X}", c); } str_buf_cat(result, buf, strlen(buf)); } else { if (c < 0x100) { - sprintf(buf, "\\x%02X", c); + snprintf(buf, CHAR_ESC_LEN, "\\x%02X", c); } else { - sprintf(buf, "\\x{%X}", c); + snprintf(buf, CHAR_ESC_LEN, "\\x{%X}", c); } str_buf_cat(result, buf, strlen(buf)); } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/