diff options
author | Jeffrey Walton <noloader@gmail.com> | 2020-09-20 11:43:03 -0400 |
---|---|---|
committer | Jeffrey Walton <noloader@gmail.com> | 2020-09-20 11:43:03 -0400 |
commit | d97aede813f309158e698189f0aba9fefe09f0f0 (patch) | |
tree | cda57e4cb4a529f08e94a72b973da96de94d2ba3 /files.cpp | |
parent | 58e7247a9c7e96de4320e0b6b33d698bd45277cd (diff) | |
download | cryptopp-git-d97aede813f309158e698189f0aba9fefe09f0f0.tar.gz |
Guard for exceptions in FileStore::MaxRetrievable (GH #968)
Diffstat (limited to 'files.cpp')
-rw-r--r-- | files.cpp | 36 |
1 files changed, 35 insertions, 1 deletions
@@ -6,8 +6,39 @@ #include "files.h"
+#include <iostream>
+#include <fstream>
#include <limits>
+ANONYMOUS_NAMESPACE_BEGIN
+
+/// \brief Disable badbit, failbit and eof exceptions
+/// \sa https://github.com/weidai11/cryptopp/pull/968 and
+/// https://www.cplusplus.com/reference/ios/ios/exceptions
+class IosExceptionMask
+{
+public:
+ IosExceptionMask(std::istream& stream) : m_stream(stream) {
+ m_mask = m_stream.exceptions();
+ m_stream.exceptions(static_cast<std::ios::iostate>(0));
+ }
+
+ IosExceptionMask(std::istream& stream, std::ios::iostate newMask) : m_stream(stream) {
+ m_mask = m_stream.exceptions();
+ m_stream.exceptions(newMask);
+ }
+
+ ~IosExceptionMask() {
+ m_stream.exceptions(m_mask);
+ }
+
+private:
+ std::istream& m_stream;
+ std::ios::iostate m_mask;
+};
+
+ANONYMOUS_NAMESPACE_END
+
NAMESPACE_BEGIN(CryptoPP)
#if defined(CRYPTOPP_DEBUG) && !defined(CRYPTOPP_DOXYGEN_PROCESSING)
@@ -65,6 +96,9 @@ lword FileStore::MaxRetrievable() const if (!m_stream)
return 0;
+ // Disable badbit, failbit and eof exceptions
+ IosExceptionMask guard(*m_stream);
+
// Clear error bits due to seekg(). Also see
// https://github.com/weidai11/cryptopp/pull/968
std::streampos current = m_stream->tellg();
@@ -74,7 +108,7 @@ lword FileStore::MaxRetrievable() const m_stream->clear();
// Return max for a non-seekable stream
- // https://www.cplusplus.com/reference/istream/istream/tellg/
+ // https://www.cplusplus.com/reference/istream/istream/tellg
if (end == static_cast<std::streampos>(-1))
return LWORD_MAX;
|