From 03d1c9cd8238af6f1063ea4eb98d17fa2a511107 Mon Sep 17 00:00:00 2001 From: matz Date: Sat, 14 Jul 2001 15:17:19 +0000 Subject: * regex.c (re_search): should consider reverse search. * dir.c (dir_s_chdir): warn only when invoked from multiple threads or block is not given. * object.c (rb_convert_type): should use rb_rescue(), not rb_rescue2(). * range.c (range_init): ditto. * object.c (rb_obj_dup): should free generic_ivar if original owns them. * string.c (rb_str_each_line): should propagate taint mark. * ext/nkf/nkf.c (rb_nkf_kconv): ditto. * eval.c (rb_f_require): revamp for simpler implementation. * file.c (rb_find_file_noext): use String object, instead of passing char* around. * file.c (rb_find_file): ditto. * dln.c (dln_load): should use NSLINKMODULE_OPTION_BINDNOW. * ruby.c (load_file): local variables 'c' remain uninitialized on xflag. * regex.c (re_match): prefetched escaped character too early. * eval.c (rb_call0): add argument check for attr_readers. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1612 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- file.c | 96 +++++++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 54 insertions(+), 42 deletions(-) (limited to 'file.c') diff --git a/file.c b/file.c index 66f82bcc58..60cab5ba80 100644 --- a/file.c +++ b/file.c @@ -2202,26 +2202,27 @@ is_macos_native_path(path) } #endif -static char* +static int file_load_ok(file) char *file; { FILE *f; + if (!f) return 0; f = fopen(file, "r"); if (f == NULL) return 0; fclose(f); - return file; + return 1; } extern VALUE rb_load_path; int -rb_find_file_noext(file) - char *file; +rb_find_file_noext(filep) + VALUE *filep; { char *path, *e, *found; - char *fend = file + strlen(file); + char *f = RSTRING(*filep)->ptr; VALUE fname; int i, j; @@ -2233,16 +2234,22 @@ rb_find_file_noext(file) 0 }; - if (file[0] == '~') { - fname = rb_str_new2(file); + if (f[0] == '~') { + fname = *filep; fname = rb_file_s_expand_path(1, &fname); - file = StringValuePtr(fname); + if (rb_safe_level() >= 2 && OBJ_TAINTED(fname)) { + rb_raise(rb_eSecurityError, "loading from unsafe file %s", f); + } } - if (is_absolute_path(file)) { + if (is_absolute_path(f)) { for (i=0; ext[i]; i++) { - strcpy(fend, ext[i]); - if (file_load_ok(file)) return i+1; + fname = rb_str_dup(*filep); + rb_str_cat2(fname, ext[i]); + if (file_load_ok(RSTRING(fname)->ptr)) { + *filep = fname; + return i+1; + } } return 0; } @@ -2256,71 +2263,76 @@ rb_find_file_noext(file) SafeStringValue(str); path = RSTRING(str)->ptr; for (j=0; ext[j]; j++) { - strcpy(fend, ext[j]); - found = dln_find_file(file, path); - if (found && file_load_ok(found)) return j+1; + fname = rb_str_dup(*filep); + rb_str_cat2(fname, ext[j]); + found = dln_find_file(RSTRING(fname)->ptr, path); + if (found && file_load_ok(found)) { + *filep = fname; + return j+1; + } } } return 0; } -char* -rb_find_file(file) - char *file; +VALUE +rb_find_file(path) + VALUE path; { - VALUE vpath, fname; - char *path; + VALUE tmp, fname; + char *f = RSTRING(path)->ptr; + char *lpath; struct stat st; - if (file[0] == '~') { - fname = rb_str_new2(file); - fname = rb_file_s_expand_path(1, &fname); - if (rb_safe_level() >= 2 && OBJ_TAINTED(fname)) { - rb_raise(rb_eSecurityError, "loading from unsafe file %s", file); + if (f[0] == '~') { + tmp = rb_file_s_expand_path(1, &path); + if (rb_safe_level() >= 2 && OBJ_TAINTED(tmp)) { + rb_raise(rb_eSecurityError, "loading from unsafe file %s", f); } - file = StringValuePtr(fname); } #if defined(__MACOS__) || defined(riscos) - if (is_macos_native_path(file)) { - if (rb_safe_level() >= 2 && !rb_path_check(file)) { - rb_raise(rb_eSecurityError, "loading from unsafe file %s", file); + if (is_macos_native_path(f)) { + if (rb_safe_level() >= 2 && !rb_path_check(f)) { + rb_raise(rb_eSecurityError, "loading from unsafe file %s", f); } - return file_load_ok(file); + if (file_load_ok(f)) return path; } #endif - if (is_absolute_path(file)) { - if (rb_safe_level() >= 2 && !rb_path_check(file)) { - rb_raise(rb_eSecurityError, "loading from unsafe file %s", file); + if (is_absolute_path(f)) { + if (rb_safe_level() >= 2 && !rb_path_check(f)) { + rb_raise(rb_eSecurityError, "loading from unsafe file %s", f); } - return file_load_ok(file); + if (file_load_ok(f)) return path; } if (rb_load_path) { int i; Check_Type(rb_load_path, T_ARRAY); - vpath = rb_ary_new(); + tmp = rb_ary_new(); for (i=0;ilen;i++) { VALUE str = RARRAY(rb_load_path)->ptr[i]; SafeStringValue(str); if (RSTRING(str)->len > 0) { - rb_ary_push(vpath, str); + rb_ary_push(tmp, str); } } - vpath = rb_ary_join(vpath, rb_str_new2(PATH_SEP)); - path = StringValuePtr(vpath); - if (rb_safe_level() >= 2 && !rb_path_check(path)) { - rb_raise(rb_eSecurityError, "loading from unsafe path %s", path); + tmp = rb_ary_join(tmp, rb_str_new2(PATH_SEP)); + lpath = StringValuePtr(tmp); + if (rb_safe_level() >= 2 && !rb_path_check(lpath)) { + rb_raise(rb_eSecurityError, "loading from unsafe path %s", lpath); } } else { - path = 0; + lpath = 0; } - path = dln_find_file(file, path); - return file_load_ok(path); + f = dln_find_file(f, lpath); + if (file_load_ok(f)) { + return rb_str_new2(f); + } } static void -- cgit v1.2.3