c++ - std::out_of_range while setting a QLineEdit -


in c++ application, have select file qfiledialog class. then, verify file name correct (it has start "vg").

my file has structure : vgx-xx-xxxx-xxx-xxx.pigs

after what, set in associate qlineedit. each time select file, crashes, , don't understand why.

here different function :

open qfile dialog window

/** open file dialog window  **/ void vgccc::selectpigsfile() {     qstring pigsfile = qfiledialog::getopenfilename     (         this,         tr("select pigs file"),         "./../vgccolorconfigurator/inputs",         tr("document files (*.pigs *.igs)"),         0,         qfiledialog::dontusenativedialog     );      pigspath = pigsfile;     if(verifypigsfilevalidity(pigspath.tostdstring()))     {         m_filepathline->settext("");         m_filepathline->settext(pigspath);         m_testtextedit->insertplaintext("file selected : "+pigspath+"\n");     }     else     {         m_filepathline->settext("please select valid pigs (format vgx-xx-xxxx-xxx-xxx.pigs)");         m_testtextedit->insertplaintext("uncorrect pigs file.\n");     } } 

verification of file name

/** verify selected pig file **/ bool vgccc::verifypigsfilevalidity(std::string pigspath) {     splitpigsname(pigspath);     std::string verification = pigsnametable[0].erase(2,2);     std::string headername = "vg";      if(!verification.compare(headername))     {         m_testtextedit->insertplaintext("pigs name correct");         return true;     }     else         return false; } 

split method

/** split pigs name table **/ std::string* vgccc::splitpigsname(std::string pigspath) {     std::string pigspathtosplit = pigspath;     std::string delimiter = "-";     size_t position = 0;     int i=0;     std::string token;      while ((position = pigspathtosplit.find(delimiter)) != std::string::npos)     {         token = pigspathtosplit.substr(0, position);         std::cout << token << std::endl;         pigsnametable[i] = token;         i++;         pigspathtosplit.erase(0, position + delimiter.length());     }     pigsnametable[4] = pigspathtosplit.c_str();     std::cout << pigspathtosplit << std::endl; } 

bool vgccc::verifypigsfilevalidity(std::string pigspath) {     splitpigsname(pigspath);     std::string verification = pigsnametable[0].erase(2,2);     std::string headername = "vg";      if(!verification.compare(headername))     {         m_testtextedit->insertplaintext("pigs name correct");         return true;     }     else         return false; } 

is unsafe because:

1- don't check if pigsnametable has element @ index (if vector?) or key (if map?) 0

2- don't check pigsnametable[0] has more 2 elements. see erase documentation:

pos: position of first character erased. if greater string length, throws out_of_range.

you do:

bool vgccc::verifypigsfilevalidity(std::string pigspath) {     splitpigsname(pigspath);      if ( /* test pigsnametable[0] exists depending on pigsnametable's type */ )     {         return pigsnametable[0].find( "vg" ) == 0; // return true if pigsnametable[0] starts "vg"     }     else     {         return false;     } } 

if pigsnametable vector test can !pigsnametable.empty(), if it's map, pigsnametable.find(0) != pigsnametable.end()....


Comments