Skip to content

Commit

Permalink
Fixed size buffer. Hang on socket input.
Browse files Browse the repository at this point in the history
* Don't use fix size strings (`char [MAX_LINE]`) for input file path.
Use `std::string`.

* In `show-aligns` and `show-tiling`, When the file path in a delta file
points to a non-regular file (e.g., a fifo, tty or socket), warn that
the process may hang on input.
  • Loading branch information
gmarcais committed Dec 2, 2024
1 parent 61d2b1f commit 41a3d16
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 80 deletions.
11 changes: 6 additions & 5 deletions src/tigr/prepro.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
//
//------------------------------------------------------------------------------

#include <string>
#include <mummer/tigrinc.hh>
#include <mummer/translate.hh>

Expand Down Expand Up @@ -72,7 +73,7 @@ int main
char * A, * tA;
char mask_char = 0;
char Id [MAX_LINE];
char InputFileName [MAX_LINE];
std::string InputFileName;

FILE * InputFile;

Expand All @@ -93,13 +94,13 @@ int main
break;

case 'q' :
strcpy (InputFileName, optarg);
InputFileName = optarg;
isQuery = true;
mask_char = QUERY_MASK;
break;

case 'r' :
strcpy (InputFileName, optarg);
InputFileName = optarg;
isReference = true;
mask_char = REFERENCE_MASK;
break;
Expand All @@ -123,7 +124,7 @@ int main
}
}

InputFile = File_Open (InputFileName, "r");
InputFile = File_Open (InputFileName.c_str(), "r");

InitSize = INIT_SIZE;
A = (char *) Safe_malloc ( sizeof(char) * InitSize );
Expand All @@ -132,7 +133,7 @@ int main

