@@ -93,15 +93,13 @@ namespace {
std::string fileStrerror(int errNum)
{
#ifdef __MINGW32__
- static char buf[256];
- if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
- 0, errNum,
- // Default language
- MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), (LPTSTR)&buf,
- sizeof(buf), 0) == 0) {
+ auto msg = util::formatLastError(errNum);
+ if (msg.empty()) {
+ char buf[256];
snprintf(buf, sizeof(buf), "File I/O error %x", errNum);
+ return buf;
}
- return buf;
+ return msg;
#else // !__MINGW32__
return util::safeStrerror(errNum);
#endif // !__MINGW32__
@@ -105,14 +105,13 @@ std::string errorMsg(int errNum)
#ifndef __MINGW32__
#else
- nullptr, errNum,
- sizeof(buf), nullptr) == 0) {
snprintf(buf, sizeof(buf), EX_SOCKET_UNKNOWN_ERROR, errNum, errNum);
#endif // __MINGW32__
} // namespace
@@ -2087,6 +2087,24 @@ TLSVersion toTLSVersion(const std::string& ver)
#endif // ENABLE_SSL
+
+#ifdef __MINGW32__
+std::string formatLastError(int errNum)
+{
+ std::array<char, 4_k> buf;
+ if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
+ nullptr, errNum,
+ // Default language
+ MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
+ static_cast<LPTSTR>(buf.data()), buf.size(),
+ nullptr) == 0) {
+ return "";
+ }
+ return buf.data();
+}
+#endif // __MINGW32__
} // namespace util
} // namespace aria2
@@ -855,6 +855,13 @@ bool tlsHostnameMatch(const std::string& pattern, const std::string& hostname);
TLSVersion toTLSVersion(const std::string& ver);
+// Formats error message for error code errNum, which is the return
+// value of GetLastError(). On error, this function returns empty
+// string.
+std::string formatLastError(int errNum);