Improve error handling when loading file
* Handle IO errors when reading internal buffer * Throw an error when the decompressed or decrypted buffer is empty
This commit is contained in:
parent
7fd253f895
commit
60d8972dcb
|
@ -4,6 +4,7 @@
|
||||||
#include "./parsingexception.h"
|
#include "./parsingexception.h"
|
||||||
|
|
||||||
#include <c++utilities/conversion/stringconversion.h>
|
#include <c++utilities/conversion/stringconversion.h>
|
||||||
|
#include <c++utilities/conversion/stringbuilder.h>
|
||||||
#include <c++utilities/io/catchiofailure.h>
|
#include <c++utilities/io/catchiofailure.h>
|
||||||
|
|
||||||
#include <openssl/conf.h>
|
#include <openssl/conf.h>
|
||||||
|
@ -21,6 +22,7 @@
|
||||||
#include <streambuf>
|
#include <streambuf>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
using namespace ConversionUtilities;
|
||||||
using namespace IoUtilities;
|
using namespace IoUtilities;
|
||||||
|
|
||||||
namespace Io {
|
namespace Io {
|
||||||
|
@ -252,6 +254,9 @@ void PasswordFile::load()
|
||||||
throw CryptoException("Decrypted size is negative.");
|
throw CryptoException("Decrypted size is negative.");
|
||||||
}
|
}
|
||||||
remainingSize = static_cast<size_t>(decryptedSize);
|
remainingSize = static_cast<size_t>(decryptedSize);
|
||||||
|
if (!remainingSize) {
|
||||||
|
throw ParsingException("Decrypted buffer is empty.");
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// use raw data directly if not encrypted
|
// use raw data directly if not encrypted
|
||||||
|
@ -278,15 +283,28 @@ void PasswordFile::load()
|
||||||
remainingSize = decompressedSize;
|
remainingSize = decompressedSize;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!remainingSize) {
|
||||||
|
throw ParsingException("Decompressed buffer is empty.");
|
||||||
|
}
|
||||||
|
|
||||||
// parse contents
|
// parse contents
|
||||||
stringstream decryptedStream(stringstream::in | stringstream::out | stringstream::binary);
|
stringstream decryptedStream(stringstream::in | stringstream::out | stringstream::binary);
|
||||||
decryptedStream.rdbuf()->pubsetbuf(decryptedData.data(), static_cast<streamsize>(remainingSize));
|
decryptedStream.exceptions(ios_base::failbit | ios_base::badbit);
|
||||||
if (version >= 0x5u) {
|
try {
|
||||||
const auto extendedHeaderSize = m_freader.readUInt16BE();
|
decryptedStream.rdbuf()->pubsetbuf(decryptedData.data(), static_cast<streamsize>(remainingSize));
|
||||||
m_encryptedExtendedHeader = m_freader.readString(extendedHeaderSize);
|
if (version >= 0x5u) {
|
||||||
|
const auto extendedHeaderSize = m_freader.readUInt16BE();
|
||||||
|
m_encryptedExtendedHeader = m_freader.readString(extendedHeaderSize);
|
||||||
|
}
|
||||||
|
m_rootEntry.reset(new NodeEntry(decryptedStream));
|
||||||
|
} catch (...) {
|
||||||
|
const char *const what = catchIoFailure();
|
||||||
|
if (decryptedStream.eof()) {
|
||||||
|
throw ParsingException("The file seems to be truncated.");
|
||||||
|
}
|
||||||
|
throw ParsingException(argsToString("An IO error occurred when reading internal buffer: ", what));
|
||||||
}
|
}
|
||||||
m_rootEntry.reset(new NodeEntry(decryptedStream));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
|
Loading…
Reference in New Issue