ruby-changes:62377
From: Nobuyoshi <ko1@a...>
Date: Thu, 23 Jul 2020 16:59:24 +0900 (JST)
Subject: [ruby-changes:62377] f1563edca0 (ruby_2_7): Get rid of use of magic number 'E'
https://git.ruby-lang.org/ruby.git/commit/?id=f1563edca0 From f1563edca0af1a5ad5cc748eb855ff9265e69e7f Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Sat, 11 Jan 2020 16:32:56 +0900 Subject: Get rid of use of magic number 'E' diff --git a/marshal.c b/marshal.c index df8da10..cac6329 100644 --- a/marshal.c +++ b/marshal.c @@ -574,7 +574,7 @@ w_obj_each(st_data_t key, st_data_t val, st_data_t a) https://github.com/ruby/ruby/blob/trunk/marshal.c#L574 if (to_be_skipped_id(id)) { if (id == s_encoding_short) { - rb_warn("instance variable `E' on class %"PRIsVALUE" is not dumped", + rb_warn("instance variable `"name_s_encoding_short"' on class %"PRIsVALUE" is not dumped", CLASS_OF(arg->obj)); } return ST_CONTINUE; @@ -1360,6 +1360,13 @@ r_bytes0(long len, struct load_arg *arg) https://github.com/ruby/ruby/blob/trunk/marshal.c#L1360 return str; } +static inline int +name_equal(const char *name, size_t nlen, const char *p, long l) +{ + if ((size_t)l != nlen || *p != *name) return 0; + return nlen == 1 || memcmp(p+1, name+1, nlen-1) == 0; +} + static int sym2encidx(VALUE sym, VALUE val) { @@ -1369,12 +1376,11 @@ sym2encidx(VALUE sym, VALUE val) https://github.com/ruby/ruby/blob/trunk/marshal.c#L1376 if (rb_enc_get_index(sym) != ENCINDEX_US_ASCII) return -1; RSTRING_GETMEM(sym, p, l); if (l <= 0) return -1; - if (l == sizeof(name_encoding) && - memcmp(p, name_encoding, sizeof(name_encoding)) == 0) { + if (name_equal(name_encoding, sizeof(name_encoding), p, l)) { int idx = rb_enc_find_index(StringValueCStr(val)); return idx; } - else if (l == 1 && *p == 'E') { + if (name_equal(name_s_encoding_short, rb_strlen_lit(name_s_encoding_short), p, l)) { if (val == Qfalse) return rb_usascii_encindex(); else if (val == Qtrue) return rb_utf8_encindex(); /* bogus ignore */ -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/