From 58fa6eb1f9a0fad94b59b4d2aefc0f09bc1372f0 Mon Sep 17 00:00:00 2001 From: selurvedu Date: Mon, 20 Jul 2015 00:25:46 +0300 Subject: [PATCH 1/3] Support symbol strings with '_' in place of '+' This fixes a segfault that occurs for the following reason: - In XKeyboard::initializeXkb(), the XkbSymbolParser::parse() is called with three arguments - string to process (symName), vector to push symbol names to (_symbolNames) and vector to push variant names to (_variantNames). - In XkbSymbolParser::parse(), it looks for char '+' in symName string, but Xkb may use '_' instead when creating that string. If the char is not found, parse() doesn't append anything to vectors and they remain empty. - Later, XKeyboard::initializeXkb() tries to access nonexistent data in _symbolNames, which causes a segmentation fault (on lines 162-163). --- XKeyboard.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/XKeyboard.cpp b/XKeyboard.cpp index c514980..84d08f2 100644 --- a/XKeyboard.cpp +++ b/XKeyboard.cpp @@ -327,7 +327,7 @@ void XkbSymbolParser::parse(const std::string& symbols, StringVector& symbolList for (size_t i = 0; i < symbols.size(); i++) { char ch = symbols[i]; - if (ch == '+') { + if (ch == '+' || ch == '_') { if (inSymbol) { if (isXkbLayoutSymbol(curSymbol)) { symbolList.push_back(curSymbol); From 7d5c44d4c4c03347b9bcf124c1393bc67237dd78 Mon Sep 17 00:00:00 2001 From: selurvedu Date: Mon, 27 Jul 2015 21:11:13 +0000 Subject: [PATCH 2/3] Trim unwanted bracket in layout name --- XKeyboard.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/XKeyboard.cpp b/XKeyboard.cpp index 84d08f2..2fb8142 100644 --- a/XKeyboard.cpp +++ b/XKeyboard.cpp @@ -124,7 +124,7 @@ Bool XKeyboard::initializeXkb() groupName = groupNameC; std::string::size_type pos = groupName.find('(', 0); if (pos != std::string::npos) { - groupName = groupName.substr(0, pos + 1); + groupName = groupName.substr(0, pos - 1); } _groupNames.push_back(groupName); } From 2602e1710fd77edd0d4839851c0d598dac50e8a7 Mon Sep 17 00:00:00 2001 From: selurvedu Date: Mon, 27 Jul 2015 22:02:07 +0000 Subject: [PATCH 3/3] Fix issues found by Cppcheck These include: - [X11Exception.h:16]: Class 'X11Exception' has a constructor with 1 argument that is not explicit. - [wrapper.cpp:62]: Array index 'i' is used before limits check. - [wrapper.cpp:183]: Exception should be caught by reference. --- X11Exception.h | 5 ++--- wrapper.cpp | 6 +++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/X11Exception.h b/X11Exception.h index c0468bf..3585570 100644 --- a/X11Exception.h +++ b/X11Exception.h @@ -13,7 +13,7 @@ class X11Exception : public std::exception { public: X11Exception() : _reason("unknown") {} - X11Exception(const std::string& what) : _reason(what) {} + explicit X11Exception(const std::string& what) : _reason(what) {} virtual ~X11Exception() throw () {}; virtual const char* what() const throw () { return _reason.c_str(); } @@ -21,9 +21,8 @@ private: std::string _reason; }; -#endif // GAMEEXCEPTION_H_FE39A315_6827_447B_AE62_5FA2C3FD391F +#endif // X11EXCEPTION_H_FE39A315_6827_447B_AE62_5FA2C3FD391F // Local Variables: // mode: c++ // End: - diff --git a/wrapper.cpp b/wrapper.cpp index bc3a46d..93fc636 100644 --- a/wrapper.cpp +++ b/wrapper.cpp @@ -59,7 +59,7 @@ bool print_status(XKeyboard& xkb, string format) { stringstream r; // resulting string for (size_t i = 0; i < format.length(); ++i) { - if (format[i] == '%' && i < format.length()-1) { + if (i < format.length()-2 && format[i] == '%') { switch (format[i+1]) { case 'c': r << xkb.currentGroupNum(); @@ -180,8 +180,8 @@ int main(int argc, char* argv[]) } } } - catch (exception e) { - cerr << e.what() << endl; + catch (const exception *e) { + cerr << e->what() << endl; return EXIT_FAILURE; } return EXIT_SUCCESS;