Skip to content

Commit e818abc

Browse files
zhileireniche033
andauthored
Fix crash when calling absPath with empty input (#620)
Signed-off-by: Zhilei Ren <[email protected]> Co-authored-by: Ian Chen <[email protected]>
1 parent 85b12c6 commit e818abc

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/Filesystem.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,14 @@ std::string common::copyToUnixPath(const std::string &_path)
135135
/////////////////////////////////////////////////
136136
std::string common::absPath(const std::string &_path)
137137
{
138-
return fs::absolute(_path).string();
138+
std::error_code ec;
139+
auto path = fs::absolute(_path, ec);
140+
if (!fsWarn("absPath", ec))
141+
{
142+
path = "";
143+
}
144+
145+
return path.string();
139146
}
140147

141148
/////////////////////////////////////////////////

src/Filesystem_TEST.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,3 +636,13 @@ TEST_F(FilesystemTest, separator)
636636
EXPECT_EQ("\\", gz::common::separator(""));
637637
#endif
638638
}
639+
640+
/////////////////////////////////////////////////
641+
TEST_F(FilesystemTest, empty)
642+
{
643+
#ifdef __APPLE__
644+
EXPECT_EQ(common::absPath(""), gz::common::cwd() + "/");
645+
#else
646+
EXPECT_EQ(common::absPath(""), "");
647+
#endif
648+
}

0 commit comments

Comments
 (0)