From eada930eca89bf89a2d79564f749d5178d6cf532 Mon Sep 17 00:00:00 2001 From: Jonathan Lebon Date: Fri, 26 Jul 2013 15:31:49 -0400 Subject: [PATCH] session.cxx: make string params const And change colorize() to take in a token pointer rather than a ref. This is more in line with other funcs that take in a token and also ensures that we don't have undefined behaviour if we're called with a dereferenced NULL pointer (which coverity complained about). --- session.cxx | 21 ++++++++++++--------- session.h | 6 +++--- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/session.cxx b/session.cxx index 0a15b9fa2..01a5f133b 100644 --- a/session.cxx +++ b/session.cxx @@ -1835,7 +1835,7 @@ systemtap_session::print_token (ostream& o, const token* tok) o << ts; } else - o << colorize(*tok); + o << colorize(tok); last_token = tok; } @@ -1998,12 +1998,12 @@ systemtap_session::print_error (const parse_error &pe, if (pe.tok || found_junk) { - cerr << _("\tat: ") << colorize(*tok) << endl; + cerr << _("\tat: ") << colorize(tok) << endl; print_error_source (cerr, align_parse_error, tok); } else if (tok) // "expected" type error { - cerr << _("\tsaw: ") << colorize(*tok) << endl; + cerr << _("\tsaw: ") << colorize(tok) << endl; print_error_source (cerr, align_parse_error, tok); } else @@ -2014,7 +2014,7 @@ systemtap_session::print_error (const parse_error &pe, // print chained macro invocations while (tok && tok->chain) { tok = tok->chain; - cerr << _("\tin expansion of macro: ") << colorize(*tok) << endl; + cerr << _("\tin expansion of macro: ") << colorize(tok) << endl; print_error_source (cerr, align_parse_error, tok); } } @@ -2096,7 +2096,7 @@ assert_no_interrupts() } std::string -systemtap_session::colorize(std::string str, std::string type) +systemtap_session::colorize(const std::string& str, const std::string& type) { if (str.empty() || !color_errors) return str; @@ -2112,10 +2112,13 @@ systemtap_session::colorize(std::string str, std::string type) // Colorizes the path:row:col part of the token std::string -systemtap_session::colorize(const token& tok) +systemtap_session::colorize(const token* tok) { + if (tok == NULL) + return ""; + stringstream tmp; - tmp << tok; + tmp << *tok; if (!color_errors) return tmp.str(); // Might as well stop now to save time @@ -2124,7 +2127,7 @@ systemtap_session::colorize(const token& tok) // Print token location, which is also the tail of ts stringstream loc; - loc << tok.location; + loc << tok->location; // Remove token location and re-add it colorized ts.erase(ts.size()-loc.str().size()); @@ -2140,7 +2143,7 @@ For example, the default setting would be: 'error=01;31:warning=00;33:source=00;34:caret=01:token=01' */ std::string -systemtap_session::parse_stap_color(std::string type) +systemtap_session::parse_stap_color(const std::string& type) { const char *key, *col, *eq; int n = type.size(); diff --git a/session.h b/session.h index c1e515b11..d1a863825 100644 --- a/session.h +++ b/session.h @@ -395,9 +395,9 @@ public: // NB: It is very important for all of the above (and below) fields // to be cleared in the systemtap_session ctor (session.cxx). - std::string colorize(std::string str, std::string type); - std::string colorize(const token& tok); - std::string parse_stap_color(std::string type); + std::string colorize(const std::string& str, const std::string& type); + std::string colorize(const token* tok); + std::string parse_stap_color(const std::string& type); }; struct exit_exception: public std::runtime_error -- 2.43.5