From 7f2a212c6a371785270def09c3eb1f44e85237d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Jaworski?= Date: Thu, 28 Feb 2019 15:38:43 +0100 Subject: [PATCH] catching bad alloc --- concordia/concordia.cpp | 76 ++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 36 deletions(-) diff --git a/concordia/concordia.cpp b/concordia/concordia.cpp index 41448e2..783de43 100644 --- a/concordia/concordia.cpp +++ b/concordia/concordia.cpp @@ -119,48 +119,52 @@ void Concordia::loadRAMIndexFromDisk() { if (boost::filesystem::exists(_getWordMapFilePath()) && boost::filesystem::exists(_getHashedIndexFilePath()) && boost::filesystem::exists(_getMarkersFilePath())) { - // reading index from file - _T->clear(); - std::ifstream hashedIndexFile; - hashedIndexFile.open( - _getHashedIndexFilePath().c_str(), std::ios::in - | std::ios::ate | std::ios::binary); - saidx_t hiFileSize = hashedIndexFile.tellg(); - if (hiFileSize > 0) { - hashedIndexFile.seekg(0, std::ios::beg); + try { + // reading index from file + _T->clear(); + std::ifstream hashedIndexFile; + hashedIndexFile.open( + _getHashedIndexFilePath().c_str(), std::ios::in + | std::ios::ate | std::ios::binary); + long hiFileSize = hashedIndexFile.tellg(); + if (hiFileSize > 0) { + hashedIndexFile.seekg(0, std::ios::beg); - while (!hashedIndexFile.eof()) { - INDEX_CHARACTER_TYPE character = - Utils::readIndexCharacter(hashedIndexFile); - Utils::appendCharToSaucharVector(_T, character); + while (!hashedIndexFile.eof()) { + INDEX_CHARACTER_TYPE character = + Utils::readIndexCharacter(hashedIndexFile); + Utils::appendCharToSaucharVector(_T, character); + } + hashedIndexFile.close(); + } else { + hashedIndexFile.close(); + throw ConcordiaException("Index corrupt: empty hash index file"); } - hashedIndexFile.close(); - } else { - hashedIndexFile.close(); - throw ConcordiaException("Index corrupt: empty hash index file"); - } - // reading markers from file - _markers->clear(); - std::ifstream markersFile; - markersFile.open(_getMarkersFilePath().c_str(), std::ios::in - | std::ios::ate | std::ios::binary); - long maFileSize = markersFile.tellg(); - if (maFileSize > 0) { - markersFile.seekg(0, std::ios::beg); + // reading markers from file + _markers->clear(); + std::ifstream markersFile; + markersFile.open(_getMarkersFilePath().c_str(), std::ios::in + | std::ios::ate | std::ios::binary); + long maFileSize = markersFile.tellg(); + if (maFileSize > 0) { + markersFile.seekg(0, std::ios::beg); - while (!markersFile.eof()) { - SUFFIX_MARKER_TYPE marker = - Utils::readMarker(markersFile); - _markers->push_back(marker); + while (!markersFile.eof()) { + SUFFIX_MARKER_TYPE marker = + Utils::readMarker(markersFile); + _markers->push_back(marker); + } + markersFile.close(); + } else { + markersFile.close(); + throw ConcordiaException("Index corrupt: empty markers file"); } - markersFile.close(); - } else { - markersFile.close(); - throw ConcordiaException("Index corrupt: empty markers file"); + // generating suffix array + _SA = _index->generateSuffixArray(_T); + } catch (const std::bad_alloc&) { + throw ConcordiaException("Error allocating memory, probably out of memory."); } - // generating suffix array - _SA = _index->generateSuffixArray(_T); } else { throw ConcordiaException("Index corrupt: missing files"); }