ct = 0;
if ( isReference )
printf (">allcontigs %s\n", InputFileName);
printf (">allcontigs %s\n", InputFileName.c_str());
while ( Read_String (InputFile, A, InitSize, Id, false) )
{
LenA = strlen(A + 1);
Expand Down
121 changes: 66 additions & 55 deletions src/tigr/show-aligns.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//------------------------------------------------------------------------------

#include <sys/ioctl.h>
#include <sys/stat.h>

#include <mummer/delta.hh>
#include <mummer/tigrinc.hh>
Expand Down Expand Up @@ -179,7 +180,7 @@ bool isSortByReference = false; // -r option
int DATA_TYPE = NUCMER_DATA;
int MATRIX_TYPE = mummer::sw_align::BLOSUM62;

char InputFileName [MAX_LINE];
std::string InputFileName;
// Today, only 1 ref and qry file supported in delta format. It may change one day!
std::vector<std::string> RefFileNames, QryFileNames;

Expand Down Expand Up @@ -234,59 +235,59 @@ int main
while ( !errflg && ((ch = getopt
(argc, argv, "hqrw:x:m:c")) != EOF) )
switch (ch)
{
case 'h' :
printHelp (argv[0]);
exit (EXIT_SUCCESS);
break;

case 'q' :
isSortByQuery = true;
break;

case 'r' :
isSortByReference = true;
break;

case 'w' :
Screen_Width = atoi (optarg);
if ( Screen_Width <= LINE_PREFIX_LEN )
{
fprintf(stderr,
"WARNING: invalid screen width %d, using default\n",
Screen_Width);
Screen_Width = DEFAULT_SCREEN_WIDTH;
}
break;

case 'm':
Marker_Width = atoi(optarg);
break;

case 'x' :
MATRIX_TYPE = atoi (optarg);
if ( MATRIX_TYPE < 1 || MATRIX_TYPE > 3 )
{
fprintf(stderr,
"WARNING: invalid matrix type %d, using default\n",
MATRIX_TYPE);
MATRIX_TYPE = mummer::sw_align::BLOSUM62;
}
break;

case 'c' :
Colorize = true;
break;

default :
errflg ++;
}
{
case 'h' :
printHelp (argv[0]);
exit (EXIT_SUCCESS);
break;

case 'q' :
isSortByQuery = true;
break;

case 'r' :
isSortByReference = true;
break;

case 'w' :
Screen_Width = atoi (optarg);
if ( Screen_Width <= LINE_PREFIX_LEN )
{
fprintf(stderr,
"WARNING: invalid screen width %d, using default\n",
Screen_Width);
Screen_Width = DEFAULT_SCREEN_WIDTH;
}
break;

case 'm':
Marker_Width = atoi(optarg);
break;

case 'x' :
MATRIX_TYPE = atoi (optarg);
if ( MATRIX_TYPE < 1 || MATRIX_TYPE > 3 )
{
fprintf(stderr,
"WARNING: invalid matrix type %d, using default\n",
MATRIX_TYPE);
MATRIX_TYPE = mummer::sw_align::BLOSUM62;
}
break;

case 'c' :
Colorize = true;
break;

default :
errflg ++;
}

if ( errflg > 0 || argc - optind != 3 )
{
printUsage (argv[0]);
exit (EXIT_FAILURE);
}
{
printUsage (argv[0]);
exit (EXIT_FAILURE);
}

if ( isSortByQuery && isSortByReference )
fprintf (stderr,
Expand All @@ -295,7 +296,7 @@ int main
Screen_Width = get_screen_width();
}

strcpy (InputFileName, argv[optind ++]);
InputFileName = argv[optind ++];
IdR = argv[optind++];
IdQ = argv[optind++];

Expand Down Expand Up @@ -369,11 +370,21 @@ void parseDelta
bool found = false;

DeltaReader_t dr;
dr.open (InputFileName);
dr.open (InputFileName.c_str());
DATA_TYPE = dr.getDataType( ) == NUCMER_STRING ?
NUCMER_DATA : PROMER_DATA;

struct stat sb;
RefFileNames.push_back(dr.getReferencePath());
if(stat(RefFileNames.back().c_str(), &sb) == -1 || !(sb.st_mode & S_IFREG)) {
fprintf(stderr, "WARNING: the reference file '%s' is not a regular file, process may hang for input\n",
RefFileNames.back().c_str());
}
QryFileNames.push_back(dr.getQueryPath());
if(stat(QryFileNames.back().c_str(), &sb) == -1 || !(sb.st_mode & S_IFREG)) {
fprintf(stderr, "WARNING: the query file '%s' is not a regular file, process may hang for input\n",
QryFileNames.back().c_str());
}

while ( dr.readNext( ) )
{
Expand Down Expand Up @@ -793,7 +804,7 @@ long int revC
bool find_sequence(const std::vector<string>& paths, const std::string& Id, std::string& seq)
{
//-- Find, and read in sequences. Return if find one with name Id, and store it in seq.
stream_manager streams(paths.cbegin(), paths.cend());
stream_manager streams(paths.cbegin(), paths.cend());
sequence_parser parser(16, 10, 1, streams);
bool found = false;
while(!found) {
Expand Down
19 changes: 10 additions & 9 deletions src/tigr/show-coords.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <mummer/redirect_to_pager.hpp>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

//------------------------------------------------------------- Constants ----//
Expand Down Expand Up @@ -186,8 +187,8 @@ bool isAnnotation = false; // true if either -w or -o
float idyCutoff = 0; // -I option
long int lenCutoff = 0; // -L option
int whichDataType = NUCMER_DATA; // set by .delta header
char InputFileName [MAX_LINE]; // I/O filenames
char RefFileName [MAX_LINE], QryFileName [MAX_LINE];
std::string InputFileName; // I/O filenames
std::string RefFileName, QryFileName;



Expand Down Expand Up @@ -401,7 +402,7 @@ int main
srand (time (NULL));

//-- Open and parse the delta file
strcpy (InputFileName, argv[optind ++]);
InputFileName = argv[optind ++];
parseDelta (Stats);

//-- Can only pick best frame for promer data
Expand Down Expand Up @@ -815,7 +816,7 @@ void printBtab

//-- Output the stats for this alignment in btab format
printf("%s\t%s\t%ld\t%s\t%s\t%s\t",
Sip->IdB, date, Sip->SeqLenB, type, RefFileName, Sip->IdA);
Sip->IdB, date, Sip->SeqLenB, type, RefFileName.c_str(), Sip->IdA);

printf("%ld\t%ld\t%ld\t%ld\t%f\t%f\t%ld\t0\t0\tNULL\t",
Sip->sB, Sip->eB, Sip->sA, Sip->eA,
Expand Down Expand Up @@ -845,7 +846,7 @@ void printHuman
//-- Print the output header
if ( isPrintHeader )
{
printf ("%s %s\n%s\n\n", RefFileName, QryFileName,
printf ("%s %s\n%s\n\n", RefFileName.c_str(), QryFileName.c_str(),
whichDataType == NUCMER_DATA ? "NUCMER" : "PROMER");
printf("%8s %8s | ", "[S1]", "[E1]");
printf("%8s %8s | ", "[S2]", "[E2]");
Expand Down Expand Up @@ -935,7 +936,7 @@ void printTabular
//-- Print the output header
if ( isPrintHeader )
{
printf ("%s %s\n%s\n\n", RefFileName, QryFileName,
printf ("%s %s\n%s\n\n", RefFileName.c_str(), QryFileName.c_str(),
whichDataType == NUCMER_DATA ? "NUCMER" : "PROMER");
printf("%s\t%s\t", "[S1]", "[E1]");
printf("%s\t%s\t", "[S2]", "[E2]");
Expand Down Expand Up @@ -1005,11 +1006,11 @@ void parseDelta
AlignStats CurrStats; // single alignment region

DeltaReader_t dr;
dr.open (InputFileName);
dr.open (InputFileName.c_str());
whichDataType = dr.getDataType( ) == NUCMER_STRING ?
NUCMER_DATA : PROMER_DATA;
strcpy (RefFileName, dr.getReferencePath( ).c_str( ));
strcpy (QryFileName, dr.getQueryPath( ).c_str( ));
RefFileName = dr.getReferencePath( );
QryFileName = dr.getQueryPath( );

//-- for each delta record
while ( dr.readNext( ) )
Expand Down
29 changes: 18 additions & 11 deletions src/tigr/show-tiling.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
//
//------------------------------------------------------------------------------

#include <sys/stat.h>
#include <mummer/delta.hh>
#include <mummer/tigrinc.hh>
#include <mummer/redirect_to_pager.hpp>
Expand Down Expand Up @@ -211,8 +212,8 @@ bool isdef_MIN_PIDY = false;

int DATA_TYPE = NUCMER_DATA; // set by .delta header

char InputFileName [MAX_LINE];
char RefFileName [MAX_LINE], QryFileName [MAX_LINE];
std::string InputFileName;
std::string RefFileName, QryFileName;



Expand Down Expand Up @@ -265,7 +266,7 @@ void printTilingPath
(vector<QueryContig> & Contigs);

void printTilingXML
(vector<QueryContig> & Contigs, char * QryFileName,
(vector<QueryContig> & Contigs, const char * QryFileName,
int argc, char ** argv);

void printHelp
Expand Down Expand Up @@ -403,7 +404,7 @@ int main


//-- Parse the delta file
strcpy (InputFileName, argv[optind ++]);
InputFileName = argv[optind ++];
parseDelta (Contigs);


Expand All @@ -412,8 +413,14 @@ int main
ContigsFile = File_Open ( ContigsFileName, "w" );
if ( isOutputPseudoMolecule )
PseudoMoleculeFile = File_Open ( PseudoMoleculeFileName, "w" );
if ( isOutputPseudoMolecule )
QryFile = File_Open ( QryFileName, "r" );
if ( isOutputPseudoMolecule ) {
struct stat sb;
if(stat(QryFileName.c_str(), &sb) == -1 || !(sb.st_mode & S_IFREG)) {
fprintf(stderr, "WARNING: the query file '%s' is not a regular file, process may hang for input\n",
QryFileName.c_str());
}
QryFile = File_Open ( QryFileName.c_str(), "r" );
}
if ( isOutputUnusable )
UnusableFile = File_Open ( UnusableFileName, "w" );

Expand Down Expand Up @@ -455,7 +462,7 @@ int main
if ( isPrintAlignments )
printTilingAlignments (Contigs);
else if ( isPrintXML )
printTilingXML (Contigs, QryFileName, argc, argv);
printTilingXML (Contigs, QryFileName.c_str(), argc, argv);
else
printTilingPath (Contigs);

Expand Down Expand Up @@ -1003,11 +1010,11 @@ void parseDelta
AlignStats aStats; // single alignment region

DeltaReader_t dr;
dr.open (InputFileName);
dr.open (InputFileName.c_str());
DATA_TYPE = dr.getDataType( ) == NUCMER_STRING ?
NUCMER_DATA : PROMER_DATA;
strcpy (RefFileName, dr.getReferencePath( ).c_str( ));
strcpy (QryFileName, dr.getQueryPath( ).c_str( ));
RefFileName = dr.getReferencePath( );
QryFileName = dr.getQueryPath( );


Contigs.clear( );
Expand Down Expand Up @@ -1305,7 +1312,7 @@ void printTilingPath


void printTilingXML
(vector<QueryContig> & Contigs, char * QryFileName, int argc, char ** argv)
(vector<QueryContig> & Contigs, const char * QryFileName, int argc, char ** argv)
{
vector<AlignStats>::iterator Ap;
vector<QueryContig>::iterator Cp;
Expand Down

0 comments on commit 41a3d16

Please sign in to comment.