ruby-changes:29688
From: nobu <ko1@a...>
Date: Tue, 2 Jul 2013 17:22:37 +0900 (JST)
Subject: [ruby-changes:29688] nobu:r41739 (trunk): encoding.c: validate index
nobu 2013-07-02 17:22:19 +0900 (Tue, 02 Jul 2013) New Revision: 41739 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=41739 Log: encoding.c: validate index * encoding.c (rb_enc_set_index, rb_enc_associate_index): validate argument encoding index. * include/ruby/encoding.h (ENCODING_SET): use rb_enc_set_index() instead of setting inlined bits directly. Modified files: trunk/ChangeLog trunk/encoding.c trunk/include/ruby/encoding.h Index: encoding.c =================================================================== --- encoding.c (revision 41738) +++ encoding.c (revision 41739) @@ -142,6 +142,25 @@ must_encoding(VALUE enc) https://github.com/ruby/ruby/blob/trunk/encoding.c#L142 return index; } +static rb_encoding * +must_encindex(int index) +{ + rb_encoding *enc = rb_enc_from_index(index); + if (!enc) { + rb_raise(rb_eEncodingError, "encoding index out of bound: %d", + index); + } + if (ENC_TO_ENCINDEX(enc) != index) { + rb_raise(rb_eEncodingError, "wrong encoding index %d for %s (expected %d)", + index, rb_enc_name(enc), ENC_TO_ENCINDEX(enc)); + } + if (enc_autoload_p(enc) && enc_autoload(enc) == -1) { + rb_loaderror("failed to load encoding (%s)", + rb_enc_name(enc)); + } + return enc; +} + int rb_to_encoding_index(VALUE enc) { @@ -736,12 +755,15 @@ void https://github.com/ruby/ruby/blob/trunk/encoding.c#L755 rb_enc_set_index(VALUE obj, int idx) { rb_check_frozen(obj); + must_encindex(idx); enc_set_index(obj, idx); } VALUE rb_enc_associate_index(VALUE obj, int idx) { + rb_encoding *enc; + /* enc_check_capable(obj);*/ rb_check_frozen(obj); if (rb_enc_get_index(obj) == idx) @@ -749,8 +771,9 @@ rb_enc_associate_index(VALUE obj, int id https://github.com/ruby/ruby/blob/trunk/encoding.c#L771 if (SPECIAL_CONST_P(obj)) { rb_raise(rb_eArgError, "cannot set encoding"); } + enc = must_encindex(idx); if (!ENC_CODERANGE_ASCIIONLY(obj) || - !rb_enc_asciicompat(rb_enc_from_index(idx))) { + !rb_enc_asciicompat(enc)) { ENC_CODERANGE_CLEAR(obj); } enc_set_index(obj, idx); Index: include/ruby/encoding.h =================================================================== --- include/ruby/encoding.h (revision 41738) +++ include/ruby/encoding.h (revision 41739) @@ -32,14 +32,7 @@ RUBY_SYMBOL_EXPORT_BEGIN https://github.com/ruby/ruby/blob/trunk/include/ruby/encoding.h#L32 RBASIC(obj)->flags &= ~ENCODING_MASK;\ RBASIC(obj)->flags |= (VALUE)(i) << ENCODING_SHIFT;\ } while (0) -#define ENCODING_SET(obj,i) do {\ - VALUE rb_encoding_set_obj = (obj); \ - int encoding_set_enc_index = (i); \ - if (encoding_set_enc_index < ENCODING_INLINE_MAX) \ - ENCODING_SET_INLINED(rb_encoding_set_obj, encoding_set_enc_index); \ - else \ - rb_enc_set_index(rb_encoding_set_obj, encoding_set_enc_index); \ -} while (0) +#define ENCODING_SET(obj,i) rb_enc_set_index((obj), (i)) #define ENCODING_GET_INLINED(obj) (int)((RBASIC(obj)->flags & ENCODING_MASK)>>ENCODING_SHIFT) #define ENCODING_GET(obj) \ Index: ChangeLog =================================================================== --- ChangeLog (revision 41738) +++ ChangeLog (revision 41739) @@ -1,4 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 -Tue Jul 2 17:22:12 2013 Nobuyoshi Nakada <nobu@r...> +Tue Jul 2 17:22:16 2013 Nobuyoshi Nakada <nobu@r...> + + * encoding.c (rb_enc_set_index, rb_enc_associate_index): validate + argument encoding index. + + * include/ruby/encoding.h (ENCODING_SET): use rb_enc_set_index() + instead of setting inlined bits directly. * encoding.c (rb_enc_init): register preserved indexes. -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/