summaryrefslogtreecommitdiff
path: root/files.cpp
diff options
context:
space:
mode:
authorJeffrey Walton <noloader@gmail.com>2020-09-20 11:43:03 -0400
committerJeffrey Walton <noloader@gmail.com>2020-09-20 11:43:03 -0400
commitd97aede813f309158e698189f0aba9fefe09f0f0 (patch)
treecda57e4cb4a529f08e94a72b973da96de94d2ba3 /files.cpp
parent58e7247a9c7e96de4320e0b6b33d698bd45277cd (diff)
downloadcryptopp-git-d97aede813f309158e698189f0aba9fefe09f0f0.tar.gz
Guard for exceptions in FileStore::MaxRetrievable (GH #968)
Diffstat (limited to 'files.cpp')
-rw-r--r--files.cpp36
1 files changed, 35 insertions, 1 deletions
diff --git a/files.cpp b/files.cpp
index 22af7097..79ea6418 100644
--- a/files.cpp
+++ b/files.cpp
@@ -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;