From 42de8af413596a544c5438222260d3a16483be8e Mon Sep 17 00:00:00 2001 From: nobu Date: Thu, 20 Feb 2003 09:11:05 +0000 Subject: * file.c (file_expand_path): should not upward beyond share name. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3512 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 4 ++++ file.c | 50 +++++++++++++++++++++++++++++++++++++------------- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6650b0a0ef..ba37bfd1a6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +Thu Feb 20 18:11:01 2003 Nobuyoshi Nakada + + * file.c (file_expand_path): should not upward beyond share name. + Thu Feb 20 15:45:33 2003 WATANABE Hirofumi * missing.h (strtoul): fix prototype of strtoul. diff --git a/file.c b/file.c index 581e490cac..3578b17a1e 100644 --- a/file.c +++ b/file.c @@ -1434,6 +1434,26 @@ nextdirsep(s) return (char *)s; } + +static inline char * +skipprefix(path) + const char *path; +{ +#ifdef DOSISH_UNC + if (isdirsep(path[0]) && isdirsep(path[1])) { + if (*(path = nextdirsep(path + 2))) + path = nextdirsep(path + 1); + return (char *)path; + } +#endif +#ifdef DOSISH_DRIVE_LETTER + if (has_drive_letter(path)) + return (char *)(path + 2); +#endif + while (isdirsep(*path)) path++; + return (char *)path; +} + static char * strrdirsep(path) const char *path; @@ -1494,7 +1514,7 @@ static VALUE file_expand_path(fname, dname, result) VALUE fname, dname, result; { - char *s, *buf, *b, *p, *pend; + char *s, *buf, *b, *p, *pend, *root; long buflen; int tainted; @@ -1594,14 +1614,8 @@ file_expand_path(fname, dname, result) #ifdef DOSISH if (isdirsep(*s)) { /* specified full path, but not drive letter nor UNC */ - if (has_drive_letter(buf)) { - /* we need to get the drive letter */ - p = &buf[2]; - } - else if (isdirsep(buf[0]) && isdirsep(buf[1])) { - /* or UNC share name */ - if (*(p = nextdirsep(buf + 2))) p = nextdirsep(p + 1); - } + /* we need to get the drive letter or UNC share name */ + p = skipprefix(buf); } else #endif @@ -1619,6 +1633,9 @@ file_expand_path(fname, dname, result) else *p = '/'; + p[1] = 0; + root = skipprefix(buf); + b = s; while (*s) { switch (*s) { @@ -1632,7 +1649,7 @@ file_expand_path(fname, dname, result) if (*(s+1) == '\0' || isdirsep(*(s+1))) { /* We must go back to the parent */ *p = '\0'; - if (!(b = strrdirsep(buf))) { + if (!(b = strrdirsep(root))) { *p = '/'; } else { @@ -1684,6 +1701,13 @@ file_expand_path(fname, dname, result) return result; } +VALUE +rb_file_expand_path(fname, dname) + VALUE fname, dname; +{ + return file_expand_path(fname, dname, rb_str_new(0, MAXPATHLEN + 2)); +} + VALUE rb_file_s_expand_path(argc, argv) int argc; @@ -1692,7 +1716,7 @@ rb_file_s_expand_path(argc, argv) VALUE fname, dname; rb_scan_args(argc, argv, "11", &fname, &dname); - return file_expand_path(fname, dname, rb_str_new(0, MAXPATHLEN + 2)); + return rb_file_expand_path(fname, dname); } static int @@ -2653,7 +2677,7 @@ rb_find_file_ext(filep, ext) long i, j; if (f[0] == '~') { - fname = rb_file_s_expand_path(1, filep); + fname = rb_file_expand_path(*filep, Qnil); if (rb_safe_level() >= 2 && OBJ_TAINTED(fname)) { rb_raise(rb_eSecurityError, "loading from unsafe file %s", f); } @@ -2704,7 +2728,7 @@ rb_find_file(path) char *lpath; if (f[0] == '~') { - path = rb_file_s_expand_path(1, &path); + path = rb_file_expand_path(path, Qnil); if (rb_safe_level() >= 2 && OBJ_TAINTED(path)) { rb_raise(rb_eSecurityError, "loading from unsafe file %s", f); } -- cgit v1.2.3