Skip to content

Commit

Permalink
fix(encodingcheck): not crash when filesize is zero
Browse files Browse the repository at this point in the history
  • Loading branch information
rsp4jack committed Mar 3, 2024
1 parent 5ec7c19 commit 282570e
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/encodingcheck/encodingcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ constexpr void print(std::format_string<Args...> fmt, Args&&... args)
template<class... Args>
constexpr void printerr(std::format_string<Args...> fmt, Args&&... args)
{
fflush(stderr);
std::fputs(std::format(fmt, std::forward<decltype(args)>(args)...).c_str(), stderr);
fflush(stderr);
}

namespace fs = std::filesystem;
Expand All @@ -41,6 +43,10 @@ bool checkutf8(fs::path file)
close(fd);
throw fs::filesystem_error("fstat()", std::error_code{errno, std::system_category()});
}
if (statbuf.st_size == 0) {
close(fd);
return true;
}
auto ptr = static_cast<char*>(mmap(nullptr, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0));
if (ptr == MAP_FAILED) {
throw fs::filesystem_error("mmap()", std::error_code{errno, std::system_category()});
Expand Down Expand Up @@ -92,6 +98,7 @@ int main()
catch(const std::exception& err){
printerr("ERROR {} ({})\n", entry.path().c_str(), mime);
printerr("{}", err.what());
throw err;
}
}

Expand Down

0 comments on commit 282570e

Please sign in to comment.