[ruby-dev:48384] [ruby-trunk - Bug #10014] [Open] Do not expose no-pinned down IDs

From: ko1@...
Date: 2014-07-08 10:56:40 UTC
List: ruby-dev #48384
Issue #10014 has been reported by Koichi Sasada.

----------------------------------------
Bug #10014: Do not expose no-pinned down IDs
https://bugs.ruby-lang.org/issues/10014

* Author: Koichi Sasada
* Status: Open
* Priority: Normal
* Assignee: Koichi Sasada
* Category: core
* Target version: current: 2.2.0
* ruby -v: 2.2
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN
----------------------------------------
=E6=A6=82=E8=A6=81
----

ID =E3=81=A8 Symbol =E3=81=AE=E9=96=A2=E4=BF=82=E3=82=92=E6=95=B4=E7=90=86=
=E3=81=97=E3=80=81=E3=82=88=E3=82=8A=E3=83=90=E3=82=B0=E3=81=AE=E5=B0=91=E3=
=81=AA=E3=81=84=E3=82=B3=E3=83=BC=E3=83=89=E3=81=B8=E3=81=A8=E5=A4=89=E6=9B=
=B4=E3=81=99=E3=82=8B=E3=80=82

=E7=8F=BE=E7=8A=B6=E3=81=A8=E5=95=8F=E9=A1=8C=E7=82=B9
------------

Symbol GC =E3=81=8C=E5=B0=8E=E5=85=A5=E3=81=95=E3=82=8C=E3=80=81Symbol =E3=
=81=AB dynamic symbol =E3=81=A8 static symbol =E3=81=AB=E5=88=A5=E3=82=8C=
=E3=82=8B
=E3=81=93=E3=81=A8=E3=81=AB=E3=81=AA=E3=81=A3=E3=81=9F=E3=80=82

pindown =E3=81=95=E3=82=8C=E3=81=A6=E3=81=84=E3=81=AA=E3=81=84 dynamic symb=
ol =E3=81=AF=E3=80=81GC =E5=8F=AF=E8=83=BD=E3=81=A7=E3=81=82=E3=82=8A=E3=80=
=81=E3=81=93=E3=81=93=E3=81=A7=E3=81=AF mortal=20
symbol =E3=81=A8=E5=91=BC=E3=81=B6=E3=81=93=E3=81=A8=E3=81=AB=E3=81=99=E3=
=82=8B=E3=80=82

=E9=80=86=E3=81=AB=E3=80=81static symbol =E3=81=8A=E3=82=88=E3=81=B3 pindow=
n =E3=81=95=E3=82=8C=E3=81=9F symbol =E3=82=92=E3=80=81=E3=81=93=E3=81=93=
=E3=81=A7=E3=81=AF immortal=20
symbol =E3=81=A8=E5=91=BC=E3=81=B6=E3=81=93=E3=81=A8=E3=81=AB=E3=81=99=E3=
=82=8B=E3=80=82

=E7=8F=BE=E5=9C=A8=E3=80=81=E3=82=B3=E3=83=BC=E3=83=89=E3=81=AE=E5=90=84=E6=
=89=80=E3=81=AB=E3=80=81=E3=80=8CID =E3=82=92=E5=8F=96=E3=82=8A=E5=87=BA=E3=
=81=97=E3=81=9F=E3=81=84=E3=81=8C=E3=80=81mortal symbol =E3=82=92 immortal=
=20
symbol =E3=81=B8=E5=A4=89=E6=8F=9B=E3=81=97=E3=81=9F=E3=81=8F=E3=81=AA=E3=
=81=84=E3=80=8D=E3=81=A8=E3=81=84=E3=81=86=E7=94=A8=E9=80=94=E3=81=8C=E3=81=
=82=E3=82=8B=E3=80=82=E5=85=B7=E4=BD=93=E7=9A=84=E3=81=AB=E3=81=AF=E3=80=81=
=E3=80=8CID =E3=82=92 key =E3=81=A8=E3=81=97
=E3=81=A6=E3=81=84=E3=82=8B=E3=83=87=E3=83=BC=E3=82=BF=E3=81=AB=E5=AF=BE=E3=
=81=97=E3=80=81=E4=B8=8E=E3=81=88=E3=82=89=E3=82=8C=E3=81=9F=E5=BC=95=E6=95=
=B0 name =E3=81=8B=E3=82=89 ID =E3=82=92=E5=8F=96=E3=82=8A=E5=87=BA=E3=81=
=97=E3=80=81=E3=81=9D=E3=82=8C=E3=81=A8=E5=90=88=E8=87=B4=E3=81=99
=E3=82=8B=E3=81=8B=E5=88=A4=E5=AE=9A=E3=81=97=E3=81=9F=E3=81=84=E5=A0=B4=E5=
=90=88=E3=80=8D=E3=81=A7=E3=81=82=E3=82=8B=E3=80=82=E3=82=82=E3=81=97 name =
=E3=81=8C=E5=AF=BE=E5=BF=9C=E3=81=99=E3=82=8B=E3=82=82=E3=81=AE=E3=81=8C mo=
rtal symbol =E3=81=A7
=E3=81=82=E3=81=A3=E3=81=9F=E5=A0=B4=E5=90=88=E3=80=81=E3=81=93=E3=81=AE=E5=
=88=A4=E5=AE=9A=E3=81=AE=E3=81=9F=E3=82=81=E3=81=A0=E3=81=91=E3=81=AB immor=
tal symbol =E3=81=A8=E5=A4=89=E6=8F=9B=E3=81=99=E3=82=8B=E3=81=A8=E3=80=81G=
C =E3=81=99=E3=82=8B=E3=81=93
=E3=81=A8=E3=81=8C=E3=81=A7=E3=81=8D=E3=81=9A=E5=8A=B9=E7=8E=87=E3=81=AB=E5=
=95=8F=E9=A1=8C=E3=81=8C=E5=87=BA=E3=82=8B=E3=81=9F=E3=82=81=E3=81=A7=E3=81=
=82=E3=82=8B=E3=80=82

=E3=81=93=E3=81=AE=E7=9B=AE=E7=9A=84=E3=82=92=E8=A7=A3=E6=B1=BA=E3=81=99=E3=
=82=8B=E3=81=9F=E3=82=81=E3=81=AB=E3=80=81rb_check_id_without_pindown() =EF=
=BC=88=E3=81=8A=E3=82=88=E3=81=B3=E3=80=81=E9=A1=9E=E4=BC=BC=E3=81=AE
=E9=96=A2=E6=95=B0=EF=BC=89=E3=81=8C=E6=8F=90=E4=BE=9B=E3=81=95=E3=82=8C=E3=
=81=A6=E3=81=84=E3=82=8B=E3=80=82=E3=81=93=E3=82=8C=E3=81=AF=E3=80=81mortal=
 symbol =E3=81=8B=E3=82=89=E8=A8=88=E7=AE=97=E3=81=95=E3=82=8C=E3=82=8B ID =
=E3=82=92=E3=80=81
immortal symbol =E3=81=AB=E5=A4=89=E6=9B=B4=E3=81=9B=E3=81=9A=E3=81=AB=E8=
=BF=94=E3=81=99=E3=80=82

=E3=81=97=E3=81=8B=E3=81=97=E3=80=81mortal symbol =E3=81=8B=E3=82=89=E8=A8=
=88=E7=AE=97=E3=81=95=E3=82=8C=E3=81=9F ID =E3=81=AA=E3=81=AE=E3=81=A7=E3=
=80=81=E3=81=93=E3=82=8C=E3=82=92=E3=81=A9=E3=81=93=E3=81=8B=E3=81=AB=E4=BF=
=9D=E5=AD=98=E3=81=97=E3=80=81GC=20
=E3=82=92=E7=B5=8C=E9=81=8E=E3=81=99=E3=82=8B=E3=81=A8=E3=80=81=E3=81=93=E3=
=81=AE ID =E3=81=8C=E7=84=A1=E3=81=8F=E3=81=AA=E3=81=A3=E3=81=A6=E3=81=97=
=E3=81=BE=E3=81=86=E5=8D=B1=E9=99=BA=E3=81=8C=E3=81=82=E3=82=8B=E3=80=82

=E7=8F=BE=E7=8A=B6=E3=81=AF=E3=80=81=E4=B8=AD=E7=94=B0=E3=81=95=E3=82=93=E3=
=81=8C=E8=81=B7=E4=BA=BA=E7=9A=84=E3=81=AB=E3=80=8C=E3=81=93=E3=81=AE ID =
=E3=81=AF=E4=BF=9D=E5=AD=98=E3=81=97=E3=81=AA=E3=81=84=E3=80=8D=E3=81=A8=E3=
=81=84=E3=81=86=E7=94=A8=E9=80=94=E3=81=A7=E3=81=AE=E3=81=BF=E4=BD=BF=E3=81=
=86=E3=82=88
=E3=81=86=E3=81=AB=E3=81=97=E3=81=A6=E3=81=84=E3=82=8B=E3=81=9F=E3=82=81=E3=
=80=81=E5=95=8F=E9=A1=8C=E3=81=AF=E8=B5=B7=E3=81=8D=E3=81=A6=E3=81=84=E3=81=
=AA=E3=81=84=E3=81=8C=E3=80=81=E3=81=86=E3=81=A3=E3=81=8B=E3=82=8A pindown =
=E3=81=97=E3=81=A6=E3=81=84=E3=81=AA=E3=81=84 ID=20
=E3=82=92=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89 ID =E3=81=AA=E3=81=A9=E3=81=
=AB=E4=BD=BF=E3=81=A3=E3=81=A6=E4=BF=9D=E5=AD=98=E3=81=97=E3=81=A6=E3=81=97=
=E3=81=BE=E3=81=84=E3=80=81GC =E3=81=95=E3=82=8C=E3=81=A6=E3=81=97=E3=81=BE=
=E3=81=86=E3=80=81=E3=81=A8=E3=81=84=E3=81=86=E5=8D=B1=E9=99=BA=E3=81=8C
=E3=81=82=E3=82=8B=E3=80=82

=E8=A7=A3=E6=B1=BA=E7=AD=96
------

=E7=8F=BE=E7=8A=B6=E3=81=AE

(a) ID =E3=81=8C=E3=81=82=E3=82=8C=E3=81=B0=E5=AF=BE=E5=BF=9C=E3=81=99=E3=
=82=8B Symbol =E3=81=8C=E3=81=82=E3=82=8B
(b) Symbol =E3=81=8C=E3=81=82=E3=82=8C=E3=81=B0=E5=AF=BE=E5=BF=9C=E3=81=99=
=E3=82=8B ID =E3=81=8C=E3=81=82=E3=82=8B

=E3=81=A8=E3=81=84=E3=81=86=E5=8E=9F=E5=89=87=E3=82=92=E5=A4=89=E6=9B=B4=E3=
=81=97=E3=80=81

(a') ID =E3=81=8C=E3=81=82=E3=82=8C=E3=81=B0=E5=AF=BE=E5=BF=9C=E3=81=99=E3=
=82=8B *Immortal* Symbol =E3=81=8C=E3=81=82=E3=82=8B
(b') *Immortal Symbol* =E3=81=8C=E3=81=82=E3=82=8C=E3=81=B0=E5=AF=BE=E5=BF=
=9C=E3=81=99=E3=82=8B ID =E3=81=8C=E3=81=82=E3=82=8B
(c') *Mortal Symbol* =E3=81=AF=E3=80=81ID =E3=82=92=E6=8C=81=E3=81=9F=E3=81=
=AA=E3=81=84

=E3=81=A8=E5=A4=89=E6=9B=B4=E3=81=99=E3=82=8B=E3=80=82

=E3=80=8CID =E3=82=92 key =E3=81=A8=E3=81=97=E3=81=A6=E3=81=84=E3=82=8B=E3=
=83=87=E3=83=BC=E3=82=BF=E3=81=AB=E5=AF=BE=E3=81=97=E3=80=81=E4=B8=8E=E3=81=
=88=E3=82=89=E3=82=8C=E3=81=9F=E5=BC=95=E6=95=B0 name =E3=81=8B=E3=82=89 ID=
 =E3=82=92=E5=8F=96=E3=82=8A=E5=87=BA
=E3=81=97=E3=80=81=E3=81=9D=E3=82=8C=E3=81=A8=E5=90=88=E8=87=B4=E3=81=99=E3=
=82=8B=E3=81=8B=E5=88=A4=E5=AE=9A=E3=81=97=E3=81=9F=E3=81=84=E5=A0=B4=E5=90=
=88=E3=80=8D=E3=81=A8=E3=81=84=E3=81=86=E7=94=A8=E9=80=94=E3=81=A7=E3=81=AF=
=E3=80=81=E3=81=9D=E3=82=82=E3=81=9D=E3=82=82=E4=BF=9D=E5=AD=98=E3=81=99=E3=
=82=8B=E3=82=88=E3=81=86
=E3=81=AA ID =E3=81=AF immortal symbol =E3=81=A7=E3=81=82=E3=82=8B=E3=81=9F=
=E3=82=81=E3=80=81=E3=82=8F=E3=81=96=E3=82=8F=E3=81=96 mortal symbol =E3=81=
=AB=E5=AF=BE=E5=BF=9C=E3=81=99=E3=82=8B=20
ID =E3=82=92=E3=81=B5=E3=82=8B=E5=BF=85=E8=A6=81=E3=81=8C=E3=81=AA=E3=81=84=
=E3=81=9F=E3=82=81=E3=81=A0=E3=81=8B=E3=82=89=E3=81=A7=E3=81=82=E3=82=8B=E3=
=80=82


=E7=95=99=E6=84=8F=E7=82=B9
------

=E3=81=84=E3=81=8F=E3=81=A4=E3=81=8B=E3=81=AE=E3=82=B3=E3=83=BC=E3=83=89=E3=
=81=A7=E3=81=AF=E3=80=81=E5=8E=9F=E5=89=87 (a) =E3=82=92=E5=88=A9=E7=94=A8=
=E3=81=97=E3=81=A6=E3=80=81name =E3=81=8B=E3=82=89 Symbol =E3=81=AE=E5=AD=
=98=E5=9C=A8=E7=A2=BA=E8=AA=8D=E3=82=92
=E3=81=99=E3=82=8B=E3=80=81=E3=81=A8=E3=81=84=E3=81=86=E3=82=B3=E3=83=BC=E3=
=83=89=E3=81=8C=E3=81=82=E3=82=8B=E3=80=82=E3=81=A4=E3=81=BE=E3=82=8A=E3=80=
=81rb_check_id() =E3=81=A7 ID =E3=81=8C=E5=8F=96=E5=BE=97=E3=81=A7=E3=81=8D=
=E3=82=8C=E3=81=B0=E3=80=81
Symbol =E3=81=8C=E3=81=82=E3=82=8B=E3=80=81=E3=81=A8=E3=81=99=E3=82=8B=E3=
=82=B3=E3=83=BC=E3=83=89=E3=81=A7=E3=81=82=E3=82=8B=E3=80=82

=E3=81=93=E3=81=AE=E3=82=88=E3=81=86=E3=81=AA=E5=A0=B4=E5=90=88=E3=81=AF=E3=
=80=81=E6=96=B0=E3=81=9F=E3=81=AB=E4=BD=9C=E6=88=90=E3=81=97=E3=81=9F rb_ch=
eck_symbol(namep) =E3=82=92=E5=88=A9=E7=94=A8=E3=81=97=E3=81=A6=E3=80=81
Symbol =E3=81=AE=E5=AD=98=E5=9C=A8=E7=A2=BA=E8=AA=8D=E3=80=81=E3=81=8A=E3=
=82=88=E3=81=B3=E5=8F=96=E3=82=8A=E5=87=BA=E3=81=97=E3=82=92=E3=81=99=E3=82=
=8B=E3=82=88=E3=81=86=E3=81=AB=E5=A4=89=E6=9B=B4=E3=81=99=E3=82=8B=E5=BF=85=
=E8=A6=81=E3=81=8C=E3=81=82=E3=82=8B=E3=80=82

Ruby =E3=82=A4=E3=83=B3=E3=82=BF=E3=83=97=E3=83=AA=E3=82=BF=E3=81=AB 2 =E7=
=AE=87=E6=89=80=E3=81=82=E3=81=A3=E3=81=9F=E3=81=8C=E3=80=81=E5=A4=96=E9=83=
=A8=E3=81=AE=E6=8B=A1=E5=BC=B5=E3=83=A9=E3=82=A4=E3=83=96=E3=83=A9=E3=83=AA=
=E3=81=AB=E3=81=A9=E3=81=AE=E7=A8=8B=E5=BA=A6=E5=BD=B1=E9=9F=BF=E3=81=8C
=E3=81=82=E3=82=8B=E3=81=8B=E3=81=AF=E6=9C=AA=E8=AA=BF=E6=9F=BB=E3=81=A7=E3=
=81=82=E3=82=8B=E3=80=82


=E3=83=91=E3=83=83=E3=83=81
------

```diff
Index: internal.h
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- internal.h	(revision 46756)
+++ internal.h	(working copy)
@@ -809,12 +809,12 @@ void rb_gc_mark_symbols(int full_mark);
 ID rb_make_internal_id(void);
 void rb_gc_free_dsymbol(VALUE);
 VALUE rb_str_dynamic_intern(VALUE);
-ID rb_check_id_without_pindown(VALUE *);
-ID rb_sym2id_without_pindown(VALUE);
+ID rb_id_attrget(ID id);
+
+VALUE rb_check_symbol(volatile VALUE *namep);
 #ifdef RUBY_ENCODING_H
-ID rb_check_id_cstr_without_pindown(const char *, long, rb_encoding *);
+VALUE rb_check_symbol_cstr(const char *ptr, long len, rb_encoding *enc);
 #endif
-ID rb_id_attrget(ID id);
=20
 /* proc.c */
 VALUE rb_proc_location(VALUE self);
Index: load.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- load.c	(revision 46756)
+++ load.c	(working copy)
@@ -1108,7 +1108,7 @@ rb_mod_autoload(VALUE mod, VALUE sym, VA
 static VALUE
 rb_mod_autoload_p(VALUE mod, VALUE sym)
 {
-    ID id =3D rb_check_id_without_pindown(&sym);
+    ID id =3D rb_check_id(&sym);
     if (!id) {
 	return Qnil;
     }
Index: object.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- object.c	(revision 46756)
+++ object.c	(working copy)
@@ -2135,7 +2135,7 @@ rb_mod_const_get(int argc, VALUE *argv,
=20
 	if (pbeg =3D=3D p) goto wrong_name;
=20
-	id =3D rb_check_id_cstr_without_pindown(pbeg, len =3D p-pbeg, enc);
+	id =3D rb_check_id_cstr(pbeg, len =3D p-pbeg, enc);
 	beglen =3D pbeg-path;
=20
 	if (p < pend && p[0] =3D=3D ':') {
@@ -2277,7 +2277,7 @@ rb_mod_const_defined(int argc, VALUE *ar
=20
 	if (pbeg =3D=3D p) goto wrong_name;
=20
-	id =3D rb_check_id_cstr_without_pindown(pbeg, len =3D p-pbeg, enc);
+	id =3D rb_check_id_cstr(pbeg, len =3D p-pbeg, enc);
 	beglen =3D pbeg-path;
=20
 	if (p < pend && p[0] =3D=3D ':') {
@@ -2348,7 +2348,7 @@ rb_mod_const_defined(int argc, VALUE *ar
 static VALUE
 rb_obj_ivar_get(VALUE obj, VALUE iv)
 {
-    ID id =3D rb_check_id_without_pindown(&iv);
+    ID id =3D rb_check_id(&iv);
=20
     if (!id) {
 	if (rb_is_instance_name(iv)) {
@@ -2419,7 +2419,7 @@ rb_obj_ivar_set(VALUE obj, VALUE iv, VAL
 static VALUE
 rb_obj_ivar_defined(VALUE obj, VALUE iv)
 {
-    ID id =3D rb_check_id_without_pindown(&iv);
+    ID id =3D rb_check_id(&iv);
=20
     if (!id) {
 	if (rb_is_instance_name(iv)) {
@@ -2456,7 +2456,7 @@ rb_obj_ivar_defined(VALUE obj, VALUE iv)
 static VALUE
 rb_mod_cvar_get(VALUE obj, VALUE iv)
 {
-    ID id =3D rb_check_id_without_pindown(&iv);
+    ID id =3D rb_check_id(&iv);
=20
     if (!id) {
 	if (rb_is_class_name(iv)) {
@@ -2522,7 +2522,7 @@ rb_mod_cvar_set(VALUE obj, VALUE iv, VAL
 static VALUE
 rb_mod_cvar_defined(VALUE obj, VALUE iv)
 {
-    ID id =3D rb_check_id_without_pindown(&iv);
+    ID id =3D rb_check_id(&iv);
=20
     if (!id) {
 	if (rb_is_class_name(iv)) {
Index: parse.y
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- parse.y	(revision 46756)
+++ parse.y	(working copy)
@@ -51,7 +51,10 @@ static ID register_static_symid_str(ID,
 #define REGISTER_SYMID(id, name) register_static_symid((id), (name), strle=
n(name), enc)
 #include "id.c"
 #endif
+
 #define ID_DYNAMIC_SYM_P(id) (!(id&ID_STATIC_SYM)&&id>tLAST_TOKEN)
+#define STATIC_SYM2ID(sym) RSHIFT((unsigned long)(sym), RUBY_SPECIAL_SHIFT)
+#define STATIC_ID2SYM(id)  (((VALUE)(id)<<RUBY_SPECIAL_SHIFT)|SYMBOL_FLAG)
=20
 static inline int id_type(ID);
 #define is_notop_id(id) ((id)>tLAST_OP_ID)
@@ -8831,7 +8834,6 @@ block_dup_check_gen(struct parser_params
     }
 }
=20
-static ID rb_pin_dynamic_symbol(VALUE);
 static ID attrsetname_to_attr(VALUE name);
 static int lookup_id_str(ID id, st_data_t *data);
=20
@@ -10481,7 +10483,7 @@ dsymbol_check(const VALUE sym)
 }
=20
 static ID
-rb_pin_dynamic_symbol(VALUE sym)
+dsymbol_pindown(VALUE sym)
 {
     must_be_dynamic_symbol(sym);
=20
@@ -10496,20 +10498,56 @@ rb_pin_dynamic_symbol(VALUE sym)
     return (ID)sym;
 }
=20
-static int
-lookup_str_id(st_data_t str, st_data_t *data)
+static ID
+lookup_str_id(st_data_t str, st_data_t *id_datap)
 {
-    ID id;
+    if (st_lookup(global_symbols.str_id, str, id_datap)) {
+	const ID id =3D (ID)*id_datap;
=20
-    if (!st_lookup(global_symbols.str_id, str, data)) {
+	if (ID_DYNAMIC_SYM_P(id) && !SYMBOL_PINNED_P(id)) {
+	    *id_datap =3D 0;
+	    return FALSE;
+	}
+	else {
+	    return TRUE;
+	}
+    }
+    else {
+	return FALSE;
+    }
+}
+
+static VALUE
+lookup_str_sym(const st_data_t str, st_data_t *sym_datap)
+{
+    if (st_lookup(global_symbols.str_id, str, sym_datap)) {
+	const ID id =3D *sym_datap;
+
+	if (ID_DYNAMIC_SYM_P(id)) {
+	    *sym_datap =3D dsymbol_check(id);
+	}
+	else {
+	    *sym_datap =3D STATIC_ID2SYM(id);
+	}
+	return TRUE;
+    }
+    else {
 	return FALSE;
     }
-    id =3D (ID)*data;
+}
+
+static int
+lookup_id_str(ID id, st_data_t *data)
+{
     if (ID_DYNAMIC_SYM_P(id)) {
-	*data =3D (st_data_t)rb_pin_dynamic_symbol((VALUE)id);
+	*data =3D RSYMBOL(id)->fstr;
+	return TRUE;
     }
+    if (st_lookup(global_symbols.id_str, id, data)) {
     return TRUE;
 }
+    return FALSE;
+}
=20
 static ID
 intern_cstr_without_pindown(const char *name, long len, rb_encoding *enc)
@@ -10535,7 +10573,7 @@ rb_intern3(const char *name, long len, r
=20
     id =3D intern_cstr_without_pindown(name, len, enc);
     if (ID_DYNAMIC_SYM_P(id)) {
-	id =3D rb_pin_dynamic_symbol((VALUE)id);
+	id =3D dsymbol_pindown((VALUE)id);
     }
=20
     return id;
@@ -10684,10 +10722,12 @@ rb_intern(const char *name)
 ID
 rb_intern_str(VALUE str)
 {
-    st_data_t id;
+    st_data_t sym;
+
+    if (lookup_str_sym(str, &sym)) {
+	return SYM2ID(sym);
+    }
=20
-    if (lookup_str_id(str, &id))
-	return (ID)id;
     return intern_str(rb_str_dup(str));
 }
=20
@@ -10733,15 +10773,9 @@ rb_str_dynamic_intern(VALUE str)
 {
 #if USE_SYMBOL_GC
     rb_encoding *enc, *ascii;
-    ID id;
+    VALUE sym;
=20
-    if (st_lookup(global_symbols.str_id, str, &id)) {
-	VALUE sym =3D ID2SYM(id);
-	if (ID_DYNAMIC_SYM_P(id)) {
-	    /* because of lazy sweep, dynamic symbol may be unmarked already and =
swept
-	     * at next time */
-	    sym =3D dsymbol_check(sym);
-	}
+    if (lookup_str_sym(str, &sym)) {
 	return sym;
     }
=20
@@ -10762,40 +10796,17 @@ rb_str_dynamic_intern(VALUE str)
 #endif
 }
=20
-static int
-lookup_id_str(ID id, st_data_t *data)
-{
-    if (ID_DYNAMIC_SYM_P(id)) {
-	id =3D (ID)dsymbol_check((VALUE)id);
-	*data =3D RSYMBOL(id)->fstr;
-	return TRUE;
-    }
-    if (st_lookup(global_symbols.id_str, id, data)) {
-	return TRUE;
-    }
-    return FALSE;
-}
-
 ID
-rb_sym2id(VALUE x)
+rb_sym2id(VALUE sym)
 {
-    if (STATIC_SYM_P(x)) {
-	return RSHIFT((unsigned long)(x),RUBY_SPECIAL_SHIFT);
+    if (STATIC_SYM_P(sym)) {
+	return STATIC_SYM2ID(sym);
     }
     else {
-	return rb_pin_dynamic_symbol(x);
+	if (!SYMBOL_PINNED_P(sym)) {
+	    dsymbol_pindown(sym);
     }
-}
-
-ID
-rb_sym2id_without_pindown(VALUE x)
-{
-    if (STATIC_SYM_P(x)) {
-	return RSHIFT((unsigned long)(x),RUBY_SPECIAL_SHIFT);
-    }
-    else {
-	must_be_dynamic_symbol(x);
-	return (ID)x;
+	return (ID)sym;
     }
 }
=20
@@ -10803,7 +10814,7 @@ VALUE
 rb_id2sym(ID x)
 {
     if (!ID_DYNAMIC_SYM_P(x)) {
-	return ((VALUE)(x)<<RUBY_SPECIAL_SHIFT)|SYMBOL_FLAG;
+	return STATIC_ID2SYM(x);
     }
     else {
 	return (VALUE)x;
@@ -10814,9 +10825,13 @@ rb_id2sym(ID x)
 VALUE
 rb_sym2str(VALUE sym)
 {
-    return rb_id2str(rb_sym2id_without_pindown(sym));
+    if (DYNAMIC_SYM_P(sym)) {
+	return RSYMBOL(sym)->fstr;
+    }
+    else {
+	return rb_id2str(STATIC_SYM2ID(sym));
+    }
 }
-
=20
 VALUE
 rb_id2str(ID id)
@@ -10862,7 +10877,7 @@ rb_id2str(ID id)
 	str =3D rb_str_dup(str);
 	rb_str_cat(str, "=3D", 1);
 	register_static_symid_str(id, str);
-	if (st_lookup(global_symbols.id_str, id, &data)) {
+	if (lookup_id_str(id, &data)) {
             VALUE str =3D (VALUE)data;
             if (RBASIC(str)->klass =3D=3D 0)
                 RBASIC_SET_CLASS_RAW(str, rb_cString);
@@ -10890,10 +10905,15 @@ rb_make_internal_id(void)
 static int
 symbols_i(VALUE key, ID value, VALUE ary)
 {
-    VALUE sym =3D ID2SYM(value);
+    VALUE sym;
+
     if (ID_DYNAMIC_SYM_P(value)) {
-	sym =3D dsymbol_check(sym);
+	sym =3D (VALUE)value; /* sym =3D dsymbol_check((VALUE)value); rb_gc_start=
() prevent such situation */
     }
+    else {
+	sym =3D STATIC_ID2SYM(value);
+    }
+
     rb_ary_push(ary, sym);
     return ST_CONTINUE;
 }
@@ -10918,7 +10938,7 @@ VALUE
 rb_sym_all_symbols(void)
 {
     VALUE ary =3D rb_ary_new2(global_symbols.str_id->num_entries);
-
+    rb_gc_start();
     st_foreach(global_symbols.str_id, symbols_i, ary);
     return ary;
 }
@@ -10979,38 +10999,56 @@ rb_is_junk_id(ID id)
 ID
 rb_check_id(volatile VALUE *namep)
 {
-    ID id;
+    st_data_t id;
+    VALUE tmp;
+    VALUE name =3D *namep;
=20
-    id =3D rb_check_id_without_pindown((VALUE *)namep);
-    if (ID_DYNAMIC_SYM_P(id)) {
-	id =3D rb_pin_dynamic_symbol((VALUE)id);
+    if (STATIC_SYM_P(name)) {
+	return STATIC_SYM2ID(name);
+    }
+    else if (DYNAMIC_SYM_P(name)) {
+	if (SYMBOL_PINNED_P(name)) {
+	    return (ID)name;
     }
+	else {
+	    *namep =3D RSYMBOL(name)->fstr;
+	    return 0;
+	}
+    }
+    else if (!RB_TYPE_P(name, T_STRING)) {
+	tmp =3D rb_check_string_type(name);
+	if (NIL_P(tmp)) {
+	    tmp =3D rb_inspect(name);
+	    rb_raise(rb_eTypeError, "%s is not a symbol nor a string",
+		     RSTRING_PTR(tmp));
+	}
+	name =3D tmp;
+	*namep =3D name;
+    }
+
+    sym_check_asciionly(name);
=20
+    if (lookup_str_id(name, &id)) {
     return id;
 }
=20
-ID
-rb_check_id_cstr(const char *ptr, long len, rb_encoding *enc)
 {
-    ID id;
-
-    id =3D rb_check_id_cstr_without_pindown(ptr, len, enc);
-    if (ID_DYNAMIC_SYM_P(id)) {
-	id =3D rb_pin_dynamic_symbol((VALUE)id);
+	ID gid =3D attrsetname_to_attr(name);
+	if (gid) return rb_id_attrset(gid);
     }
=20
-    return id;
+    return (ID)0;
 }
=20
-ID
-rb_check_id_without_pindown(VALUE *namep)
+VALUE
+rb_check_symbol(volatile VALUE *namep)
 {
-    st_data_t id;
+    st_data_t sym;
     VALUE tmp;
     VALUE name =3D *namep;
=20
     if (SYMBOL_P(name)) {
-	return rb_sym2id_without_pindown(name);
+	return name;
     }
     else if (!RB_TYPE_P(name, T_STRING)) {
 	tmp =3D rb_check_string_type(name);
@@ -11025,55 +11063,81 @@ rb_check_id_without_pindown(VALUE *namep
=20
     sym_check_asciionly(name);
=20
-    if (st_lookup(global_symbols.str_id, (st_data_t)name, &id))
-	return (ID)id;
+    if (lookup_str_sym(name, &sym)) {
+	return sym;
+    }
=20
     {
 	ID gid =3D attrsetname_to_attr(name);
-	if (gid) return rb_id_attrset(gid);
+	if (gid) return ID2SYM(rb_id_attrset(gid));
     }
=20
-    return (ID)0;
+    return Qnil;
 }
=20
-static ID
-attrsetname_to_attr(VALUE name)
+ID
+rb_check_id_cstr(const char *ptr, long len, rb_encoding *enc)
 {
-    if (rb_is_attrset_name(name)) {
 	st_data_t id;
 	struct RString fake_str;
-	/* make local name by chopping '=3D' */
-	const VALUE localname =3D setup_fake_str(&fake_str, RSTRING_PTR(name), RS=
TRING_LEN(name) - 1);
-	rb_enc_copy(localname, name);
-	OBJ_FREEZE(localname);
+    const VALUE name =3D setup_fake_str(&fake_str, ptr, len);
+    rb_enc_associate(name, enc);
=20
-	if (st_lookup(global_symbols.str_id, (st_data_t)localname, &id)) {
-	    return (ID)id;
+    sym_check_asciionly(name);
+
+    if (lookup_str_id(name, &id)) {
+	return id;
+    }
+
+    if (rb_is_attrset_name(name)) {
+	fake_str.as.heap.len =3D len - 1;
+	if (lookup_str_id((st_data_t)name, &id)) {
+	    return rb_id_attrset((ID)id);
 	}
-	RB_GC_GUARD(name);
     }
=20
     return (ID)0;
 }
=20
-ID
-rb_check_id_cstr_without_pindown(const char *ptr, long len, rb_encoding *e=
nc)
+VALUE
+rb_check_symbol_cstr(const char *ptr, long len, rb_encoding *enc)
 {
-    st_data_t id;
+    st_data_t sym;
     struct RString fake_str;
     const VALUE name =3D setup_fake_str(&fake_str, ptr, len);
     rb_enc_associate(name, enc);
=20
     sym_check_asciionly(name);
=20
-    if (st_lookup(global_symbols.str_id, (st_data_t)name, &id))
-	return (ID)id;
+    if (lookup_str_sym(name, &sym)) {
+	return sym;
+    }
=20
     if (rb_is_attrset_name(name)) {
 	fake_str.as.heap.len =3D len - 1;
-	if (st_lookup(global_symbols.str_id, (st_data_t)name, &id)) {
-	    return rb_id_attrset((ID)id);
+	if (lookup_str_sym((st_data_t)name, &sym)) {
+	    return ID2SYM(rb_id_attrset(SYM2ID(sym)));
+	}
+    }
+
+    return Qnil;
+}
+
+static ID
+attrsetname_to_attr(VALUE name)
+{
+    if (rb_is_attrset_name(name)) {
+	st_data_t id;
+	struct RString fake_str;
+	/* make local name by chopping '=3D' */
+	const VALUE localname =3D setup_fake_str(&fake_str, RSTRING_PTR(name), RS=
TRING_LEN(name) - 1);
+	rb_enc_copy(localname, name);
+	OBJ_FREEZE(localname);
+
+	if (lookup_str_id((st_data_t)localname, &id)) {
+	    return (ID)id;
 	}
+	RB_GC_GUARD(name);
     }
=20
     return (ID)0;
Index: sprintf.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- sprintf.c	(revision 46756)
+++ sprintf.c	(working copy)
@@ -506,7 +506,7 @@ rb_str_format(int argc, const VALUE *arg
     for (; p < end; p++) {
 	const char *t;
 	int n;
-	ID id =3D 0;
+	VALUE sym =3D Qnil;
=20
 	for (t =3D p; t < end && *t !=3D '%'; t++) ;
 	PUSH(p, t - p);
@@ -601,16 +601,16 @@ rb_str_format(int argc, const VALUE *arg
 		}
 #endif
 		len =3D (int)(p - start + 1); /* including parenthesis */
-		if (id) {
+		if (sym !=3D Qnil) {
 		    rb_enc_raise(enc, rb_eArgError, "named%.*s after <%s>",
-				 len, start, rb_id2name(id));
+				 len, start, RSTRING_PTR(rb_sym2str(sym)));
 		}
 		CHECKNAMEARG(start, len, enc);
 		get_hash(&hash, argc, argv);
-		id =3D rb_check_id_cstr_without_pindown(start + 1,
+		sym =3D rb_check_symbol_cstr(start + 1,
 						      len - 2 /* without parenthesis */,
 						      enc);
-		if (id) nextvalue =3D rb_hash_lookup2(hash, ID2SYM(id), Qundef);
+		if (sym !=3D Qnil) nextvalue =3D rb_hash_lookup2(hash, sym, Qundef);
 		if (nextvalue =3D=3D Qundef) {
 		    rb_enc_raise(enc, rb_eKeyError, "key%.*s not found", len, start);
 		}
Index: string.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- string.c	(revision 46756)
+++ string.c	(working copy)
@@ -8323,14 +8323,7 @@ str_scrub_bang(int argc, VALUE *argv, VA
 static VALUE
 sym_find(VALUE dummy, VALUE sym)
 {
-    ID id =3D rb_check_id(&sym);
-
-    if (id) {
-	return ID2SYM(id);
-    }
-    else {
-	return Qnil;
-    }
+    return rb_check_symbol(&sym);
 }
=20
 /*
Index: struct.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- struct.c	(revision 46756)
+++ struct.c	(working copy)
@@ -759,7 +759,7 @@ rb_struct_aref(VALUE s, VALUE idx)
 	return rb_struct_aref_sym(s, idx);
     }
     else if (RB_TYPE_P(idx, T_STRING)) {
-	ID id =3D rb_check_id_without_pindown(&idx);
+	ID id =3D rb_check_id(&idx);
 	if (!id) {
 	    rb_name_error_str(idx, "no member '%"PRIsVALUE"' in struct",
 			      QUOTE(idx));
Index: thread.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- thread.c	(revision 46756)
+++ thread.c	(working copy)
@@ -2820,7 +2820,7 @@ rb_thread_local_aref(VALUE thread, ID id
 static VALUE
 rb_thread_aref(VALUE thread, VALUE key)
 {
-    ID id =3D rb_check_id_without_pindown(&key);
+    ID id =3D rb_check_id(&key);
     if (!id) return Qnil;
     return rb_thread_local_aref(thread, id);
 }
@@ -2906,7 +2906,7 @@ static VALUE
 rb_thread_variable_get(VALUE thread, VALUE key)
 {
     VALUE locals;
-    ID id =3D rb_check_id_without_pindown(&key);
+    ID id =3D rb_check_id(&key);
=20
     if (!id) return Qnil;
     locals =3D rb_ivar_get(thread, id_locals);
@@ -2952,7 +2952,7 @@ static VALUE
 rb_thread_key_p(VALUE self, VALUE key)
 {
     rb_thread_t *th;
-    ID id =3D rb_check_id_without_pindown(&key);
+    ID id =3D rb_check_id(&key);
=20
     GetThreadPtr(self, th);
=20
@@ -3073,7 +3073,7 @@ static VALUE
 rb_thread_variable_p(VALUE thread, VALUE key)
 {
     VALUE locals;
-    ID id =3D rb_check_id_without_pindown(&key);
+    ID id =3D rb_check_id(&key);
=20
     if (!id) return Qfalse;
=20
Index: variable.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- variable.c	(revision 46756)
+++ variable.c	(working copy)
@@ -353,7 +353,7 @@ rb_path_to_class(VALUE pathname)
     }
     while (*p) {
 	while (*p && *p !=3D ':') p++;
-	id =3D rb_check_id_cstr_without_pindown(pbeg, p-pbeg, enc);
+	id =3D rb_check_id_cstr(pbeg, p-pbeg, enc);
 	if (p[0] =3D=3D ':') {
 	    if (p[1] !=3D ':') goto undefined_class;
 	    p +=3D 2;
@@ -1404,7 +1404,7 @@ VALUE
 rb_obj_remove_instance_variable(VALUE obj, VALUE name)
 {
     VALUE val =3D Qnil;
-    const ID id =3D rb_check_id_without_pindown(&name);
+    const ID id =3D rb_check_id(&name);
     st_data_t n, v;
     struct st_table *iv_index_tbl;
     st_data_t index;
@@ -1920,7 +1920,7 @@ rb_public_const_get_at(VALUE klass, ID i
 VALUE
 rb_mod_remove_const(VALUE mod, VALUE name)
 {
-    const ID id =3D rb_check_id_without_pindown(&name);
+    const ID id =3D rb_check_id(&name);
=20
     if (!id) {
 	if (rb_is_const_name(name)) {
@@ -2569,7 +2569,7 @@ rb_mod_class_variables(int argc, const V
 VALUE
 rb_mod_remove_cvar(VALUE mod, VALUE name)
 {
-    const ID id =3D rb_check_id_without_pindown(&name);
+    const ID id =3D rb_check_id(&name);
     st_data_t val, n =3D id;
=20
     if (!id) {
Index: vm_method.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- vm_method.c	(revision 46756)
+++ vm_method.c	(working copy)
@@ -797,7 +797,7 @@ rb_mod_remove_method(int argc, VALUE *ar
=20
     for (i =3D 0; i < argc; i++) {
 	VALUE v =3D argv[i];
-	ID id =3D rb_check_id_without_pindown(&v);
+	ID id =3D rb_check_id(&v);
 	if (!id) {
 	    rb_name_error_str(v, "method `%s' not defined in %s",
 			      RSTRING_PTR(v), rb_class2name(mod));
@@ -1008,7 +1008,7 @@ rb_mod_undef_method(int argc, VALUE *arg
     int i;
     for (i =3D 0; i < argc; i++) {
 	VALUE v =3D argv[i];
-	ID id =3D rb_check_id_without_pindown(&v);
+	ID id =3D rb_check_id(&v);
 	if (!id) {
 	    rb_method_name_error(mod, v);
 	}
@@ -1048,7 +1048,7 @@ rb_mod_undef_method(int argc, VALUE *arg
 static VALUE
 rb_mod_method_defined(VALUE mod, VALUE mid)
 {
-    ID id =3D rb_check_id_without_pindown(&mid);
+    ID id =3D rb_check_id(&mid);
     if (!id || !rb_method_boundp(mod, id, 1)) {
 	return Qfalse;
     }
@@ -1062,7 +1062,7 @@ static VALUE
 check_definition(VALUE mod, VALUE mid, rb_method_flag_t noex)
 {
     const rb_method_entry_t *me;
-    ID id =3D rb_check_id_without_pindown(&mid);
+    ID id =3D rb_check_id(&mid);
     if (!id) return Qfalse;
     me =3D rb_method_entry(mod, id, 0);
     if (me) {
@@ -1691,7 +1691,7 @@ obj_respond_to(int argc, VALUE *argv, VA
     ID id;
=20
     rb_scan_args(argc, argv, "11", &mid, &priv);
-    if (!(id =3D rb_check_id_without_pindown(&mid))) {
+    if (!(id =3D rb_check_id(&mid))) {
 	if (!rb_method_basic_definition_p(CLASS_OF(obj), idRespond_to_missing)) {
 	    VALUE args[2];
 	    args[0] =3D ID2SYM(rb_to_id(mid));
```




--=20
https://bugs.ruby-lang.org/

In This Thread

Prev Next