summaryrefslogtreecommitdiffstats
path: root/libqpdf/QUtil.cc
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2013-02-28 23:10:30 +0100
committerJay Berkenbilt <ejb@ql.org>2013-03-05 19:35:46 +0100
commitfd649593983925356fcc61c04a75c9f55cee3648 (patch)
tree7a79631772422d1808872e50e21eb4178e2eddeb /libqpdf/QUtil.cc
parentac4deac1873ca1bb570ffd479ed2cc1010762f89 (diff)
downloadqpdf-fd649593983925356fcc61c04a75c9f55cee3648.tar.zst
Favor strerror_s and fopen_s on MSVC
Make remaining calls to fopen and strerror use strerror_s and fopen_s on MSVC.
Diffstat (limited to 'libqpdf/QUtil.cc')
-rw-r--r--libqpdf/QUtil.cc31
1 files changed, 28 insertions, 3 deletions
diff --git a/libqpdf/QUtil.cc b/libqpdf/QUtil.cc
index b7cdb9b2..adcb1515 100644
--- a/libqpdf/QUtil.cc
+++ b/libqpdf/QUtil.cc
@@ -96,7 +96,22 @@ QUtil::unsigned_char_pointer(char const* str)
void
QUtil::throw_system_error(std::string const& description)
{
- throw std::runtime_error(description + ": " + strerror(errno)); // XXXX
+#ifdef _MSC_VER
+ // "94" is mentioned in the MSVC docs, but it's still safe if the
+ // message is longer. strerror_s is a templated function that
+ // knows the size of buf and truncates.
+ char buf[94];
+ if (strerror_s(buf, errno) != 0)
+ {
+ throw std::runtime_error(description + ": failed with an unknown error");
+ }
+ else
+ {
+ throw std::runtime_error(description + ": " + buf);
+ }
+#else
+ throw std::runtime_error(description + ": " + strerror(errno));
+#endif
}
int
@@ -112,8 +127,18 @@ QUtil::os_wrapper(std::string const& description, int status)
FILE*
QUtil::safe_fopen(char const* filename, char const* mode)
{
- return fopen_wrapper(std::string("open ") + filename,
- fopen(filename, mode)); // XXXX
+ FILE* f = 0;
+#ifdef _MSC_VER
+ errno_t err = fopen_s(&f, filename, mode);
+ if (err != 0)
+ {
+ errno = err;
+ throw_system_error(std::string("open ") + filename);
+ }
+#else
+ f = fopen_wrapper(std::string("open ") + filename, fopen(filename, mode));
+#endif
+ return f;
}
FILE*