Skip to content

Commit 7996ca6

Browse files
committed
Fix "sz" parameter in calls to strncpy() - possible buffer overflow or string truncation
1 parent b9a409d commit 7996ca6

4 files changed

Lines changed: 10 additions & 10 deletions

File tree

libhdt/src/libdcs/CSD_FMIndex.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ CSD_FMIndex::CSD_FMIndex(hdt::IteratorUCharString *it, bool sparse_bitsequence,
9090
// Checking the current size of the encoded
9191
// sequence: realloc if necessary
9292
if ((total + currentLength + 1) > reservedSize) {
93-
while (((size_t)total + currentLength + 1) > reservedSize) {
93+
while ((total + currentLength + 1) > reservedSize) {
9494
reservedSize <<= 1;
9595
if (reservedSize == 0) {
9696
reservedSize = ((size_t)total + currentLength) * 2;
@@ -99,7 +99,7 @@ CSD_FMIndex::CSD_FMIndex(hdt::IteratorUCharString *it, bool sparse_bitsequence,
9999
text =
100100
(unsigned char *)realloc(text, reservedSize * sizeof(unsigned char));
101101
}
102-
strncpy((char *)(text + total), (char *)currentStr, currentLength);
102+
strncpy((char *)(text + total), (char *)currentStr, reservedSize - total);
103103

104104
total += currentLength;
105105

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

libhdt/src/libdcs/CSD_HTFC.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ CSD_HTFC::CSD_HTFC(hdt::IteratorUCharString *it, uint32_t blocksize,
9393

9494
// The string is explicitly copied to the
9595
// encoded sequence.
96-
strncpy((char *)(textfc + bytesfc), (char *)currentStr, currentLength);
96+
strncpy((char *)(textfc + bytesfc), (char *)currentStr, reservedSize - bytesfc);
9797
bytesfc += currentLength;
9898

9999
// cout << nblocks-1 << "," << length << " => " << currentStr << endl;
@@ -113,7 +113,7 @@ CSD_HTFC::CSD_HTFC(hdt::IteratorUCharString *it, uint32_t blocksize,
113113

114114
// The suffix is copied to the sequence
115115
strncpy((char *)(textfc + bytesfc), (char *)currentStr + delta,
116-
currentLength - delta);
116+
reservedSize - bytesfc);
117117
bytesfc += currentLength - delta;
118118
// cout << nblocks-1 << "," << length << " => " << currentStr << endl;
119119
}
@@ -333,7 +333,7 @@ void CSD_HTFC::dumpBlock(uint block) {
333333
uint idInBlock = 0;
334334

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

@@ -352,7 +352,7 @@ void CSD_HTFC::dumpBlock(uint block) {
352352

353353
// Copying the suffix
354354
slen = strlen((char *)text + pos) + 1;
355-
strncpy((char *)(string + delta), (char *)(text + pos), slen);
355+
strncpy((char *)(string + delta), (char *)(text + pos), maxlength - delta + 1);
356356

357357
cout << block * blocksize + idInBlock << " (" << idInBlock << ") => "
358358
<< string << " Delta=" << delta << " Len=" << slen << endl;

libhdt/src/libdcs/CSD_PFC.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ CSD_PFC::CSD_PFC(hdt::IteratorUCharString *it, uint32_t blocksize,
8181
nblocks++;
8282

8383
// The string is explicitly copied to the encoded sequence.
84-
strncpy((char *)(text + bytes), (char *)currentStr, currentLength);
84+
strncpy((char *)(text + bytes), (char *)currentStr, reservedSize - bytes);
8585
bytes += currentLength;
8686
} else {
8787
// Regular string
@@ -96,7 +96,7 @@ CSD_PFC::CSD_PFC(hdt::IteratorUCharString *it, uint32_t blocksize,
9696

9797
// The suffix is copied to the sequence
9898
strncpy((char *)(text + bytes), (char *)currentStr + delta,
99-
currentLength - delta);
99+
reservedSize - bytes);
100100
bytes += currentLength - delta;
101101
}
102102

libhdt/src/triples/TripleListDisk.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ void TripleListDisk::insert(TripleID &triple)
303303

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

306-
memcpy(&arrayTriples[numTotalTriples], &triple, sizeof(TripleID));
306+
arrayTriples[numTotalTriples] = triple;
307307
numTotalTriples++;
308308
numValidTriples++;
309309
//cout << "Inserted: "<< numTotalTriples << endl;

0 commit comments

Comments
 (0)