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 possible buffer overflow #275

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
*.la
.dirstamp

# Wasm files
*.wasm

# Executables
hdt2rdf
hdtInfo
Expand Down Expand Up @@ -54,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
1 change: 0 additions & 1 deletion libcds/src/static/suffixtree/LCP_PT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include <LCP_PT.h>

const int max_precomputed_cover = 8;
const int coversizes[max_precomputed_cover+1]= {1,2,3,4,5,7,9,13,20};
const unsigned cover0[] = {0};
const unsigned cover1[] = {0,1};
const unsigned cover2[] = {0,1,2};
Expand Down
19 changes: 0 additions & 19 deletions libcds/src/static/suffixtree/factorization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,6 @@

const unsigned int TABLEBASE[7] = {0,16,272,4368,69904,1118480,17895696};

const unsigned short TABLESUM[256] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30
};

namespace cds_static
{

Expand Down
20 changes: 1 addition & 19 deletions libcds/src/static/textindex/suftest3.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,8 @@

#include <stdlib.h>
#include <stdio.h>
#if 1
#include <sys/timeb.h>
#else
#include <sys/time.h>
#include <sys/resource.h>
#endif
#include <comparray4.h>
#include <limits.h>
#include <stdbool.h>
Expand All @@ -58,20 +54,7 @@

namespace cds_static
{

#if 1
typedef struct timeb mytimestruct;
void mygettime(mytimestruct *t) {
ftime(t);
}
double mylaptime(mytimestruct *before,mytimestruct *after) {
double t;
t = after->time - before->time;
t += (double)(after->millitm - before->millitm)/1000;
return t;
}
#else
typedef mytimestruct struct rusage;
typedef struct rusage mytimestruct;
void mygettime(mytimestruct *t) {
getrusage(RUSAGE_SELF,t);
}
Expand All @@ -82,7 +65,6 @@ namespace cds_static
- before->ru_utime.tv_usec)/1000000;
return t;
}
#endif

/* Three function to variables to manage parameters */
static bool is_delimeter(char *delimiters, char c) {
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
7 changes: 7 additions & 0 deletions libhdt/include/HDTSpecification.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ class HDTSpecification
*/
const std::string& get(const std::string& key);


/**
* Get the value of a property, or "" if the key is not found.
*/
const std::string& getOrEmpty(const std::string& key);


/** Set the value of a property */
void set(const std::string& key, const std::string& value);
};
Expand Down
24 changes: 24 additions & 0 deletions libhdt/src/bitsequence/BitSequence375.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,25 @@ void BitSequence375::set(const size_t i, bool val) {
array = &data[0];
}
if(val) {
#ifdef __EMSCRIPTEN__
/* Webassembly size_t differs from the one of
other architectures.
EDITED BY @lucafabbian, TODO: check */
// fprintf(stderr, "[WARN] Unsafe cast called\n");
bitset((uint64_t*) &array[0], i);
#else
bitset(&array[0], i);
#endif
} else {
#ifdef __EMSCRIPTEN__
/* Webassembly size_t differs from the one of
other architectures.
EDITED BY @lucafabbian, TODO: check */
// fprintf(stderr, "[WARN] Unsafe cast called\n");
bitclean((uint64_t*)&array[0], i);
#else
bitclean(&array[0], i);
#endif
}

