From 55de873f660b725470340ad737aec1340fcf6a26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Rombauts?= Date: Sat, 4 Jan 2020 18:54:27 +0100 Subject: [PATCH] Fix Database::getHeaderInfo() for case where the file is not even 100 bytes long --- src/Database.cpp | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/Database.cpp b/src/Database.cpp index 5f6f5b4..48248a1 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -287,28 +287,32 @@ Header Database::getHeaderInfo(const std::string& aFilename) if (aFilename.empty()) { - throw SQLite::Exception("Could not open database, the aFilename parameter was empty."); + throw SQLite::Exception("Filename parameter is empty"); } - std::ifstream fileBuffer(aFilename.c_str(), std::ios::in | std::ios::binary); - - if (fileBuffer.is_open()) { - fileBuffer.seekg(0, std::ios::beg); - fileBuffer.read(pBuf, 100); - fileBuffer.close(); - strncpy(pHeaderStr, pBuf, 16); - } - - else - { - throw SQLite::Exception("Error opening file: " + aFilename); + std::ifstream fileBuffer(aFilename.c_str(), std::ios::in | std::ios::binary); + if (fileBuffer.is_open()) + { + fileBuffer.seekg(0, std::ios::beg); + fileBuffer.read(pBuf, 100); + fileBuffer.close(); + if (fileBuffer.gcount() < 100) + { + throw SQLite::Exception("File " + aFilename + " is too short"); + } + } + else + { + throw SQLite::Exception("Error opening file " + aFilename); + } } // If the "magic string" can't be found then header is invalid, corrupt or unreadable + strncpy(pHeaderStr, pBuf, 16); if (!strncmp(pHeaderStr, "SQLite format 3", 15) == 0) { - throw SQLite::Exception("Invalid or encrypted SQLite header"); + throw SQLite::Exception("Invalid or encrypted SQLite header in file " + aFilename); } h.pageSizeBytes = (buf[16] << 8) | buf[17];