From d97aede813f309158e698189f0aba9fefe09f0f0 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Sun, 20 Sep 2020 11:43:03 -0400 Subject: Guard for exceptions in FileStore::MaxRetrievable (GH #968) --- files.cpp | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'files.cpp') 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 +#include #include +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(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(-1)) return LWORD_MAX; -- cgit v1.2.1