Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

load.c : add String's subclass to prevent double expansion of load path.

  • Loading branch information...
commit 98bbe30badeb85a3c704d6dfdef7d66a82ee86cd 1 parent a8a212a
Sokolov Yura authored

Showing 3 changed files with 20 additions and 0 deletions. Show diff stats Hide diff stats

  1. +14 0 file.c
  2. +1 0  internal.h
  3. +5 0 load.c
14 file.c
@@ -167,6 +167,10 @@ rb_get_path_check(VALUE obj, int level)
167 167 }
168 168 StringValue(tmp);
169 169
  170 + if (RBASIC(obj)->klass == rb_cExpandedPath) {
  171 + return obj;
  172 + }
  173 +
170 174 tmp = file_path_convert(tmp);
171 175 if (obj != tmp && insecure_obj_p(tmp, level)) {
172 176 rb_insecure_operation();
@@ -2904,6 +2908,16 @@ file_expand_path(VALUE fname, VALUE dname, int abs_mode, VALUE result)
2904 2908 BUFINIT();
2905 2909 tainted = OBJ_TAINTED(fname);
2906 2910
  2911 + if (RBASIC(fname)->klass == rb_cExpandedPath) {
  2912 + size_t dlen = RSTRING_LEN(fname);
  2913 + BUFCHECK(dlen > buflen);
  2914 + strncpy(buf, RSTRING_PTR(fname), dlen + 1);
  2915 + rb_str_set_len(result, dlen);
  2916 + rb_enc_associate(result, rb_enc_check(result, fname));
  2917 + ENC_CODERANGE_CLEAR(result);
  2918 + return result;
  2919 + }
  2920 +
2907 2921 if (s[0] == '~' && abs_mode == 0) { /* execute only if NOT absolute_path() */
2908 2922 long userlen = 0;
2909 2923 tainted = 1;
1  internal.h
@@ -120,6 +120,7 @@ VALUE rb_get_load_path(void);
120 120 NORETURN(void rb_load_fail(VALUE, const char*));
121 121 void rb_reset_expanded_cache();
122 122 void rb_load_path_ary_push(VALUE path);
  123 +extern VALUE rb_cExpandedPath;
123 124
124 125 /* math.c */
125 126 VALUE rb_math_atan2(VALUE, VALUE);
5 load.c
@@ -32,6 +32,7 @@ static VALUE rb_checked_expanded_cache(int*);
32 32 static void rb_set_expanded_cache(VALUE, int);
33 33 static VALUE rb_expand_load_paths(long, VALUE*, int*);
34 34 static int cached_expanded_load_path = 1;
  35 +VALUE rb_cExpandedPath;
35 36
36 37 VALUE
37 38 rb_get_expanded_load_path(void)
@@ -900,6 +901,7 @@ rb_expand_load_paths(long pathc, VALUE* paths, int *has_relative)
900 901 p = RSTRING_PTR(path);
901 902 *has_relative = *has_relative || !rb_is_absolute_path(p);
902 903 path = rb_file_expand_path(path, Qnil);
  904 + RBASIC(path)->klass = rb_cExpandedPath;
903 905 rb_str_freeze(path);
904 906 rb_ary_push(expanded, path);
905 907 }
@@ -992,6 +994,9 @@ rb_load_path_init(void)
992 994 cached_expanded_load_path = atoi(cached_flag);
993 995 }
994 996
  997 + rb_cExpandedPath = rb_class_new(rb_cString); /* XXX could GC collect it before next line is executed? */
  998 + rb_iv_set(rb_cFile, "expanded_path", rb_cExpandedPath); /* prevent from GC */
  999 +
995 1000 /* Do all the magick if user did not disable it
996 1001 * with RUBY_CACHED_LOAD_PATH=0 environment variable
997 1002 */

0 comments on commit 98bbe30

Please sign in to comment.
Something went wrong with that request. Please try again.