aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-02-20 08:27:14 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-02-20 08:27:14 +0000
commit471457733e77cdaeeb138e9df331dcf3ccb4f6c1 (patch)
tree86169c77877901abb39b0b559b7aed7af226777e
parentb386332792332f6e136ede00e2b3f61dacd80b13 (diff)
downloadruby-471457733e77cdaeeb138e9df331dcf3ccb4f6c1.tar.gz
file.c: fix handle leak
* file.c (rb_file_identical_p): fix handle leak, ensure to close the handle of the first argument. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49664 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--file.c30
-rw-r--r--test/ruby/test_file_exhaustive.rb2
3 files changed, 35 insertions, 2 deletions
diff --git a/ChangeLog b/ChangeLog
index 74e8386bad..facc8cbccc 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Fri Feb 20 17:27:12 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * file.c (rb_file_identical_p): fix handle leak, ensure to close
+ the handle of the first argument.
+
Fri Feb 20 17:19:23 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
* win32/win32.c (different_device_p): compare by volume serial
diff --git a/file.c b/file.c
index 01370030fe..44cf4ae3bf 100644
--- a/file.c
+++ b/file.c
@@ -1053,6 +1053,25 @@ w32_io_info(VALUE *file, BY_HANDLE_FILE_INFORMATION *st)
if (ret) CloseHandle(ret);
return INVALID_HANDLE_VALUE;
}
+
+static VALUE
+close_handle(VALUE h)
+{
+ CloseHandle((HANDLE)h);
+ return Qfalse;
+}
+
+struct w32_io_info_args {
+ VALUE *fname;
+ BY_HANDLE_FILE_INFORMATION *st;
+};
+
+static VALUE
+call_w32_io_info(VALUE arg)
+{
+ struct w32_io_info_args *p = (void *)arg;
+ return (VALUE)w32_io_info(p->fname, p->st);
+}
#endif
/*
@@ -1916,8 +1935,15 @@ rb_file_identical_p(VALUE obj, VALUE fname1, VALUE fname2)
# ifdef _WIN32
f1 = w32_io_info(&fname1, &st1);
if (f1 == INVALID_HANDLE_VALUE) return Qfalse;
- f2 = w32_io_info(&fname2, &st2);
- if (f1) CloseHandle(f1);
+ if (f1) {
+ struct w32_io_info_args arg;
+ arg.fname = &fname2;
+ arg.st = &st2;
+ f2 = (HANDLE)rb_ensure(call_w32_io_info, (VALUE)&arg, close_handle, (VALUE)f1);
+ }
+ else {
+ f2 = w32_io_info(&fname2, &st2);
+ }
if (f2 == INVALID_HANDLE_VALUE) return Qfalse;
if (f2) CloseHandle(f2);
diff --git a/test/ruby/test_file_exhaustive.rb b/test/ruby/test_file_exhaustive.rb
index 84a6181e89..b347e53d16 100644
--- a/test/ruby/test_file_exhaustive.rb
+++ b/test/ruby/test_file_exhaustive.rb
@@ -311,6 +311,8 @@ class TestFileExhaustive < Test::Unit::TestCase
assert_raise(IOError) {
File.identical?(@file, io)
}
+ File.unlink(@file)
+ assert_file.not_exist?(@file)
end
def test_s_size