From 41a3d16e4069a9516792ac3d041842962c16287e Mon Sep 17 00:00:00 2001 From: Guillaume Marcais Date: Mon, 2 Dec 2024 10:08:17 -0500 Subject: [PATCH] Fixed size buffer. Hang on socket input. * 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. --- src/tigr/prepro.cc | 11 ++-- src/tigr/show-aligns.cc | 121 ++++++++++++++++++++++------------------ src/tigr/show-coords.cc | 19 ++++--- src/tigr/show-tiling.cc | 29 ++++++---- 4 files changed, 100 insertions(+), 80 deletions(-) diff --git a/src/tigr/prepro.cc b/src/tigr/prepro.cc index 6a3bacb..f683227 100644 --- a/src/tigr/prepro.cc +++ b/src/tigr/prepro.cc @@ -31,6 +31,7 @@ // //------------------------------------------------------------------------------ +#include #include #include @@ -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; @@ -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; @@ -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 ); @@ -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); diff --git a/src/tigr/show-aligns.cc b/src/tigr/show-aligns.cc index cd9b4ec..24cb401 100644 --- a/src/tigr/show-aligns.cc +++ b/src/tigr/show-aligns.cc @@ -15,6 +15,7 @@ //------------------------------------------------------------------------------ #include +#include #include #include @@ -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 RefFileNames, QryFileNames; @@ -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, @@ -295,7 +296,7 @@ int main Screen_Width = get_screen_width(); } - strcpy (InputFileName, argv[optind ++]); + InputFileName = argv[optind ++]; IdR = argv[optind++]; IdQ = argv[optind++]; @@ -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( ) ) { @@ -793,7 +804,7 @@ long int revC bool find_sequence(const std::vector& 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) { diff --git a/src/tigr/show-coords.cc b/src/tigr/show-coords.cc index b8d9a33..47c1bf9 100644 --- a/src/tigr/show-coords.cc +++ b/src/tigr/show-coords.cc @@ -19,6 +19,7 @@ #include #include #include +#include using namespace std; //------------------------------------------------------------- Constants ----// @@ -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; @@ -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 @@ -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, @@ -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]"); @@ -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]"); @@ -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( ) ) diff --git a/src/tigr/show-tiling.cc b/src/tigr/show-tiling.cc index 66bf36b..2e4488d 100644 --- a/src/tigr/show-tiling.cc +++ b/src/tigr/show-tiling.cc @@ -13,6 +13,7 @@ // //------------------------------------------------------------------------------ +#include #include #include #include @@ -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; @@ -265,7 +266,7 @@ void printTilingPath (vector & Contigs); void printTilingXML - (vector & Contigs, char * QryFileName, + (vector & Contigs, const char * QryFileName, int argc, char ** argv); void printHelp @@ -403,7 +404,7 @@ int main //-- Parse the delta file - strcpy (InputFileName, argv[optind ++]); + InputFileName = argv[optind ++]; parseDelta (Contigs); @@ -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" ); @@ -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); @@ -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( ); @@ -1305,7 +1312,7 @@ void printTilingPath void printTilingXML - (vector & Contigs, char * QryFileName, int argc, char ** argv) + (vector & Contigs, const char * QryFileName, int argc, char ** argv) { vector::iterator Ap; vector::iterator Cp;