Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix potential buffer overflow/truncation, null-ptr error #283

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ stamp-h1
.deps/

*.hdt
*.hdt.index
*.hdt.index*
*.nq
*.nt
*.rdf
Expand Down
2 changes: 1 addition & 1 deletion libcds/include/RMQ_succinct.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace cds_static
void save(ostream & fp);
RMQ_succinct * load(istream & fp);

~RMQ_succinct();
virtual ~RMQ_succinct();

protected:
/* size of array a*/
Expand Down
2 changes: 1 addition & 1 deletion libcds/include/TextIndexCSA.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace cds_static
**/
TextIndexCSA(uchar *text, ulong length, char *build_options);

~TextIndexCSA();
virtual ~TextIndexCSA();

/* Writes in numocc the number of occurrences of the substring
* pattern[0..length-1] found in the text indexed by index. */
Expand Down
2 changes: 1 addition & 1 deletion libcds/include/WaveletTreeNoptrsS.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ namespace cds_static
uint set(uint val, uint ind) const;

/** Recursive function for building the Wavelet Tree. */
void build_level(uint **bm, uint *symbols, uint length, uint *occs);
void build_level(uint **bm, uint *symbols, uint length);
};
};
#endif
2 changes: 1 addition & 1 deletion libcds/src/static/bitsequence/BitSequenceRRR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ namespace cds_static
ret->create_sampling(ret->sample_rate);
return ret;
}
catch(exception e) {
catch(const exception&) {
delete ret;
}
return NULL;
Expand Down
6 changes: 3 additions & 3 deletions libcds/src/static/sequence/WaveletMatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
namespace cds_static
{

WaveletMatrix::WaveletMatrix(const Array &symbols2, BitSequenceBuilder * bmb, Mapper * am) : Sequence(n) {
WaveletMatrix::WaveletMatrix(const Array &symbols2, BitSequenceBuilder * bmb, Mapper * am) : Sequence(0) {
bmb->use();
n = symbols2.getLength();
this->length = n; // sets Sequence::length (Sequence(0) in initializer)

uint *symbols = new uint[n];
this->am = am;
am->use();
Expand Down Expand Up @@ -81,8 +83,6 @@ namespace cds_static
delete [] _bm;
// delete [] oc;
bmb->unuse();

this->length = n;
}

WaveletMatrix::WaveletMatrix(uint * symbols, size_t n, BitSequenceBuilder * bmb, Mapper * am, bool deleteSymbols) : Sequence(n) {
Expand Down
9 changes: 5 additions & 4 deletions libcds/src/static/sequence/WaveletTreeNoptrsS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
namespace cds_static
{

WaveletTreeNoptrsS::WaveletTreeNoptrsS(const Array &symb, BitSequenceBuilder * bmb, Mapper * am) : Sequence(n) {
WaveletTreeNoptrsS::WaveletTreeNoptrsS(const Array &symb, BitSequenceBuilder * bmb, Mapper * am) : Sequence(0) {
bmb->use();
this->n=symb.getLength();
this->length = this->n; // sets Sequence::length (Sequence(0) in initializer)
this->am=am;
bool deleteSymbols = true;
am->use();
Expand Down Expand Up @@ -80,7 +81,7 @@ namespace cds_static
_bm[i][j]=0;
}

build_level(_bm,new_symb,new_n,occurrences);
build_level(_bm,new_symb,new_n);
bitstring = new BitSequence*[height];
for(uint i=0;i<height;i++) {
bitstring[i] = bmb->build(_bm[i],new_n);
Expand Down Expand Up @@ -148,7 +149,7 @@ namespace cds_static
_bm[i][j]=0;
}

build_level(_bm,new_symb,new_n,occurrences);
build_level(_bm,new_symb,new_n);
bitstring = new BitSequence*[height];
for(uint i=0;i<height;i++) {
bitstring[i] = bmb->build(_bm[i],new_n);
Expand Down Expand Up @@ -328,7 +329,7 @@ namespace cds_static
return bytesBitstrings+occ->getSize()+ptrs;
}

void WaveletTreeNoptrsS::build_level(uint **bm, uint *symbols, uint length, uint *occs) {
void WaveletTreeNoptrsS::build_level(uint **bm, uint *symbols, uint length) {
// for (uint i = 0; i < length; i++)
// cout << " " << symbols[i];
// cout << endl;
Expand Down
4 changes: 2 additions & 2 deletions libcds/tests/testLCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ bool testLCP(LCP *s1, LCP *s2, TextIndex *csa){


int main(int argc, char *argv[]){
char *text;
size_t length;
char *text = nullptr;
size_t length = 0;
LCP *lcp_naive=NULL;
LCP *lcp=NULL;

Expand Down
4 changes: 2 additions & 2 deletions libcds/tests/testNPR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ bool testNPR(NPR *npr, LCP *lcp, TextIndex *csa, size_t *naive_nsv, size_t *nai


int main(int argc, char *argv[]){
char *text;
size_t length;
char *text = nullptr;
size_t length = 0;
LCP *lcp = NULL;
NPR *npr = NULL;

Expand Down
10 changes: 5 additions & 5 deletions libhdt/src/libdcs/CSD_FMIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ CSD_FMIndex::CSD_FMIndex(hdt::IteratorUCharString *it, bool sparse_bitsequence,

this->type = FMINDEX;
string element;
unsigned char *text;
unsigned char *text; // TODO: std::vector<unsigned char>
uint *bitmap = 0;
// uint32_t *bitmap = 0;

Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -118,7 +118,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 Expand Up @@ -149,7 +149,7 @@ CSD_FMIndex::CSD_FMIndex(hdt::IteratorUCharString *it, bool sparse_bitsequence,
separators = new BitSequenceRG(bitmap, len, 4);
delete[] bitmap;
}
delete[] text;
free(text);
}

void CSD_FMIndex::build_ssa(unsigned char *text, size_t len,
Expand Down
21 changes: 11 additions & 10 deletions libhdt/src/libdcs/CSD_HTFC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* Miguel A. Martinez-Prieto: [email protected]
*/

#include <string>
#include "CSD_HTFC.h"

#if HAVE_CDS
Expand Down Expand Up @@ -57,8 +58,9 @@ CSD_HTFC::CSD_HTFC(hdt::IteratorUCharString *it, uint32_t blocksize,

vector<uint> xblocks; // Temporal storage for start positions

unsigned char *previousStr = NULL, *currentStr = NULL;
uint previousLength = 0, currentLength = 0;
std::basic_string<unsigned char> previousStr((const unsigned char*)"");
unsigned char *currentStr = NULL;
uint currentLength = 0;

while (it->hasNext()) {
currentStr = it->next();
Expand Down Expand Up @@ -91,16 +93,16 @@ 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);
bytesfc += currentLength;

// cout << nblocks-1 << "," << length << " => " << currentStr << endl;
} else {
// Regular string

// Calculating the length of the long common prefix
uint delta = longest_common_prefix(previousStr, currentStr,
previousLength, currentLength);
uint delta = longest_common_prefix(previousStr.data(), currentStr,
previousStr.length(), currentLength);

// cout << "Block: " << nblocks << " Pos: "<< length << endl;
// cout << previousStr << endl << currentStr << endl << " Delta: " <<
Expand All @@ -111,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;
}
Expand All @@ -121,8 +123,7 @@ CSD_HTFC::CSD_HTFC(hdt::IteratorUCharString *it, uint32_t blocksize,

// New string processed
numstrings++;
memcpy(previousStr, currentStr, currentLength);
previousLength = currentLength;
previousStr.assign(currentStr, currentLength);

it->freeStr(currentStr);
// NOTIFYCOND(listener, "Converting dictionary to HTFC", length,
Expand Down Expand Up @@ -332,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;

Expand All @@ -351,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;
Expand Down
4 changes: 2 additions & 2 deletions libhdt/src/libdcs/CSD_PFC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions libhdt/src/rdf/RDFParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ RDFParserCallback *RDFParserCallback::getParserCallback(RDFNotation notation) {
#else
throw ParseException("No Parser available for input RDF Format");
#endif
return nullptr;
}

}
3 changes: 2 additions & 1 deletion libhdt/src/rdf/RDFParserSerd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ void RDFParserSerd::doParse(const char *fileName, const char *baseUri, RDFNotati

serd_reader_set_error_sink(reader, hdtserd_on_error, NULL);

const uint8_t* input=serd_uri_to_path((const uint8_t *)fileName);
uint8_t* input=serd_file_uri_parse((const uint8_t *)fileName, NULL);

if(fileUtil::str_ends_with(fileName,".gz")){

Expand Down Expand Up @@ -216,6 +216,7 @@ void RDFParserSerd::doParse(const char *fileName, const char *baseUri, RDFNotati

serd_env_free(env);
serd_node_free(&base);
serd_free(input);
}

}
Expand Down
2 changes: 1 addition & 1 deletion libhdt/src/triples/BitmapTriples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ struct sort_pred {
};

// function object
class PredicateComparator : public std::binary_function<unsigned int,unsigned int,bool>
class PredicateComparator
{
private:
IntSequence *array;
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
8 changes: 4 additions & 4 deletions libhdt/src/util/Histogram.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Histogram {

Histogram() :
Start(0), nBins_by_interval(0), nBins(0),
freq(new size_t[0]) {
freq(new size_t[1]) { // size_t[0] is undefined behavior
reset();
}

Expand Down Expand Up @@ -69,7 +69,7 @@ class Histogram {
delete[] freq;
}

/** operator=
/** operator=
* Set this histogram equal to another.
* @param other Description of the param.
* @return The expected result.
Expand All @@ -81,9 +81,9 @@ class Histogram {
if (nBins != other.nBins) {
nBins = other.nBins;
delete[] freq;
freq = new size_t[nBins];
freq = new size_t[nBins];
}
for (size_t i(0); i < nBins; ++i)
for (size_t i(0); i < nBins; ++i)
freq[i] = other.freq[i];
}
return *this;
Expand Down
1 change: 1 addition & 0 deletions libhdt/src/util/StopWatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <sstream>

#include <string.h> // For memcpy()
#include <cstdint> // for uint64_t etc

#include "StopWatch.hpp"

Expand Down
2 changes: 1 addition & 1 deletion libhdt/tools/hdtSearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void iterate(HDT *hdt, char *query, ostream &out, bool measure, uint32_t offset)
it->skip(offset);
offset = 0;
}
catch (const runtime_error error) {
catch (const runtime_error&) {
/*invalid offset*/
interruptSignal = 1;
}
Expand Down