From 1780ad37483592d7752d314fbdb3bfd01966c229 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Mon, 24 Jul 2023 23:41:01 +0900 Subject: [PATCH] Extract magic numbers --- vm_eval.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/vm_eval.c b/vm_eval.c index 8e57ad4f43..6ee958b659 100644 --- a/vm_eval.c +++ b/vm_eval.c @@ -1615,13 +1615,17 @@ rb_each(VALUE obj) static VALUE eval_default_path = Qfalse; +#define EVAL_LOCATION_MARK "eval at " +#define EVAL_LOCATION_MARK_LEN (int)rb_strlen_lit(EVAL_LOCATION_MARK) + static VALUE get_eval_default_path(void) { int location_lineno; VALUE location_path = rb_source_location(&location_lineno); if (!NIL_P(location_path)) { - return rb_fstring(rb_sprintf("(eval at %"PRIsVALUE":%d)", location_path, location_lineno)); + return rb_fstring(rb_sprintf("("EVAL_LOCATION_MARK"%"PRIsVALUE":%d)", + location_path, location_lineno)); } if (!eval_default_path) { @@ -2527,9 +2531,11 @@ rb_current_realfilepath(void) } // [Feature #19755] implicit eval location is "(eval at #{__FILE__}:#{__LINE__})" - if (RSTRING_LEN(path) > 9) { - if (RSTRING_PTR(path)[RSTRING_LEN(path) - 1] == ')' && - memcmp(RSTRING_PTR(path), "(eval at ", 9) == 0) { + const long len = RSTRING_LEN(path); + if (len > EVAL_LOCATION_MARK_LEN+1) { + const char *const ptr = RSTRING_PTR(path); + if (ptr[len - 1] == ')' && + memcmp(ptr, "("EVAL_LOCATION_MARK, EVAL_LOCATION_MARK_LEN+1) == 0) { return Qnil; } }