@@ -260,7 +260,8 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
260260 }
261261
262262 else if (std::strncmp (argv[i], " --checks-max-time=" , 18 ) == 0 ) {
263- mSettings .checksMaxTime = std::atoi (argv[i] + 18 );
263+ if (!parseNumberArg (argv[i], 18 , mSettings .checksMaxTime ))
264+ return false ;
264265 }
265266
266267 else if (std::strcmp (argv[i], " --clang" ) == 0 ) {
@@ -286,6 +287,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
286287 }
287288
288289 else if (std::strncmp (argv[i], " --cppcheck-build-dir=" , 21 ) == 0 ) {
290+ // TODO: bail out when the folder does not exist? will silently do nothing
289291 mSettings .buildDir = Path::fromNativeSeparators (argv[i] + 21 );
290292 if (endsWith (mSettings .buildDir , ' /' ))
291293 mSettings .buildDir .pop_back ();
@@ -365,12 +367,8 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
365367
366368 // --error-exitcode=1
367369 else if (std::strncmp (argv[i], " --error-exitcode=" , 17 ) == 0 ) {
368- const std::string temp = argv[i]+17 ;
369- std::istringstream iss (temp);
370- if (!(iss >> mSettings .exitCode )) {
371- printError (" argument must be an integer. Try something like '--error-exitcode=1'." );
370+ if (!parseNumberArg (argv[i], 17 , mSettings .exitCode ))
372371 return false ;
373- }
374372 }
375373
376374 // Exception handling inside cppcheck client
@@ -505,18 +503,19 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
505503 else
506504 numberString = argv[i]+2 ;
507505
508- std::istringstream iss (numberString);
509- if (!(iss >> mSettings .jobs )) {
510- printError (" argument to '-j' is not a number." );
506+ unsigned int tmp;
507+ std::string err;
508+ if (!strToInt (numberString, tmp, &err)) {
509+ printError (" argument to '-j' is not valid - " + err + " ." );
511510 return false ;
512511 }
513-
514- if (mSettings .jobs > 10000 ) {
512+ if (tmp > 10000 ) {
515513 // This limit is here just to catch typos. If someone has
516514 // need for more jobs, this value should be increased.
517515 printError (" argument for '-j' is allowed to be 10000 at max." );
518516 return false ;
519517 }
518+ mSettings .jobs = tmp;
520519 }
521520
522521#ifdef THREADING_MODEL_FORK
@@ -538,11 +537,13 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
538537 else
539538 numberString = argv[i]+2 ;
540539
541- std::istringstream iss (numberString);
542- if (!(iss >> mSettings .loadAverage )) {
543- printError (" argument to '-l' is not a number." );
540+ int tmp;
541+ std::string err;
542+ if (!strToInt (numberString, tmp, &err)) {
543+ printError (" argument to '-l' is not valid - " + err + " ." );
544544 return false ;
545545 }
546+ mSettings .loadAverage = tmp;
546547 }
547548#endif
548549
@@ -577,37 +578,40 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
577578
578579 // Set maximum number of #ifdef configurations to check
579580 else if (std::strncmp (argv[i], " --max-configs=" , 14 ) == 0 ) {
580- mSettings .force = false ;
581-
582- std::istringstream iss (14 +argv[i]);
583- if (!(iss >> mSettings .maxConfigs )) {
584- printError (" argument to '--max-configs=' is not a number." );
581+ int tmp;
582+ if (!parseNumberArg (argv[i], 14 , tmp))
585583 return false ;
586- }
587-
588- if (mSettings .maxConfigs < 1 ) {
584+ if (tmp < 1 ) {
589585 printError (" argument to '--max-configs=' must be greater than 0." );
590586 return false ;
591587 }
592588
589+ mSettings .maxConfigs = tmp;
590+ mSettings .force = false ;
593591 maxconfigs = true ;
594592 }
595593
596594 // max ctu depth
597- else if (std::strncmp (argv[i], " --max-ctu-depth=" , 16 ) == 0 )
598- mSettings .maxCtuDepth = std::atoi (argv[i] + 16 );
595+ else if (std::strncmp (argv[i], " --max-ctu-depth=" , 16 ) == 0 ) {
596+ if (!parseNumberArg (argv[i], 16 , mSettings .maxCtuDepth ))
597+ return false ;
598+ }
599599
600600 // Write results in file
601601 else if (std::strncmp (argv[i], " --output-file=" , 14 ) == 0 )
602602 mSettings .outputFile = Path::simplifyPath (Path::fromNativeSeparators (argv[i] + 14 ));
603603
604604 // Experimental: limit execution time for extended valueflow analysis. basic valueflow analysis
605605 // is always executed.
606- else if (std::strncmp (argv[i], " --performance-valueflow-max-time=" , 33 ) == 0 )
607- mSettings .performanceValueFlowMaxTime = std::atoi (argv[i] + 33 );
606+ else if (std::strncmp (argv[i], " --performance-valueflow-max-time=" , 33 ) == 0 ) {
607+ if (!parseNumberArg (argv[i], 33 , mSettings .performanceValueFlowMaxTime , true ))
608+ return false ;
609+ }
608610
609- else if (std::strncmp (argv[i], " --performance-valueflow-max-if-count=" , 37 ) == 0 )
610- mSettings .performanceValueFlowMaxIfCount = std::atoi (argv[i] + 37 );
611+ else if (std::strncmp (argv[i], " --performance-valueflow-max-if-count=" , 37 ) == 0 ) {
612+ if (!parseNumberArg (argv[i], 37 , mSettings .performanceValueFlowMaxIfCount , true ))
613+ return false ;
614+ }
611615
612616 // Specify platform
613617 else if (std::strncmp (argv[i], " --platform=" , 11 ) == 0 ) {
@@ -941,26 +945,18 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
941945 }
942946
943947 else if (std::strncmp (argv[i], " --template-max-time=" , 20 ) == 0 ) {
944- mSettings .templateMaxTime = std::atoi (argv[i] + 20 );
948+ if (!parseNumberArg (argv[i], 20 , mSettings .templateMaxTime ))
949+ return false ;
945950 }
946951
947952 else if (std::strncmp (argv[i], " --typedef-max-time=" , 19 ) == 0 ) {
948- mSettings .typedefMaxTime = std::atoi (argv[i] + 19 );
953+ if (!parseNumberArg (argv[i], 19 , mSettings .typedefMaxTime ))
954+ return false ;
949955 }
950956
951957 else if (std::strncmp (argv[i], " --valueflow-max-iterations=" , 27 ) == 0 ) {
952- long tmp;
953- try {
954- tmp = std::stol (argv[i] + 27 );
955- } catch (const std::invalid_argument &) {
956- printError (" argument to '--valueflow-max-iteration' is invalid." );
958+ if (!parseNumberArg (argv[i], 27 , mSettings .valueFlowMaxIterations ))
957959 return false ;
958- }
959- if (tmp < 0 ) {
960- printError (" argument to '--valueflow-max-iteration' needs to be at least 0." );
961- return false ;
962- }
963- mSettings .valueFlowMaxIterations = static_cast <std::size_t >(tmp);
964960 }
965961
966962 else if (std::strcmp (argv[i], " -v" ) == 0 || std::strcmp (argv[i], " --verbose" ) == 0 )
@@ -979,20 +975,16 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
979975
980976 // Define the XML file version (and enable XML output)
981977 else if (std::strncmp (argv[i], " --xml-version=" , 14 ) == 0 ) {
982- const std::string numberString (argv[i]+14 );
983-
984- std::istringstream iss (numberString);
985- if (!(iss >> mSettings .xml_version )) {
986- printError (" argument to '--xml-version' is not a number." );
978+ int tmp;
979+ if (!parseNumberArg (argv[i], 14 , tmp))
987980 return false ;
988- }
989-
990- if (mSettings .xml_version != 2 ) {
981+ if (tmp != 2 ) {
991982 // We only have xml version 2
992983 printError (" '--xml-version' can only be 2." );
993984 return false ;
994985 }
995986
987+ mSettings .xml_version = tmp;
996988 // Enable also XML if version is set
997989 mSettings .xml = true ;
998990 }
0 commit comments