From 712f28879bb7a8b34da6adc470531c77964d3955 Mon Sep 17 00:00:00 2001 From: Ruoshi Zhang Date: Tue, 11 Jan 2022 14:43:26 +0100 Subject: [PATCH] Fix seg fault when memorymapped handles files that end exactly at the end of page --- src/util/convertalignments.cpp | 6 ++++-- src/util/gff2db.cpp | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/util/convertalignments.cpp b/src/util/convertalignments.cpp index 79e38e04d..f0d889309 100644 --- a/src/util/convertalignments.cpp +++ b/src/util/convertalignments.cpp @@ -102,8 +102,9 @@ std::map readKeyToSet(const std::string& file) { MemoryMapped lookup(file, MemoryMapped::WholeFile, MemoryMapped::SequentialScan); char* data = (char *) lookup.getData(); + char* end = data + lookup.mappedSize(); const char* entry[255]; - while (*data != '\0') { + while (data < end && *data != '\0') { const size_t columns = Util::getWordsOfLine(data, entry, 255); if (columns < 3) { Debug(Debug::WARNING) << "Not enough columns in lookup file " << file << "\n"; @@ -125,8 +126,9 @@ std::map readSetToSource(const std::string& file) { MemoryMapped source(file, MemoryMapped::WholeFile, MemoryMapped::SequentialScan); char* data = (char *) source.getData(); + char* end = data + source.mappedSize(); const char* entry[255]; - while (*data != '\0') { + while (data < end && *data != '\0') { const size_t columns = Util::getWordsOfLine(data, entry, 255); if (columns < 2) { Debug(Debug::WARNING) << "Not enough columns in lookup file " << file << "\n"; diff --git a/src/util/gff2db.cpp b/src/util/gff2db.cpp index 92daa0bba..6266e08f7 100644 --- a/src/util/gff2db.cpp +++ b/src/util/gff2db.cpp @@ -84,8 +84,9 @@ int gff2db(int argc, const char **argv, const Command &command) { EXIT(EXIT_FAILURE); } char *data = (char *) file.getData(); + char* end = data + file.mappedSize(); size_t idx = 0; - while (*data != '\0') { + while (data < end && *data != '\0') { // line is a comment or empty if (*data == '#' || *data == '\n') { data = Util::skipLine(data);