Skip to content

Commit

Permalink
Fix crash when calling absPath with empty input (#620)
Browse files Browse the repository at this point in the history
Signed-off-by: Zhilei Ren <zren@dlut.edu.cn>
Co-authored-by: Ian Chen <ichen@openrobotics.org>
  • Loading branch information
gzfuzz and iche033 authored Aug 9, 2024
1 parent 85b12c6 commit e818abc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Filesystem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,14 @@ std::string common::copyToUnixPath(const std::string &_path)
/////////////////////////////////////////////////
std::string common::absPath(const std::string &_path)
{
return fs::absolute(_path).string();
std::error_code ec;
auto path = fs::absolute(_path, ec);
if (!fsWarn("absPath", ec))
{
path = "";
}

return path.string();
}

/////////////////////////////////////////////////
Expand Down
10 changes: 10 additions & 0 deletions src/Filesystem_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -636,3 +636,13 @@ TEST_F(FilesystemTest, separator)
EXPECT_EQ("\\", gz::common::separator(""));
#endif
}

/////////////////////////////////////////////////
TEST_F(FilesystemTest, empty)
{
#ifdef __APPLE__
EXPECT_EQ(common::absPath(""), gz::common::cwd() + "/");
#else
EXPECT_EQ(common::absPath(""), "");
#endif
}

0 comments on commit e818abc

Please sign in to comment.