Guard for exceptions in FileStore::MaxRetrievable (GH #968)

This commit is contained in:
Jeffrey Walton 2020-09-20 11:43:03 -04:00
parent 58e7247a9c
commit d97aede813
No known key found for this signature in database
GPG Key ID: B36AB348921B1838

View File

@ -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;