From 1a06bee027d5c5b65ed0aaee76ee0040554d4efd Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Sun, 5 Jan 2025 21:42:23 -0500 Subject: [PATCH] Do not intern invalid symbols in eval parse When the inner code cannot represent the name of the locals in the outer code, do not bother putting them into the constant pool as they will not be referenced. Fixes [Bug #20992] Co-authored-by: Nobuyoshi Nakada --- test/.excludes/TestVariable.rb | 3 --- vm_eval.c | 10 ++++++++++ 2 files changed, 10 insertions(+), 3 deletions(-) delete mode 100644 test/.excludes/TestVariable.rb diff --git a/test/.excludes/TestVariable.rb b/test/.excludes/TestVariable.rb deleted file mode 100644 index ecfbb0e3b8..0000000000 --- a/test/.excludes/TestVariable.rb +++ /dev/null @@ -1,3 +0,0 @@ -if RUBY_DESCRIPTION.include?("+PRISM") - exclude(:test_local_variables_encoding, "[Bug #20992]") -end diff --git a/vm_eval.c b/vm_eval.c index eaf59f41a3..a8c12707ec 100644 --- a/vm_eval.c +++ b/vm_eval.c @@ -1690,6 +1690,8 @@ pm_eval_make_iseq(VALUE src, VALUE fname, int line, // scopes array refer to root nodes on the tree, and higher indexes are the // leaf nodes. iseq = parent; + rb_encoding *encoding = rb_enc_get(src); + for (int scopes_index = 0; scopes_index < scopes_count; scopes_index++) { VALUE iseq_value = (VALUE)iseq; int locals_count = ISEQ_BODY(iseq)->local_table_size; @@ -1711,6 +1713,14 @@ pm_eval_make_iseq(VALUE src, VALUE fname, int line, continue; } + // Check here if this local can be represented validly in the + // encoding of the source string. If it _cannot_, then it should + // not be added to the constant pool as it would not be able to + // be referenced anyway. + if (rb_enc_str_coderange_scan(name_obj, encoding) == ENC_CODERANGE_BROKEN) { + continue; + } + /* We need to duplicate the string because the Ruby string may * be embedded so compaction could move the string and the pointer * will change. */