From baa1e8790d6c8d04de5a371a1c231ca9f2caa3b0 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Thu, 1 Sep 2011 12:38:36 -0700 Subject: [PATCH] Check error code in rust_file_is_dir. Prevent comparison of uninitialized mem --- src/rt/rust_builtin.cpp | 4 +++- src/test/stdtest/fs.rs | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/rt/rust_builtin.cpp b/src/rt/rust_builtin.cpp index 1b57d0adb83..3e89d12e438 100644 --- a/src/rt/rust_builtin.cpp +++ b/src/rt/rust_builtin.cpp @@ -446,7 +446,9 @@ rust_dirent_filename(rust_task *task, dirent* ent) { extern "C" CDECL int rust_file_is_dir(rust_task *task, rust_str *path) { struct stat buf; - stat((char*)path->data, &buf); + if (stat((char*)path->data, &buf)) { + return 0; + } return S_ISDIR(buf.st_mode); } diff --git a/src/test/stdtest/fs.rs b/src/test/stdtest/fs.rs index be920418e16..a3711b23954 100644 --- a/src/test/stdtest/fs.rs +++ b/src/test/stdtest/fs.rs @@ -14,3 +14,8 @@ fn test_connect() { #[test] fn test_list_dir_no_invalid_memory_access() { fs::list_dir(~"."); } +#[test] +fn file_is_dir() { + assert fs::file_is_dir(~"."); + assert !fs::file_is_dir(~"test/stdtest/fs.rs"); +} \ No newline at end of file