Skip to content

Commit

Permalink
Fix possible buffer overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
kamahen committed Dec 25, 2023
1 parent 91ea422 commit e58ebbf
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
5 changes: 3 additions & 2 deletions libhdt/src/libdcs/CSD_FMIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ 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);
text[reservedSize-1] = '\0'; // shouldn't be needed, but make compiler happy

total += currentLength;

Expand All @@ -118,7 +119,7 @@ CSD_FMIndex::CSD_FMIndex(hdt::IteratorUCharString *it, bool sparse_bitsequence,
textFinal = new char[total + 1];
// cout<<"testing:total cpy:"<<total<<endl;
// cout<<"testing:text:"<<text<<endl;
strncpy((char *)(textFinal), (char *)text, total);
strncpy((char *)(textFinal), (char *)text, total + 1);
textFinal[total] = '\0'; // end of the text
// cout<<"testing:textFinal:"<<textFinal<<endl;

Expand Down
13 changes: 8 additions & 5 deletions libhdt/src/libdcs/CSD_HTFC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ CSD_HTFC::CSD_HTFC(hdt::IteratorUCharString *it, uint32_t blocksize,

// The string is explicitly copied to the
// encoded sequence.
strncpy((char *)(textfc + bytesfc), (char *)currentStr, currentLength);
strncpy((char *)(textfc + bytesfc), (char *)currentStr, reservedSize - bytesfc);
textfc[reservedSize-1] = '\0'; // shouldn't be needed, but make compiler happy
bytesfc += currentLength;

// cout << nblocks-1 << "," << length << " => " << currentStr << endl;
Expand All @@ -113,7 +114,8 @@ 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);
textfc[reservedSize-1] = '\0';
bytesfc += currentLength - delta;
// cout << nblocks-1 << "," << length << " => " << currentStr << endl;
}
Expand Down Expand Up @@ -333,8 +335,8 @@ void CSD_HTFC::dumpBlock(uint block) {
uint idInBlock = 0;

// Reading the first string
strncpy((char *)string, (char *)(text + pos), slen);
string[slen] = '\0';
strncpy((char *)string, (char *)(text + pos), maxlength + 1);
string[maxlength] = '\0';
pos += slen;

cout << block * blocksize + idInBlock << " (" << idInBlock << ") => "
Expand All @@ -352,7 +354,8 @@ 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 + 1 - delta);
string[maxlength] = '\0'; // shouldn't be needed, but make compiler happy

cout << block * blocksize + idInBlock << " (" << idInBlock << ") => "
<< string << " Delta=" << delta << " Len=" << slen << endl;
Expand Down
6 changes: 4 additions & 2 deletions libhdt/src/libdcs/CSD_PFC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ 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);
text[reservedSize-1] = '\0'; // shouldn't be needed, but make compiler happy
bytes += currentLength;
} else {
// Regular string
Expand All @@ -96,7 +97,8 @@ 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);
text[reservedSize-1] = '\0';
bytes += currentLength - delta;
}

Expand Down
2 changes: 1 addition & 1 deletion libhdt/src/triples/TripleListDisk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ void TripleListDisk::insert(TripleID &triple)

//cout << "Insert: " <<&pointer[numTotalTriples] << "* "<< triple << " "<<sizeof(TripleID) << endl;

memcpy(&arrayTriples[numTotalTriples], &triple, sizeof(TripleID));
arrayTriples[numTotalTriples] = triple;
numTotalTriples++;
numValidTriples++;
//cout << "Inserted: "<< numTotalTriples << endl;
Expand Down

0 comments on commit e58ebbf

Please sign in to comment.