@@ -277,17 +277,17 @@ void ErrorMessage::deserialize(const std::string &data)
277277 throw InternalError (nullptr , " Internal Error: Deserialization of error message failed - premature end of data" );
278278
279279 std::string temp;
280- for (unsigned int i = 0 ; i < len && iss.good (); ++i) {
281- const char c = static_cast <char >(iss.get ());
282- temp.append (1 , c);
283- }
280+ if (len > 0 ) {
281+ temp.resize (len);
282+ iss.read (&temp[0 ], len);
284283
285- if (!iss.good ())
286- throw InternalError (nullptr , " Internal Error: Deserialization of error message failed - premature end of data" );
284+ if (!iss.good ())
285+ throw InternalError (nullptr , " Internal Error: Deserialization of error message failed - premature end of data" );
287286
288- if (temp == " inconclusive" ) {
289- certainty = Certainty::inconclusive;
290- continue ;
287+ if (temp == " inconclusive" ) {
288+ certainty = Certainty::inconclusive;
289+ continue ;
290+ }
291291 }
292292
293293 results[elem++] = temp;
@@ -299,15 +299,32 @@ void ErrorMessage::deserialize(const std::string &data)
299299 if (elem != 7 )
300300 throw InternalError (nullptr , " Internal Error: Deserialization of error message failed - insufficient elements" );
301301
302- id = results[0 ];
302+ id = std::move ( results[0 ]) ;
303303 severity = Severity::fromString (results[1 ]);
304- if (!(std::istringstream (results[2 ]) >> cwe.id ))
305- throw InternalError (nullptr , " Internal Error: Deserialization of error message failed - invalid CWE ID" );
306- if (!(std::istringstream (results[3 ]) >> hash))
307- throw InternalError (nullptr , " Internal Error: Deserialization of error message failed - invalid hash" );
308- file0 = results[4 ];
309- mShortMessage = results[5 ];
310- mVerboseMessage = results[6 ];
304+ unsigned long long tmp = 0 ;
305+ if (!results[2 ].empty ()) {
306+ try {
307+ tmp = MathLib::toULongNumber (results[2 ]);
308+ }
309+ catch (const InternalError&) {
310+ throw InternalError (nullptr , " Internal Error: Deserialization of error message failed - invalid CWE ID" );
311+ }
312+ if (tmp > std::numeric_limits<unsigned short >::max ())
313+ throw InternalError (nullptr , " Internal Error: Deserialization of error message failed - CWE ID is out of range" );
314+ }
315+ cwe.id = static_cast <unsigned short >(tmp);
316+ hash = 0 ;
317+ if (!results[3 ].empty ()) {
318+ try {
319+ hash = MathLib::toULongNumber (results[3 ]);
320+ }
321+ catch (const InternalError&) {
322+ throw InternalError (nullptr , " Internal Error: Deserialization of error message failed - invalid hash" );
323+ }
324+ }
325+ file0 = std::move (results[4 ]);
326+ mShortMessage = std::move (results[5 ]);
327+ mVerboseMessage = std::move (results[6 ]);
311328
312329 unsigned int stackSize = 0 ;
313330 if (!(iss >> stackSize))
@@ -324,14 +341,20 @@ void ErrorMessage::deserialize(const std::string &data)
324341 if (!(iss >> len))
325342 throw InternalError (nullptr , " Internal Error: Deserialization of error message failed - invalid length (stack)" );
326343
327- iss.get ();
344+ if (iss.get () != ' ' )
345+ throw InternalError (nullptr , " Internal Error: Deserialization of error message failed - invalid separator (stack)" );
346+
328347 std::string temp;
329- for (unsigned int i = 0 ; i < len && iss.good (); ++i) {
330- const char c = static_cast <char >(iss.get ());
331- temp.append (1 , c);
348+ if (len > 0 ) {
349+ temp.resize (len);
350+ iss.read (&temp[0 ], len);
351+
352+ if (!iss.good ())
353+ throw InternalError (nullptr , " Internal Error: Deserialization of error message failed - premature end of data (stack)" );
332354 }
333355
334356 std::vector<std::string> substrings;
357+ substrings.reserve (5 );
335358 for (std::string::size_type pos = 0 ; pos < temp.size () && substrings.size () < 5 ; ++pos) {
336359 if (substrings.size () == 4 ) {
337360 substrings.push_back (temp.substr (pos));
@@ -351,7 +374,7 @@ void ErrorMessage::deserialize(const std::string &data)
351374 // (*loc).line << '\t' << (*loc).column << '\t' << (*loc).getfile(false) << '\t' << loc->getOrigFile(false) << '\t' << loc->getinfo();
352375
353376 ErrorMessage::FileLocation loc (substrings[3 ], MathLib::toLongNumber (substrings[0 ]), MathLib::toLongNumber (substrings[1 ]));
354- loc.setfile (substrings[2 ]);
377+ loc.setfile (std::move ( substrings[2 ]) );
355378 if (substrings.size () == 5 )
356379 loc.setinfo (substrings[4 ]);
357380
@@ -490,14 +513,22 @@ static std::string readCode(const std::string &file, int linenr, int column, con
490513
491514static void replaceColors (std::string& source)
492515{
493- findAndReplace (source, " {reset}" , ::toString (Color::Reset));
494- findAndReplace (source, " {bold}" , ::toString (Color::Bold));
495- findAndReplace (source, " {dim}" , ::toString (Color::Dim));
496- findAndReplace (source, " {red}" , ::toString (Color::FgRed));
497- findAndReplace (source, " {green}" , ::toString (Color::FgGreen));
498- findAndReplace (source, " {blue}" , ::toString (Color::FgBlue));
499- findAndReplace (source, " {magenta}" , ::toString (Color::FgMagenta));
500- findAndReplace (source, " {default}" , ::toString (Color::FgDefault));
516+ static const std::string reset_str = ::toString (Color::Reset);
517+ findAndReplace (source, " {reset}" , reset_str);
518+ static const std::string bold_str = ::toString (Color::Bold);
519+ findAndReplace (source, " {bold}" , bold_str);
520+ static const std::string dim_str = ::toString (Color::Dim);
521+ findAndReplace (source, " {dim}" , dim_str);
522+ static const std::string red_str = ::toString (Color::FgRed);
523+ findAndReplace (source, " {red}" , red_str);
524+ static const std::string green_str = ::toString (Color::FgGreen);
525+ findAndReplace (source, " {green}" , green_str);
526+ static const std::string blue_str = ::toString (Color::FgBlue);
527+ findAndReplace (source, " {blue}" , blue_str);
528+ static const std::string magenta_str = ::toString (Color::FgMagenta);
529+ findAndReplace (source, " {magenta}" , magenta_str);
530+ static const std::string default_str = ::toString (Color::FgDefault);
531+ findAndReplace (source, " {default}" , default_str);
501532}
502533
503534std::string ErrorMessage::toString (bool verbose, const std::string &templateFormat, const std::string &templateLocation) const
@@ -506,17 +537,20 @@ std::string ErrorMessage::toString(bool verbose, const std::string &templateForm
506537
507538 // No template is given
508539 if (templateFormat.empty ()) {
509- std::ostringstream text;
510- if (!callStack.empty ())
511- text << ErrorLogger::callStackToString (callStack) << " : " ;
540+ std::string text;
541+ if (!callStack.empty ()) {
542+ text += ErrorLogger::callStackToString (callStack);
543+ text += " : " ;
544+ }
512545 if (severity != Severity::none) {
513- text << ' (' << Severity::toString (severity);
546+ text += ' (' ;
547+ text += Severity::toString (severity);
514548 if (certainty == Certainty::inconclusive)
515- text << " , inconclusive" ;
516- text << " ) " ;
549+ text += " , inconclusive" ;
550+ text += " ) " ;
517551 }
518- text << (verbose ? mVerboseMessage : mShortMessage );
519- return text. str () ;
552+ text += (verbose ? mVerboseMessage : mShortMessage );
553+ return text;
520554 }
521555
522556 // template is given. Reformat the output according to it
@@ -542,8 +576,9 @@ std::string ErrorMessage::toString(bool verbose, const std::string &templateForm
542576 findAndReplace (result, " {severity}" , Severity::toString (severity));
543577 findAndReplace (result, " {cwe}" , MathLib::toString (cwe.id ));
544578 findAndReplace (result, " {message}" , verbose ? mVerboseMessage : mShortMessage );
545- findAndReplace (result, " {callstack}" , callStack.empty () ? emptyString : ErrorLogger::callStackToString (callStack));
546579 if (!callStack.empty ()) {
580+ if (result.find (" {callstack}" ) != std::string::npos)
581+ findAndReplace (result, " {callstack}" , ErrorLogger::callStackToString (callStack));
547582 findAndReplace (result, " {file}" , callStack.back ().getfile ());
548583 findAndReplace (result, " {line}" , MathLib::toString (callStack.back ().line ));
549584 findAndReplace (result, " {column}" , MathLib::toString (callStack.back ().column ));
@@ -559,6 +594,7 @@ std::string ErrorMessage::toString(bool verbose, const std::string &templateForm
559594 findAndReplace (result, " {code}" , readCode (callStack.back ().getOrigFile (), callStack.back ().line , callStack.back ().column , endl));
560595 }
561596 } else {
597+ findAndReplace (result, " {callstack}" , emptyString);
562598 findAndReplace (result, " {file}" , " nofile" );
563599 findAndReplace (result, " {line}" , " 0" );
564600 findAndReplace (result, " {column}" , " 0" );
@@ -663,11 +699,10 @@ std::string ErrorMessage::FileLocation::getOrigFile(bool convert) const
663699 return mOrigFileName ;
664700}
665701
666- void ErrorMessage::FileLocation::setfile (const std::string & file)
702+ void ErrorMessage::FileLocation::setfile (std::string file)
667703{
668- mFileName = file;
669- mFileName = Path::fromNativeSeparators (mFileName );
670- mFileName = Path::simplifyPath (mFileName );
704+ mFileName = Path::fromNativeSeparators (std::move (file));
705+ mFileName = Path::simplifyPath (std::move (mFileName ));
671706}
672707
673708std::string ErrorMessage::FileLocation::stringify () const
0 commit comments