diff options
Diffstat (limited to 'string.c')
-rw-r--r-- | string.c | 108 |
1 files changed, 60 insertions, 48 deletions
@@ -301,27 +301,79 @@ fstr_update_callback(st_data_t *key, st_data_t *value, st_data_t arg, int existi } } +static int +tainted_fstr_update(st_data_t *key, st_data_t *val, st_data_t arg, int existing) +{ + VALUE *fstr = (VALUE *)arg; + VALUE str = (VALUE)*key; + + if (existing) { + /* because of lazy sweep, str may be unmarked already and swept + * at next time */ + if (rb_objspace_garbage_object_p(str)) { + *fstr = Qundef; + return ST_DELETE; + } + + *fstr = str; + return ST_STOP; + } + else { + str = rb_str_resurrect(str); + RB_OBJ_TAINT_RAW(str); + RB_FL_SET_RAW(str, RSTRING_FSTR); + RB_OBJ_FREEZE_RAW(str); + + *key = *val = *fstr = str; + return ST_CONTINUE; + } +} + +static VALUE +register_fstring_tainted(VALUE str, st_table *tfstrings) +{ + st_data_t fstr; + + do { + fstr = (st_data_t)str; + st_update(tfstrings, fstr, tainted_fstr_update, (st_data_t)&fstr); + } while ((VALUE)fstr == Qundef); + + str = (VALUE)fstr; + assert(OBJ_FROZEN_RAW(str)); + assert(!FL_TEST_RAW(str, STR_FAKESTR)); + assert(!FL_TEST_RAW(str, FL_EXIVAR)); + assert(FL_TEST_RAW(str, RSTRING_FSTR)); + assert(FL_TEST_RAW(str, FL_TAINT)); + assert(RBASIC_CLASS(str) == rb_cString); + + return str; +} + RUBY_FUNC_EXPORTED VALUE rb_fstring(VALUE str) { VALUE fstr; - int bare; + int bare_ish; Check_Type(str, T_STRING); if (FL_TEST(str, RSTRING_FSTR)) return str; - bare = BARE_STRING_P(str); - if (STR_EMBED_P(str) && !bare) { + bare_ish = !FL_TEST_RAW(str, FL_EXIVAR) && RBASIC_CLASS(str) == rb_cString; + if (STR_EMBED_P(str) && !bare_ish) { OBJ_FREEZE_RAW(str); return str; } - - fstr = register_fstring(str); - - if (!bare) { + if (!FL_TEST_RAW(str, FL_TAINT)) { + fstr = register_fstring(str); + } + else { + fstr = register_fstring_tainted(str, rb_vm_tfstring_table()); + } + if (!bare_ish) { str_replace_shared_without_enc(str, fstr); OBJ_FREEZE_RAW(str); return str; @@ -349,34 +401,6 @@ register_fstring(VALUE str) return ret; } -static int -tainted_fstr_update(st_data_t *key, st_data_t *val, st_data_t arg, int existing) -{ - VALUE *fstr = (VALUE *)arg; - VALUE str = (VALUE)*key; - - if (existing) { - /* because of lazy sweep, str may be unmarked already and swept - * at next time */ - if (rb_objspace_garbage_object_p(str)) { - *fstr = Qundef; - return ST_DELETE; - } - - *fstr = str; - return ST_STOP; - } - else { - str = rb_str_resurrect(str); - RB_OBJ_TAINT_RAW(str); - RB_FL_SET_RAW(str, RSTRING_FSTR); - RB_OBJ_FREEZE_RAW(str); - - *key = *val = *fstr = str; - return ST_CONTINUE; - } -} - static VALUE rb_fstring_existing0(VALUE str) { @@ -414,20 +438,8 @@ rb_tainted_fstring_existing(VALUE str) if (!RB_FL_TEST_RAW(ret, RSTRING_FSTR)) { return Qnil; } - do { - fstr = (st_data_t)ret; - st_update(tfstrings, fstr, tainted_fstr_update, (st_data_t)&fstr); - } while ((VALUE)fstr == Qundef); - ret = (VALUE)fstr; - assert(OBJ_FROZEN_RAW(ret)); - assert(!FL_TEST_RAW(ret, STR_FAKESTR)); - assert(!FL_TEST_RAW(ret, FL_EXIVAR)); - assert(FL_TEST_RAW(ret, RSTRING_FSTR)); - assert(FL_TEST_RAW(ret, FL_TAINT)); - assert(RBASIC_CLASS(ret) == rb_cString); - - return ret; + return register_fstring_tainted(str, tfstrings); } VALUE |