From fe80dd73b9a5021e2e2cfe61cda87512f8ee4c53 Mon Sep 17 00:00:00 2001 From: nobu Date: Sat, 23 Feb 2008 18:53:00 +0000 Subject: * util.c (valid_filename): use O_EXCL to get rid of clobbering existing files in race conditions. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15586 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- util.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) (limited to 'util.c') diff --git a/util.c b/util.c index 8a00aedf60..136ff1b213 100644 --- a/util.c +++ b/util.c @@ -345,23 +345,19 @@ valid_filename(const char *s) { int fd; - /* - // if the file exists, then it's a valid filename! - */ - - if (_access(s, 0) == 0) { - return 1; - } - /* // It doesn't exist, so see if we can open it. */ - if ((fd = _open(s, O_CREAT, 0666)) >= 0) { + if ((fd = _open(s, O_CREAT|O_EXCL, 0666)) >= 0) { _close(fd); _unlink(s); /* don't leave it laying around */ return 1; } + else if (errno == EEXIST) { + /* if the file exists, then it's a valid filename! */ + return 1; + } return 0; } #endif -- cgit v1.2.3