ruby-changes:53461
From: shyouhei <ko1@a...>
Date: Mon, 12 Nov 2018 11:39:30 +0900 (JST)
Subject: [ruby-changes:53461] shyouhei:r65677 (trunk): char is neither signed nor unsigned
shyouhei 2018-11-12 11:39:24 +0900 (Mon, 12 Nov 2018) New Revision: 65677 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=65677 Log: char is neither signed nor unsigned read_escaped_byte() returns values of range -1...256. -1 indicates error. So the function basically expects char to be 0..255 range. There is no such guarantee. `char` is not always unsigned. We need to explicitly declare chbuf to be unsigned char. Modified files: trunk/re.c Index: re.c =================================================================== --- re.c (revision 65676) +++ re.c (revision 65677) @@ -2389,7 +2389,8 @@ unescape_escaped_nonascii(const char **p https://github.com/ruby/ruby/blob/trunk/re.c#L2389 { const char *p = *pp; int chmaxlen = rb_enc_mbmaxlen(enc); - char *chbuf = ALLOCA_N(char, chmaxlen); + unsigned char *area = ALLOCA_N(unsigned char, chmaxlen); + char *chbuf = (char *)area; int chlen = 0; int byte; int l; @@ -2401,14 +2402,14 @@ unescape_escaped_nonascii(const char **p https://github.com/ruby/ruby/blob/trunk/re.c#L2402 return -1; } - chbuf[chlen++] = byte; + area[chlen++] = byte; while (chlen < chmaxlen && MBCLEN_NEEDMORE_P(rb_enc_precise_mbclen(chbuf, chbuf+chlen, enc))) { byte = read_escaped_byte(&p, end, err); if (byte == -1) { return -1; } - chbuf[chlen++] = byte; + area[chlen++] = byte; } l = rb_enc_precise_mbclen(chbuf, chbuf+chlen, enc); @@ -2416,7 +2417,7 @@ unescape_escaped_nonascii(const char **p https://github.com/ruby/ruby/blob/trunk/re.c#L2417 errcpy(err, "invalid multibyte escape"); return -1; } - if (1 < chlen || (chbuf[0] & 0x80)) { + if (1 < chlen || (area[0] & 0x80)) { rb_str_buf_cat(buf, chbuf, chlen); if (*encp == 0) @@ -2428,7 +2429,7 @@ unescape_escaped_nonascii(const char **p https://github.com/ruby/ruby/blob/trunk/re.c#L2429 } else { char escbuf[5]; - snprintf(escbuf, sizeof(escbuf), "\\x%02X", chbuf[0]&0xff); + snprintf(escbuf, sizeof(escbuf), "\\x%02X", area[0]&0xff); rb_str_buf_cat(buf, escbuf, 4); } *pp = p; -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/