From 83710b14ee8cf043576dba8f3707fc585067167e Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Thu, 2 Sep 2021 06:10:59 +0800 Subject: [PATCH] fix: util::read_files_in_dir does not always return a correct result (#21883) * fix bug * use de->d_type==DT_REG * Revert "use de->d_type==DT_REG" This reverts commit ecb38c82305ea23bfec84284aa77be5b5c64957a. --- selfdrive/common/tests/test_util.cc | 17 +++++++++++++++++ selfdrive/common/util.cc | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/selfdrive/common/tests/test_util.cc b/selfdrive/common/tests/test_util.cc index d61ca38886..abc5398db6 100644 --- a/selfdrive/common/tests/test_util.cc +++ b/selfdrive/common/tests/test_util.cc @@ -75,3 +75,20 @@ TEST_CASE("util::file_exists") { } ::remove(filename); } + +TEST_CASE("util::read_files_in_dir") { + char tmp_path[] = "/tmp/test_XXXXXX"; + const std::string test_path = mkdtemp(tmp_path); + const std::string files[] = {".test1", "'test2'", "test3"}; + for (auto fn : files) { + std::ofstream{test_path + "/" + fn} << fn; + } + mkdir((test_path + "/dir").c_str(), 0777); + + std::map result = util::read_files_in_dir(test_path); + REQUIRE(result.find("dir") == result.end()); + REQUIRE(result.size() == std::size(files)); + for (auto& [k, v] : result) { + REQUIRE(k == v); + } +} diff --git a/selfdrive/common/util.cc b/selfdrive/common/util.cc index 83114c9189..99cf2ae887 100644 --- a/selfdrive/common/util.cc +++ b/selfdrive/common/util.cc @@ -85,7 +85,7 @@ std::map read_files_in_dir(const std::string &path) { struct dirent *de = NULL; while ((de = readdir(d))) { - if (isalnum(de->d_name[0])) { + if (de->d_type != DT_DIR) { ret[de->d_name] = util::read_file(path + "/" + de->d_name); } }