From 59f6e3fdc333284ea5396fa03c205179d82b64b4 Mon Sep 17 00:00:00 2001 From: Peter Ludemann Date: Thu, 27 Jun 2024 17:33:30 -0700 Subject: [PATCH] Fix "sz" parameter in calls to strncpy() --- libhdt/src/libdcs/CSD_FMIndex.cpp | 6 +++--- libhdt/src/libdcs/CSD_HTFC.cpp | 8 ++++---- libhdt/src/libdcs/CSD_PFC.cpp | 4 ++-- libhdt/src/triples/TripleListDisk.cpp | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/libhdt/src/libdcs/CSD_FMIndex.cpp b/libhdt/src/libdcs/CSD_FMIndex.cpp index f61ec93f..00a3a6b2 100644 --- a/libhdt/src/libdcs/CSD_FMIndex.cpp +++ b/libhdt/src/libdcs/CSD_FMIndex.cpp @@ -90,7 +90,7 @@ CSD_FMIndex::CSD_FMIndex(hdt::IteratorUCharString *it, bool sparse_bitsequence, // Checking the current size of the encoded // sequence: realloc if necessary if ((total + currentLength + 1) > reservedSize) { - while (((size_t)total + currentLength + 1) > reservedSize) { + while ((total + currentLength + 1) > reservedSize) { reservedSize <<= 1; if (reservedSize == 0) { reservedSize = ((size_t)total + currentLength) * 2; @@ -99,7 +99,7 @@ CSD_FMIndex::CSD_FMIndex(hdt::IteratorUCharString *it, bool sparse_bitsequence, text = (unsigned char *)realloc(text, reservedSize * sizeof(unsigned char)); } - strncpy((char *)(text + total), (char *)currentStr, currentLength); + strncpy((char *)(text + total), (char *)currentStr, reservedSize - total); total += currentLength; @@ -118,7 +118,7 @@ CSD_FMIndex::CSD_FMIndex(hdt::IteratorUCharString *it, bool sparse_bitsequence, textFinal = new char[total + 1]; // cout<<"testing:total cpy:"< " << currentStr << endl; @@ -113,7 +113,7 @@ CSD_HTFC::CSD_HTFC(hdt::IteratorUCharString *it, uint32_t blocksize, // The suffix is copied to the sequence strncpy((char *)(textfc + bytesfc), (char *)currentStr + delta, - currentLength - delta); + reservedSize - bytesfc); bytesfc += currentLength - delta; // cout << nblocks-1 << "," << length << " => " << currentStr << endl; } @@ -333,7 +333,7 @@ void CSD_HTFC::dumpBlock(uint block) { uint idInBlock = 0; // Reading the first string - strncpy((char *)string, (char *)(text + pos), slen); + strncpy((char *)string, (char *)(text + pos), maxlength + 1); string[slen] = '\0'; pos += slen; @@ -352,7 +352,7 @@ void CSD_HTFC::dumpBlock(uint block) { // Copying the suffix slen = strlen((char *)text + pos) + 1; - strncpy((char *)(string + delta), (char *)(text + pos), slen); + strncpy((char *)(string + delta), (char *)(text + pos), maxlength - delta + 1); cout << block * blocksize + idInBlock << " (" << idInBlock << ") => " << string << " Delta=" << delta << " Len=" << slen << endl; diff --git a/libhdt/src/libdcs/CSD_PFC.cpp b/libhdt/src/libdcs/CSD_PFC.cpp index a0f876c0..f6dc3447 100644 --- a/libhdt/src/libdcs/CSD_PFC.cpp +++ b/libhdt/src/libdcs/CSD_PFC.cpp @@ -81,7 +81,7 @@ CSD_PFC::CSD_PFC(hdt::IteratorUCharString *it, uint32_t blocksize, nblocks++; // The string is explicitly copied to the encoded sequence. - strncpy((char *)(text + bytes), (char *)currentStr, currentLength); + strncpy((char *)(text + bytes), (char *)currentStr, reservedSize - bytes); bytes += currentLength; } else { // Regular string @@ -96,7 +96,7 @@ CSD_PFC::CSD_PFC(hdt::IteratorUCharString *it, uint32_t blocksize, // The suffix is copied to the sequence strncpy((char *)(text + bytes), (char *)currentStr + delta, - currentLength - delta); + reservedSize - bytes); bytes += currentLength - delta; } diff --git a/libhdt/src/triples/TripleListDisk.cpp b/libhdt/src/triples/TripleListDisk.cpp index 0f4ec37c..ae575150 100644 --- a/libhdt/src/triples/TripleListDisk.cpp +++ b/libhdt/src/triples/TripleListDisk.cpp @@ -303,7 +303,7 @@ void TripleListDisk::insert(TripleID &triple) //cout << "Insert: " <<&pointer[numTotalTriples] << "* "<< triple << " "<