1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* dir.c (check_dirname): check string safety and remove extraneous

trailing directory separators.  [ruby-dev:22279]

* file.c: extern rb_path_next, rb_path_skip_prefix,
  rb_path_last_separator, rb_path_end.

* intern.h: prototypes for rb_path_next, rb_path_skip_prefix,
  rb_path_last_separator, rb_path_end.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@5191 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2003-12-15 04:07:03 +00:00
parent 8e43498222
commit 12b5b81a97
4 changed files with 44 additions and 26 deletions

View file

@ -1,3 +1,14 @@
Mon Dec 15 13:06:58 2003 Nobuyoshi Nakada <nobu@ruby-lang.org>
* dir.c (check_dirname): check string safety and remove extraneous
trailing directory separators. [ruby-dev:22279]
* file.c: extern rb_path_next, rb_path_skip_prefix,
rb_path_last_separator, rb_path_end.
* intern.h: prototypes for rb_path_next, rb_path_skip_prefix,
rb_path_last_separator, rb_path_end.
Mon Dec 15 09:27:46 2003 NAKAMURA Usaku <usa@ruby-lang.org> Mon Dec 15 09:27:46 2003 NAKAMURA Usaku <usa@ruby-lang.org>
* ext/openssl/ossl_pkcs12.c (ossl_pkcs12_initialize): first argument * ext/openssl/ossl_pkcs12.c (ossl_pkcs12_initialize): first argument

37
dir.c
View file

@ -87,21 +87,10 @@ char *strchr _((char*,char));
#if defined DOSISH #if defined DOSISH
#define isdirsep(c) ((c) == '/' || (c) == '\\') #define isdirsep(c) ((c) == '/' || (c) == '\\')
static const char *
find_dirsep(s)
const char *s;
{
while (*s) {
if (isdirsep(*s))
return s;
s = CharNext(s);
}
return 0;
}
#else #else
#define isdirsep(c) ((c) == '/') #define isdirsep(c) ((c) == '/')
#define find_dirsep(s) strchr(s, '/')
#endif #endif
#define find_dirsep(s) rb_path_next(s)
static char * static char *
range(pat, test, flags) range(pat, test, flags)
@ -509,13 +498,27 @@ dir_s_getwd(dir)
return cwd; return cwd;
} }
static void check_dirname _((volatile VALUE *));
static void
check_dirname(dir)
volatile VALUE *dir;
{
char *path, *pend;
SafeStringValue(*dir);
rb_secure(2);
path = RSTRING(*dir)->ptr;
if (*(path && pend = rb_path_end(rb_path_skip_prefix(path)))) {
*dir = rb_str_new(path, pend - path);
}
}
static VALUE static VALUE
dir_s_chroot(dir, path) dir_s_chroot(dir, path)
VALUE dir, path; VALUE dir, path;
{ {
#if defined(HAVE_CHROOT) && !defined(__CHECKER__) #if defined(HAVE_CHROOT) && !defined(__CHECKER__)
rb_secure(2); check_dirname(&path);
SafeStringValue(path);
if (chroot(RSTRING(path)->ptr) == -1) if (chroot(RSTRING(path)->ptr) == -1)
rb_sys_fail(RSTRING(path)->ptr); rb_sys_fail(RSTRING(path)->ptr);
@ -543,8 +546,7 @@ dir_s_mkdir(argc, argv, obj)
mode = 0777; mode = 0777;
} }
SafeStringValue(path); check_dirname(&path);
rb_secure(2);
#ifndef _WIN32 #ifndef _WIN32
if (mkdir(RSTRING(path)->ptr, mode) == -1) if (mkdir(RSTRING(path)->ptr, mode) == -1)
rb_sys_fail(RSTRING(path)->ptr); rb_sys_fail(RSTRING(path)->ptr);
@ -560,8 +562,7 @@ static VALUE
dir_s_rmdir(obj, dir) dir_s_rmdir(obj, dir)
VALUE obj, dir; VALUE obj, dir;
{ {
SafeStringValue(dir); check_dirname(&dir);
rb_secure(2);
if (rmdir(RSTRING(dir)->ptr) < 0) if (rmdir(RSTRING(dir)->ptr) < 0)
rb_sys_fail(RSTRING(dir)->ptr); rb_sys_fail(RSTRING(dir)->ptr);

18
file.c
View file

@ -1459,7 +1459,8 @@ skiproot(path)
return (char *)path; return (char *)path;
} }
static inline char * #define nextdirsep rb_path_next
char *
nextdirsep(s) nextdirsep(s)
const char *s; const char *s;
{ {
@ -1469,11 +1470,12 @@ nextdirsep(s)
return (char *)s; return (char *)s;
} }
#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER) #define skipprefix rb_path_skip_prefix
static inline char * char *
skipprefix(path) skipprefix(path)
const char *path; const char *path;
{ {
#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
#ifdef DOSISH_UNC #ifdef DOSISH_UNC
if (isdirsep(path[0]) && isdirsep(path[1])) { if (isdirsep(path[0]) && isdirsep(path[1])) {
if (*(path = nextdirsep(path + 2))) if (*(path = nextdirsep(path + 2)))
@ -1484,14 +1486,13 @@ skipprefix(path)
#ifdef DOSISH_DRIVE_LETTER #ifdef DOSISH_DRIVE_LETTER
if (has_drive_letter(path)) if (has_drive_letter(path))
return (char *)(path + 2); return (char *)(path + 2);
#endif
#endif #endif
return (char *)path; return (char *)path;
} }
#else
#define skipprefix(path) (path)
#endif
static char * #define strrdirsep rb_path_last_separator
char *
strrdirsep(path) strrdirsep(path)
const char *path; const char *path;
{ {
@ -1510,7 +1511,8 @@ strrdirsep(path)
return last; return last;
} }
static char * #define chompdirsep rb_path_end
char *
chompdirsep(path) chompdirsep(path)
const char *path; const char *path;
{ {

View file

@ -222,6 +222,10 @@ VALUE rb_file_s_expand_path _((int, VALUE *));
void rb_file_const _((const char*, VALUE)); void rb_file_const _((const char*, VALUE));
int rb_find_file_ext _((VALUE*, const char* const*)); int rb_find_file_ext _((VALUE*, const char* const*));
VALUE rb_find_file _((VALUE)); VALUE rb_find_file _((VALUE));
char *rb_path_next _((const char *));
char *rb_path_skip_prefix _((const char *));
char *rb_path_last_separator _((const char *));
char *rb_path_end _((const char *));
/* gc.c */ /* gc.c */
NORETURN(void rb_memerror __((void))); NORETURN(void rb_memerror __((void)));
int ruby_stack_check _((void)); int ruby_stack_check _((void));