From ea7e249079c86e502d2af4c6c896952dd5cad150 Mon Sep 17 00:00:00 2001 From: nobu Date: Thu, 26 Jul 2007 04:38:07 +0000 Subject: * dln.c (conv_to_posix_path): removed. * ruby.c (rubylib_mangled_path, rubylib_mangled_path2): return VALUE instead of a pointer to static buffer. * ruby.c (push_include_cygwin): fixed buffer overflow. [ruby-dev:31297] * ruby.c (ruby_init_loadpath): not convert built-in paths. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12847 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ruby.c | 201 +++++++++++++++++++++++++++++++++++------------------------------ 1 file changed, 109 insertions(+), 92 deletions(-) (limited to 'ruby.c') diff --git a/ruby.c b/ruby.c index 89aeae9857..279fe4d0b6 100644 --- a/ruby.c +++ b/ruby.c @@ -14,6 +14,7 @@ #ifdef __CYGWIN__ #include +#include #endif #ifdef _WIN32_WCE #include @@ -114,116 +115,142 @@ usage(const char *name) extern VALUE rb_load_path; -#define STATIC_FILE_LENGTH 255 +#ifndef CharNext /* defined as CharNext[AW] on Windows. */ +#define CharNext(p) ((p) + mblen(p, RUBY_MBCHAR_MAXSIZE)) +#endif + +#if defined DOSISH || defined __CYGWIN__ +static inline void +translate_char(char *p, int from, int to) +{ + while (*p) { + if ((unsigned char)*p == from) + *p = to; + p = CharNext(p); + } +} +#endif #if defined _WIN32 || defined __CYGWIN__ || defined __DJGPP__ -static char * -rubylib_mangle(const char *s, unsigned int l) +static VALUE +rubylib_mangled_path(const char *s, unsigned int l) { static char *newp, *oldp; static int newl, oldl, notfound; - static char newsub[STATIC_FILE_LENGTH + 1]; + char *ptr; + VALUE ret; if (!newp && !notfound) { newp = getenv("RUBYLIB_PREFIX"); if (newp) { - char *s; - - oldp = newp; + oldp = newp = strdup(newp); while (*newp && !ISSPACE(*newp) && *newp != ';') { - newp++; - oldl++; /* Skip digits. */ + newp = CharNext(newp); /* Skip digits. */ } + oldl = newp - oldp; while (*newp && (ISSPACE(*newp) || *newp == ';')) { - newp++; /* Skip whitespace. */ + newp = CharNext(newp); /* Skip whitespace. */ } newl = strlen(newp); - if (newl == 0 || oldl == 0 || newl > STATIC_FILE_LENGTH) { + if (newl == 0 || oldl == 0) { rb_fatal("malformed RUBYLIB_PREFIX"); } - strcpy(newsub, newp); - s = newsub; - while (*s) { - if (*s == '\\') - *s = '/'; - s++; - } + translate_char(newp, '\\', '/'); } else { notfound = 1; } } - if (l == 0) { - l = strlen(s); - } if (!newp || l < oldl || strncasecmp(oldp, s, oldl) != 0) { - static char ret[STATIC_FILE_LENGTH + 1]; - strncpy(ret, s, l); - ret[l] = 0; - return ret; - } - if (l + newl - oldl > STATIC_FILE_LENGTH || newl > STATIC_FILE_LENGTH) { - rb_fatal("malformed RUBYLIB_PREFIX"); + return rb_str_new(s, l); } - strcpy(newsub + newl, s + oldl); - newsub[l + newl - oldl] = 0; - return newsub; + ret = rb_str_new(0, l + newl - oldl); + ptr = RSTRING_PTR(ret); + memcpy(ptr, newp, newl); + memcpy(ptr + newl, s + oldl, l - oldl); + ptr[l + newl - oldl] = 0; + return ret; } -#define rubylib_mangled_path(s, l) rb_str_new2(rubylib_mangle((s), (l))) -#define rubylib_mangled_path2(s) rb_str_new2(rubylib_mangle((s), 0)) +static VALUE +rubylib_mangled_path2(const char *s) +{ + return rubylib_mangled_path(s, strlen(s)); +} #else -#define rubylib_mangled_path(s, l) rb_str_new((s), (l)) -#define rubylib_mangled_path2(s) rb_str_new2(s) +#define rubylib_mangled_path rb_str_new +#define rubylib_mangled_path2 rb_str_new2 #endif -void -ruby_push_include(const char *path, VALUE (*filter) (VALUE)) +static void +push_include(const char *path, VALUE (*filter)(VALUE)) { const char sep = PATH_SEP_CHAR; + const char *p, *s; - if (path == 0) - return; -#if defined(__CYGWIN__) - { - char rubylib[FILENAME_MAX]; - conv_to_posix_path(path, rubylib, FILENAME_MAX); - path = rubylib; + p = path; + while (*p) { + while (*p == sep) + p++; + if (!*p) break; + for (s = p; *s && *s != sep; s = CharNext(s)); + rb_ary_push(rb_load_path, (*filter)(rubylib_mangled_path(p, s - p))); + p = s; } -#endif - if (strchr(path, sep)) { - const char *p, *s; - VALUE ary = rb_ary_new(); - - p = path; - while (*p) { - while (*p == sep) - p++; - if ((s = strchr(p, sep)) != 0) { - rb_ary_push(ary, - (*filter) (rubylib_mangled_path - (p, (int)(s - p)))); - p = s + 1; +} + +#ifdef __CYGWIN__ +static void +push_include_cygwin(const char *path, VALUE (*filter)(VALUE)) +{ + const char *p, *s; + char rubylib[FILENAME_MAX]; + VALUE buf = 0; + + p = path; + while (*p) { + unsigned int len; + while (*p == ';') + p++; + if (!*p) break; + for (s = p; *s && *s != ';'; s = CharNext(s)); + len = s - p; + if (*s) { + if (!buf) { + buf = rb_str_new(p, len); + p = RSTRING_PTR(buf); } else { - rb_ary_push(ary, (*filter) (rubylib_mangled_path2(p))); - break; + rb_str_resize(buf, len); + p = strncpy(RSTRING_PTR(buf), p, len); } } - rb_ary_concat(rb_load_path, ary); - } - else { - rb_ary_push(rb_load_path, (*filter) (rubylib_mangled_path2(path))); + if (cygwin_conv_to_posix_path(p, rubylib) == 0) + p = rubylib; + push_include(p, filter); + if (!*s) break; + p = s + 1; } } +#define push_include push_include_cygwin +#endif + +void +ruby_push_include(const char *path, VALUE (*filter)(VALUE)) +{ + if (path == 0) + return; + push_include(path, filter); +} + static VALUE identical_path(VALUE path) { return path; } -void +void ruby_incpush(const char *path) { ruby_push_include(path, identical_path); @@ -240,7 +267,6 @@ expand_include_path(VALUE path) return rb_file_expand_path(path, Qnil); } - void ruby_incpush_expand(const char *path) { @@ -251,22 +277,6 @@ ruby_incpush_expand(const char *path) #define LOAD_RELATIVE 1 #endif -#if defined DOSISH || defined __CYGWIN__ -static inline void -translate_char(char *p, int from, int to) -{ - while (*p) { - if ((unsigned char)*p == from) - *p = to; -#ifdef CharNext /* defined as CharNext[AW] on Windows. */ - p = CharNext(p); -#else - p += mblen(p, RUBY_MBCHAR_MAXSIZE); -#endif - } -} -#endif - #if defined _WIN32 || defined __CYGWIN__ static HMODULE libruby; @@ -300,8 +310,14 @@ ruby_init_loadpath(void) #endif libpath[sizeof(libpath) - 1] = '\0'; -#if defined DOSISH || defined __CYGWIN__ +#if defined DOSISH translate_char(libpath, '\\', '/'); +#elif defined __CYGWIN__ + { + char rubylib[FILENAME_MAX]; + cygwin_conv_to_posix_path(libpath, rubylib); + strncpy(libpath, rubylib, sizeof(libpath)); + } #endif p = strrchr(libpath, '/'); if (p) { @@ -322,30 +338,31 @@ ruby_init_loadpath(void) #else #define RUBY_RELATIVE(path) (path) #endif +#define incpush(path) rb_ary_push(rb_load_path, rubylib_mangled_path2(path)) if (rb_safe_level() == 0) { ruby_incpush(getenv("RUBYLIB")); } #ifdef RUBY_SEARCH_PATH - ruby_incpush(RUBY_RELATIVE(RUBY_SEARCH_PATH)); + incpush(RUBY_RELATIVE(RUBY_SEARCH_PATH)); #endif - ruby_incpush(RUBY_RELATIVE(RUBY_SITE_LIB2)); + incpush(RUBY_RELATIVE(RUBY_SITE_LIB2)); #ifdef RUBY_SITE_THIN_ARCHLIB - ruby_incpush(RUBY_RELATIVE(RUBY_SITE_THIN_ARCHLIB)); + incpush(RUBY_RELATIVE(RUBY_SITE_THIN_ARCHLIB)); #endif - ruby_incpush(RUBY_RELATIVE(RUBY_SITE_ARCHLIB)); - ruby_incpush(RUBY_RELATIVE(RUBY_SITE_LIB)); + incpush(RUBY_RELATIVE(RUBY_SITE_ARCHLIB)); + incpush(RUBY_RELATIVE(RUBY_SITE_LIB)); - ruby_incpush(RUBY_RELATIVE(RUBY_LIB)); + incpush(RUBY_RELATIVE(RUBY_LIB)); #ifdef RUBY_THIN_ARCHLIB - ruby_incpush(RUBY_RELATIVE(RUBY_THIN_ARCHLIB)); + incpush(RUBY_RELATIVE(RUBY_THIN_ARCHLIB)); #endif - ruby_incpush(RUBY_RELATIVE(RUBY_ARCHLIB)); + incpush(RUBY_RELATIVE(RUBY_ARCHLIB)); if (rb_safe_level() == 0) { - ruby_incpush("."); + incpush("."); } } -- cgit v1.2.3