Skip to content

Commit 16a723c

Browse files
authored
Merge pull request #5584 from dhthwy/filesystem_exceptions
use noexcept std::filesystem overloads
2 parents 4b0a10b + 2f54afa commit 16a723c

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

docs/changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Template for new versions:
5757

5858
## Fixes
5959
- `zone`: animal assignment dialog now tolerates corrupt animal-to-pasture links.
60+
- improved file system handling: gracefully handle errors from operations, preventing crashes.
6061

6162
## Misc Improvements
6263

library/Core.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,14 @@ std::filesystem::path Core::findScript(std::string name)
535535
getScriptPaths(&paths);
536536
for (auto it = paths.begin(); it != paths.end(); ++it)
537537
{
538-
std::filesystem::path path = std::filesystem::weakly_canonical(*it / name);
538+
std::error_code ec;
539+
std::filesystem::path path = std::filesystem::weakly_canonical(*it / name, ec);
540+
if (ec)
541+
{
542+
con.printerr("Error loading '%s' (%s)\n", (*it / name).string().c_str(), ec.message().c_str());
543+
continue;
544+
}
545+
539546
if (Filesystem::isfile(path))
540547
return path;
541548
}

library/modules/Filesystem.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,31 @@ bool Filesystem::stat (std::filesystem::path path, std::filesystem::file_status
154154

155155
bool Filesystem::exists (std::filesystem::path path) noexcept
156156
{
157-
return std::filesystem::exists(path);
157+
std::error_code ec;
158+
auto r = std::filesystem::exists(path, ec);
159+
if (ec)
160+
return false;
161+
return r;
158162
}
159163

160164
bool Filesystem::isfile(std::filesystem::path path) noexcept
161165
{
162-
return std::filesystem::exists(path) && std::filesystem::is_regular_file(path);
166+
std::error_code ec;
167+
// is_regular_file() also checks for existence.
168+
auto r = std::filesystem::is_regular_file(path, ec);
169+
if (ec)
170+
return false;
171+
return r;
163172
}
164173

165174
bool Filesystem::isdir (std::filesystem::path path) noexcept
166175
{
167-
return std::filesystem::exists(path) && std::filesystem::is_directory(path);
176+
std::error_code ec;
177+
// is_directory() also checks for existence.
178+
auto r = std::filesystem::is_directory(path, ec);
179+
if (ec)
180+
return false;
181+
return r;
168182
}
169183

170184
std::time_t Filesystem::mtime (std::filesystem::path path) noexcept

0 commit comments

Comments
 (0)