Skip to content

Commit 9b8a267

Browse files
committed
Remove all char[LINELEN] stack allocations to avoid stack overflows
1 parent e1bd3a1 commit 9b8a267

File tree

8 files changed

+42
-24
lines changed

8 files changed

+42
-24
lines changed

src/hhalignment.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ void Alignment::Read(FILE* inf, char infile[], const char mark, const int maxcol
181181
int l; // Postion in alignment incl. gaps (first=1)
182182
int h; // Position in input line (first=0)
183183
int k; // Index of sequence being read currently (first=0)
184-
char line[LINELEN] = ""; // input line
184+
std::unique_ptr<char[]> line_ptr(new char[LINELEN]);
185+
char* line = line_ptr.get();
185186
char cur_seq[maxcol]; // Sequence currently read in
186187
char* cur_name; // Sequence currently read in
187188
int linenr = 0; // current line number in input file
@@ -3459,7 +3460,8 @@ void Alignment::WriteToFile(std::stringstream& out, const char format[]) {
34593460
out << ">" << sname[k] << std::endl << seq[k] + 1 << std::endl;
34603461
} else // PSI-BLAST format
34613462
{
3462-
char line[LINELEN];
3463+
std::unique_ptr<char[]> line_ptr(new char[LINELEN]);
3464+
char* line = line_ptr.get();
34633465
char tmp_name[NAMELEN];
34643466
for (int k = 0; k < N_in; ++k) // skip sequences before kfirst!!
34653467
if (!(k == kss_pred || k == kss_conf || k == kss_dssp || k == ksa_dssp)

src/hhdatabase.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,8 @@ void HHDatabaseEntry::getTemplateA3M(Parameters& par, float* pb,
371371
exit(4);
372372
}
373373

374-
char line[LINELEN];
374+
std::unique_ptr<char[]> line_ptr(new char[LINELEN]);
375+
char* line = line_ptr.get();
375376
if (!fgetline(line, LINELEN, dbf)) {
376377
//TODO: throw error
377378
HH_LOG(ERROR) << "In " << __FILE__ << ":" << __LINE__ << ": " << __func__ << ":" << std::endl;
@@ -398,7 +399,8 @@ void HHEntry::getTemplateHMM(FILE* dbf, char* name, Parameters& par,
398399
int& format, float* pb, const float S[20][20],
399400
const float Sim[20][20], HMM* t) {
400401
if (dbf != NULL) {
401-
char line[LINELEN];
402+
std::unique_ptr<char[]> line_ptr(new char[LINELEN]);
403+
char* line = line_ptr.get();
402404
if (!fgetline(line, LINELEN, dbf)) {
403405
//TODO: throw error
404406
HH_LOG(ERROR) << "In " << __FILE__ << ":" << __LINE__ << ": " << __func__ << ":" << std::endl;
@@ -490,7 +492,8 @@ void HHFileEntry::getTemplateA3M(Parameters& par, float* pb,
490492
const float S[20][20], const float Sim[20][20],
491493
Alignment& tali) {
492494

493-
char line[LINELEN];
495+
std::unique_ptr<char[]> line_ptr(new char[LINELEN]);
496+
char* line = line_ptr.get();
494497
HMM* t = new HMM(MAXSEQDIS, par.maxres);
495498

496499
FILE* inf = fopen(file, "r");

src/hhfullalignment.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,17 @@ FullAlignment::FullAlignment(int maxseqdis) {
3434
ta = new HalfAlignment(maxseqdis);
3535
if (!qa || !ta)
3636
MemoryError("space for formatting HMM-HMM alignment", __FILE__, __LINE__, __func__);
37+
symbol = new char[LINELEN];
38+
posterior = new char[LINELEN];
3739
}
3840

3941
/////////////////////////////////////////////////////////////////////////////////////
4042
// Destructor
4143
FullAlignment::~FullAlignment() {
4244
delete qa;
4345
delete ta;
46+
delete[] symbol;
47+
delete[] posterior;
4448
}
4549

4650
/////////////////////////////////////////////////////////////////////////////////////
@@ -194,7 +198,8 @@ void FullAlignment::Build(HMM* q, Hit& hit, const int nseqdis, const float S[20]
194198
/////////////////////////////////////////////////////////////////////////////////////
195199
void FullAlignment::PrintHeader(std::stringstream& out, HMM* q, Hit& hit) {
196200
out << ">" << hit.longname << std::endl;
197-
char line[LINELEN];
201+
std::unique_ptr<char[]> line_ptr(new char[LINELEN]);
202+
char* line = line_ptr.get();
198203

199204
sprintf(line,
200205
"Probab=%-.2f E-value=%-.2g Score=%-.2f Aligned_cols=%i Identities=%i%% Similarity=%-.3f Sum_probs=%.1f Template_Neff=%-.3f\n\n",
@@ -222,7 +227,8 @@ void FullAlignment::PrintHHR(std::stringstream& out, Hit& hit, const char showco
222227
for (k = 0; k < ta->n; k++)
223228
lt[k] = ta->l[k][hit.j1];
224229

225-
char line[LINELEN];
230+
std::unique_ptr<char[]> line_ptr(new char[LINELEN]);
231+
char* line = line_ptr.get();
226232
while (hh < ta->pos - 1) // print alignment block
227233
{
228234
// Print query secondary structure sequences

src/hhfullalignment.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ class FullAlignment
2727
private:
2828
HalfAlignment* qa; // query and template parts of the alignment
2929
HalfAlignment* ta; // query and template parts of the alignment
30-
char symbol[LINELEN]; // symbol[h] = symbol (= - . + |) indicating match score for col h of alignment
31-
char posterior[LINELEN]; // posterior probability for pair of aligned columns
30+
char* symbol; // symbol[h] = symbol (= - . + |) indicating match score for col h of alignment
31+
char* posterior; // posterior probability for pair of aligned columns
3232
void ClearSymbols() {for (int h=0; h<LINELEN-1; h++) symbol[h]=posterior[h]=' ';}
3333
void AddColumns(int i, int j, char prev_state, char state, float S, float PP);
3434
void AddGaps();

src/hhfunc.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
void ReadQueryFile(Parameters& par, FILE* inf, char& input_format,
1111
char use_global_weights, HMM* q, Alignment* qali, char infile[], float* pb,
1212
const float S[20][20], const float Sim[20][20]) {
13-
char line[LINELEN];
14-
13+
std::unique_ptr<char[]> line_ptr(new char[LINELEN]);
14+
char* line = line_ptr.get();
1515
if (!fgetline(line, LINELEN, inf)) {
1616
HH_LOG(ERROR) << "Error in " << __FILE__ << ":" << __LINE__ << ": " << __func__ << ":" << std::endl;
1717
HH_LOG(ERROR) << "\t" << infile << " is empty!\n";

src/hhhitlist.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ void HitList::PrintHitList(HMM* q, std::stringstream& out,
4848
<< std::endl;
4949
#endif
5050

51-
char line[LINELEN];
51+
std::unique_ptr<char[]> line_ptr(new char[LINELEN]);
52+
char* line = line_ptr.get();
5253
Reset();
5354
while (!End()) {
5455
Hit hit = ReadNext();
@@ -284,14 +285,15 @@ void HitList::PrintM8File(HMM* q, std::stringstream& outbuffer, const int nhits,
284285
// d1c8da_ Q32Z53 0.618 547 334 0 1 548 1 542 3.11E-185 646
285286

286287
int i = 0;
288+
std::unique_ptr<char[]> line_ptr(new char[LINELEN]);
289+
char* line = line_ptr.get();
287290
while (!End()) {
288291
i++;
289292
Hit hit = ReadNext();
290293
if (nhits >= b && hit.Probab < p)
291294
break;
292295
if (nhits >= b && hit.Eval > E)
293296
continue;
294-
char line[LINELEN];
295297
int gapOpenCount = 0;
296298
int missMatchCount = 0;
297299
int matchCount = 0;
@@ -340,6 +342,8 @@ void HitList::PrintScoreFile(HMM* q, std::stringstream& outbuffer) {
340342
// d1qsaa2 3 168 124 145.55 239.22 57.36
341343

342344
int i = 0;
345+
std::unique_ptr<char[]> line_ptr(new char[LINELEN]);
346+
char* line = line_ptr.get();
343347
while (!End()) {
344348
i++;
345349
Hit hit = ReadNext();
@@ -362,8 +366,6 @@ void HitList::PrintScoreFile(HMM* q, std::stringstream& outbuffer) {
362366
else
363367
n = 0;
364368

365-
char line[LINELEN];
366-
367369
sprintf(line, "%-20s %-10s %1i %5i %3i %8.3f %7.2f %6.2f %7.2f %8.3f\n",
368370
hit.name, hit.fam, n, hit.L, hit.matched_cols, -1.443 * hit.logPval,
369371
-hit.score_aass, hit.Probab, hit.score, -1.443 * hit.logEval);
@@ -397,7 +399,8 @@ void HitList::WriteToAlifile(HMM* q, char* alitabfile,
397399
void HitList::WriteToAlifile(HMM* q, std::stringstream& out,
398400
const int b, const int B, const int z, const int Z,
399401
const float p, const double E) {
400-
char line[LINELEN];
402+
std::unique_ptr<char[]> line_ptr(new char[LINELEN]);
403+
char* line = line_ptr.get();
401404

402405
// Store all dbfiles and ftell positions of templates to be displayed and realigned
403406
int nhits = 0;

src/hhhmm.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ HMM& HMM::operator=(HMM& q) {
200200
/////////////////////////////////////////////////////////////////////////////////////
201201
int HMM::Read(FILE* dbf, const int maxcol, const int nseqdis, float* pb,
202202
char* path) {
203-
char line[LINELEN] = ""; // input line
203+
std::unique_ptr<char[]> line_ptr(new char[LINELEN]);
204+
char* line = line_ptr.get();
204205
char str3[8] = "", str4[8] = ""; // first 3 and 4 letters of input line
205206
char* ptr; // pointer for string manipulation
206207
int i = 0; // index for match state (first=1)
@@ -692,8 +693,9 @@ int HMM::Read(FILE* dbf, const int maxcol, const int nseqdis, float* pb,
692693
//// Read an HMM from a HMMer .hmm file; return 0 at end of file
693694
/////////////////////////////////////////////////////////////////////////////////////
694695
int HMM::ReadHMMer(FILE* dbf, const char showcons, float* pb, char* filestr) {
695-
char line[LINELEN] = ""; // input line
696-
char desc[DESCLEN] = ""; // description of family
696+
std::unique_ptr<char[]> line_ptr(new char[LINELEN]);
697+
char* line = line_ptr.get();
698+
char desc[DESCLEN] = ""; // description of family
697699
char str4[5] = ""; // first 4 letters of input line
698700
char* ptr; // pointer for string manipulation
699701
int i = 0; // index for match state (first=1)
@@ -1203,7 +1205,8 @@ int HMM::ReadHMMer(FILE* dbf, const char showcons, float* pb, char* filestr) {
12031205
//// Read an HMM from a HMMER3 .hmm file; return 0 at end of file
12041206
/////////////////////////////////////////////////////////////////////////////////////
12051207
int HMM::ReadHMMer3(FILE* dbf, const char showcons, float* pb, char* filestr) {
1206-
char line[LINELEN] = ""; // input line
1208+
std::unique_ptr<char[]> line_ptr(new char[LINELEN]);
1209+
char* line = line_ptr.get();
12071210
char desc[DESCLEN] = ""; // description of family
12081211
char str4[5] = ""; // first 4 letters of input line
12091212
char* ptr; // pointer for string manipulation
@@ -2170,7 +2173,8 @@ void HMM::WriteToFile(std::stringstream& out, const int max_seqid,
21702173
const int coverage, const int qid, const int Ndiff, const float qsc,
21712174
const int argc, const char** argv, const float* pb) {
21722175
const int SEQLEN = 100; // number of residues per line for sequences to be displayed
2173-
char line[LINELEN];
2176+
std::unique_ptr<char[]> line_ptr(new char[LINELEN]);
2177+
char* line = line_ptr.get();
21742178

21752179
if (trans_lin == 1)
21762180
InternalError(

src/hhutil.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ int InternalError(const char errstr[], const char* file, const int line, const c
3939
/////////////////////////////////////////////////////////////////////////////////////
4040
// Count number of lines in <file>
4141
/////////////////////////////////////////////////////////////////////////////////////
42-
int CountLinesInFile(const char* file)
43-
{
44-
char line[LINELEN]=""; // input line
42+
int CountLinesInFile(const char* file) {
43+
std::unique_ptr<char[]> line_ptr(new char[LINELEN]);
44+
char* line = line_ptr.get();
4545
int numlines=0;
4646
char tmp_file[NAMELEN];
4747
strcpy(tmp_file, file);

0 commit comments

Comments
 (0)