numbits = i>=numbits ? i+1 : numbits;
Expand All @@ -173,7 +189,15 @@ void BitSequence375::append(bool bit) {

bool BitSequence375::access(const size_t i) const
{
#ifdef __EMSCRIPTEN__
/* Webassembly size_t differs from the one of
other architectures.
EDITED BY @lucafabbian, TODO: check */
//fprintf(stderr, "[WARN] Unsafe cast called\n");
return bitget((uint64_t*)array, i);
#else
return bitget(array, i);
#endif
}

void BitSequence375::save(ostream & out) const
Expand Down
5 changes: 1 addition & 4 deletions libhdt/src/dictionary/FourSectionDictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ FourSectionDictionary::FourSectionDictionary(HDTSpecification & spec) : blocksiz
objects = new csd::CSD_PFC();
shared = new csd::CSD_PFC();

string blockSizeStr = "";
try{
blockSizeStr = spec.get("dict.block.size");
}catch(exception& e){}
string blockSizeStr = spec.getOrEmpty("dict.block.size");

if(!blockSizeStr.empty() && (blockSizeStr.find_first_not_of("0123456789") == string::npos))
{
Expand Down
5 changes: 1 addition & 4 deletions libhdt/src/dictionary/KyotoDictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ KyotoDictionary::KyotoDictionary() {
}

KyotoDictionary::KyotoDictionary(HDTSpecification &specification) : spec(specification) {
string map = "";
try{
map = spec.get("dictionary.mapping");
}catch(exception& e){}
string map = spec.getOrEmpty("dictionary.mapping");
if(map=="mapping1") {
this->mapping = MAPPING1;
} else {
Expand Down
6 changes: 2 additions & 4 deletions libhdt/src/dictionary/LiteralDictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,8 @@ LiteralDictionary::LiteralDictionary(HDTSpecification & spec) : blocksize(8) {
objectsLiterals = new csd::CSD_FMIndex();
shared = new csd::CSD_PFC();

string blockSizeStr = "";
try{
blockSizeStr = spec.get("dict.block.size");
}catch(exception& e){}
string blockSizeStr = spec.getOrEmpty("dict.block.size");

if (blockSizeStr != "") {
blocksize = atoi(blockSizeStr.c_str());
}
Expand Down
5 changes: 1 addition & 4 deletions libhdt/src/dictionary/PlainDictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ PlainDictionary::PlainDictionary() {
}

PlainDictionary::PlainDictionary(HDTSpecification &specification) : spec(specification) {
string map ="";
try{
map = spec.get("dictionary.mapping");
}catch(exception& e){}
string map =spec.getOrEmpty("dictionary.mapping");
if(map=="mapping1") {
this->mapping = MAPPING1;
} else {
Expand Down
37 changes: 8 additions & 29 deletions libhdt/src/hdt/BasicHDT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,8 @@ void BasicHDT::createComponents() {

// DICTIONARY

std::string dictType = "";
try{
dictType = spec.get("dictionary.type");
}
catch (std::exception& e){
}
std::string dictType = spec.getOrEmpty("dictionary.type");


if(dictType==HDTVocabulary::DICTIONARY_TYPE_FOUR) {
dictionary = new FourSectionDictionary(spec);
Expand All @@ -118,11 +114,8 @@ void BasicHDT::createComponents() {
}

// TRIPLES
std::string triplesType = "";
try{
triplesType = spec.get("triples.type");
}catch (std::exception& e) {
}
std::string triplesType = spec.getOrEmpty("triples.type");

if(triplesType==HDTVocabulary::TRIPLES_TYPE_BITMAP) {
triples = new BitmapTriples(spec);
} else if(triplesType==HDTVocabulary::TRIPLES_TYPE_PLAIN) {
Expand Down Expand Up @@ -299,11 +292,8 @@ void BasicHDT::loadTriples(const char* fileName, const char* baseUri, RDFNotatio
triplesList->stopProcessing(&iListener);

// SORT & Duplicates
string ord = "";
try{
ord = spec.get("triplesOrder");
}catch (std::exception& e){
}
string ord = spec.getOrEmpty("triplesOrder");

TripleComponentOrder order = parseOrder(
ord.c_str());
if (order == Unknown) {
Expand All @@ -319,17 +309,10 @@ void BasicHDT::loadTriples(const char* fileName, const char* baseUri, RDFNotatio
cout << "Catch exception triples" << e << endl;
delete triplesList;
throw e;
#ifndef WIN32
} catch (char *e) {
cout << "Catch exception triples" << e << endl;
delete triplesList;
throw e;
#else
} catch (std::exception& e) {
// cerr << "Catch exception triples" << e << endl;
delete triplesList;
throw;
#endif
}
if (triples->getType() == triplesList->getType()) {
delete triples;
Expand Down Expand Up @@ -588,12 +571,8 @@ void BasicHDT::loadTriplesFromHDTs(const char** fileNames, size_t numFiles, cons
triplesList->stopProcessing(&iListener);

// SORT & Duplicates
string ord = "";
try{
ord = spec.get("triplesOrder");
}
catch (std::exception& e){
}
string ord = spec.getOrEmpty("triplesOrder");

TripleComponentOrder order = parseOrder(ord.c_str());
if (order == Unknown) {
order = SPO;
Expand Down
Loading