Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
load.c : add String's subclass to prevent double expansion of load path.
  • Loading branch information
funny-falcon committed Mar 31, 2012
commit 98bbe30badeb85a3c704d6dfdef7d66a82ee86cd
14 changes: 14 additions & 0 deletions file.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ rb_get_path_check(VALUE obj, int level)
}
StringValue(tmp);

if (RBASIC(obj)->klass == rb_cExpandedPath) {
return obj;
}

tmp = file_path_convert(tmp);
if (obj != tmp && insecure_obj_p(tmp, level)) {
rb_insecure_operation();
Expand Down Expand Up @@ -2904,6 +2908,16 @@ file_expand_path(VALUE fname, VALUE dname, int abs_mode, VALUE result)
BUFINIT();
tainted = OBJ_TAINTED(fname);

if (RBASIC(fname)->klass == rb_cExpandedPath) {
size_t dlen = RSTRING_LEN(fname);
BUFCHECK(dlen > buflen);
strncpy(buf, RSTRING_PTR(fname), dlen + 1);
rb_str_set_len(result, dlen);
rb_enc_associate(result, rb_enc_check(result, fname));
ENC_CODERANGE_CLEAR(result);
return result;
}

if (s[0] == '~' && abs_mode == 0) { /* execute only if NOT absolute_path() */
long userlen = 0;
tainted = 1;
Expand Down
1 change: 1 addition & 0 deletions internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ VALUE rb_get_load_path(void);
NORETURN(void rb_load_fail(VALUE, const char*));
void rb_reset_expanded_cache();
void rb_load_path_ary_push(VALUE path);
extern VALUE rb_cExpandedPath;

/* math.c */
VALUE rb_math_atan2(VALUE, VALUE);
Expand Down
5 changes: 5 additions & 0 deletions load.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ static VALUE rb_checked_expanded_cache(int*);
static void rb_set_expanded_cache(VALUE, int);
static VALUE rb_expand_load_paths(long, VALUE*, int*);
static int cached_expanded_load_path = 1;
VALUE rb_cExpandedPath;

VALUE
rb_get_expanded_load_path(void)
Expand Down Expand Up @@ -900,6 +901,7 @@ rb_expand_load_paths(long pathc, VALUE* paths, int *has_relative)
p = RSTRING_PTR(path);
*has_relative = *has_relative || !rb_is_absolute_path(p);
path = rb_file_expand_path(path, Qnil);
RBASIC(path)->klass = rb_cExpandedPath;
rb_str_freeze(path);
rb_ary_push(expanded, path);
}
Expand Down Expand Up @@ -992,6 +994,9 @@ rb_load_path_init(void)
cached_expanded_load_path = atoi(cached_flag);
}

rb_cExpandedPath = rb_class_new(rb_cString); /* XXX could GC collect it before next line is executed? */
rb_iv_set(rb_cFile, "expanded_path", rb_cExpandedPath); /* prevent from GC */

/* Do all the magick if user did not disable it
* with RUBY_CACHED_LOAD_PATH=0 environment variable
*/
Expand Down