Skip to content

Commit 88110cc

Browse files
authored
Merge pull request #283 from JanWielemaker/develop-buffer2
Fix potential buffer overflow/truncation, null-ptr error
2 parents d9ae092 + ba7742a commit 88110cc

19 files changed

Lines changed: 46 additions & 41 deletions

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ stamp-h1
5757
.deps/
5858

5959
*.hdt
60-
*.hdt.index
60+
*.hdt.index*
6161
*.nq
6262
*.nt
6363
*.rdf

libcds/include/RMQ_succinct.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ namespace cds_static
5555
void save(ostream & fp);
5656
RMQ_succinct * load(istream & fp);
5757

58-
~RMQ_succinct();
58+
virtual ~RMQ_succinct();
5959

6060
protected:
6161
/* size of array a*/

libcds/include/TextIndexCSA.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ namespace cds_static
4949
**/
5050
TextIndexCSA(uchar *text, ulong length, char *build_options);
5151

52-
~TextIndexCSA();
52+
virtual ~TextIndexCSA();
5353

5454
/* Writes in numocc the number of occurrences of the substring
5555
* pattern[0..length-1] found in the text indexed by index. */

libcds/include/WaveletTreeNoptrsS.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ namespace cds_static
8989
uint set(uint val, uint ind) const;
9090

9191
/** Recursive function for building the Wavelet Tree. */
92-
void build_level(uint **bm, uint *symbols, uint length, uint *occs);
92+
void build_level(uint **bm, uint *symbols, uint length);
9393
};
9494
};
9595
#endif

libcds/src/static/bitsequence/BitSequenceRRR.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ namespace cds_static
422422
ret->create_sampling(ret->sample_rate);
423423
return ret;
424424
}
425-
catch(exception e) {
425+
catch(const exception&) {
426426
delete ret;
427427
}
428428
return NULL;

libcds/src/static/sequence/WaveletMatrix.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
namespace cds_static
2525
{
2626

27-
WaveletMatrix::WaveletMatrix(const Array &symbols2, BitSequenceBuilder * bmb, Mapper * am) : Sequence(n) {
27+
WaveletMatrix::WaveletMatrix(const Array &symbols2, BitSequenceBuilder * bmb, Mapper * am) : Sequence(0) {
2828
bmb->use();
2929
n = symbols2.getLength();
30+
this->length = n; // sets Sequence::length (Sequence(0) in initializer)
31+
3032
uint *symbols = new uint[n];
3133
this->am = am;
3234
am->use();
@@ -81,8 +83,6 @@ namespace cds_static
8183
delete [] _bm;
8284
// delete [] oc;
8385
bmb->unuse();
84-
85-
this->length = n;
8686
}
8787

8888
WaveletMatrix::WaveletMatrix(uint * symbols, size_t n, BitSequenceBuilder * bmb, Mapper * am, bool deleteSymbols) : Sequence(n) {

libcds/src/static/sequence/WaveletTreeNoptrsS.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
namespace cds_static
2525
{
2626

27-
WaveletTreeNoptrsS::WaveletTreeNoptrsS(const Array &symb, BitSequenceBuilder * bmb, Mapper * am) : Sequence(n) {
27+
WaveletTreeNoptrsS::WaveletTreeNoptrsS(const Array &symb, BitSequenceBuilder * bmb, Mapper * am) : Sequence(0) {
2828
bmb->use();
2929
this->n=symb.getLength();
30+
this->length = this->n; // sets Sequence::length (Sequence(0) in initializer)
3031
this->am=am;
3132
bool deleteSymbols = true;
3233
am->use();
@@ -80,7 +81,7 @@ namespace cds_static
8081
_bm[i][j]=0;
8182
}
8283

83-
build_level(_bm,new_symb,new_n,occurrences);
84+
build_level(_bm,new_symb,new_n);
8485
bitstring = new BitSequence*[height];
8586
for(uint i=0;i<height;i++) {
8687
bitstring[i] = bmb->build(_bm[i],new_n);
@@ -148,7 +149,7 @@ namespace cds_static
148149
_bm[i][j]=0;
149150
}
150151

151-
build_level(_bm,new_symb,new_n,occurrences);
152+
build_level(_bm,new_symb,new_n);
152153
bitstring = new BitSequence*[height];
153154
for(uint i=0;i<height;i++) {
154155
bitstring[i] = bmb->build(_bm[i],new_n);
@@ -328,7 +329,7 @@ namespace cds_static
328329
return bytesBitstrings+occ->getSize()+ptrs;
329330
}
330331

331-
void WaveletTreeNoptrsS::build_level(uint **bm, uint *symbols, uint length, uint *occs) {
332+
void WaveletTreeNoptrsS::build_level(uint **bm, uint *symbols, uint length) {
332333
// for (uint i = 0; i < length; i++)
333334
// cout << " " << symbols[i];
334335
// cout << endl;

libcds/tests/testLCP.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ bool testLCP(LCP *s1, LCP *s2, TextIndex *csa){
5353

5454

5555
int main(int argc, char *argv[]){
56-
char *text;
57-
size_t length;
56+
char *text = nullptr;
57+
size_t length = 0;
5858
LCP *lcp_naive=NULL;
5959
LCP *lcp=NULL;
6060

libcds/tests/testNPR.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ bool testNPR(NPR *npr, LCP *lcp, TextIndex *csa, size_t *naive_nsv, size_t *nai
7171

7272

7373
int main(int argc, char *argv[]){
74-
char *text;
75-
size_t length;
74+
char *text = nullptr;
75+
size_t length = 0;
7676
LCP *lcp = NULL;
7777
NPR *npr = NULL;
7878

libhdt/src/libdcs/CSD_FMIndex.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ CSD_FMIndex::CSD_FMIndex(hdt::IteratorUCharString *it, bool sparse_bitsequence,
5454

5555
this->type = FMINDEX;
5656
string element;
57-
unsigned char *text;
57+
unsigned char *text; // TODO: std::vector<unsigned char>
5858
uint *bitmap = 0;
5959
// uint32_t *bitmap = 0;
6060

@@ -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

@@ -149,7 +149,7 @@ CSD_FMIndex::CSD_FMIndex(hdt::IteratorUCharString *it, bool sparse_bitsequence,
149149
separators = new BitSequenceRG(bitmap, len, 4);
150150
delete[] bitmap;
151151
}
152-
delete[] text;
152+
free(text);
153153
}
154154

155155
void CSD_FMIndex::build_ssa(unsigned char *text, size_t len,

0 commit comments

Comments
 (0)