From 1a4469d38839ad37f66f814d7ce2bf216cac40a9 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 15 Feb 2009 22:33:18 -0500 Subject: [PATCH] Error handling - Validates specified columns in custom reports against list of good column names. - Validates list of sort columns in custom reports against list of specified column names. - Minor fix to grammar file. --- grammar.bnf | 6 +++--- src/report.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/task.h | 2 ++ 3 files changed, 61 insertions(+), 3 deletions(-) diff --git a/grammar.bnf b/grammar.bnf index 18c082005..ba82df08e 100644 --- a/grammar.bnf +++ b/grammar.bnf @@ -8,7 +8,7 @@ command ::= simple_command | id_command | "export" file | - | + | ; simple_command ::= "version" | "help" | "projects" | "tags" | "next" | "stats" | "color" ; @@ -27,8 +27,8 @@ filter_part ::= tag_add | tag_remove | attribute | word ; tag_add ::= "+" word ; tag_remove ::= "-" word ; attribute ::= word ":" word ; -word ::= ... -file ::= ... +word ::= +file ::= id ::= digit+ ; digit ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ; substitution ::= "/" word+ "/" word* "/" ; diff --git a/src/report.cpp b/src/report.cpp index 740a88712..8c4c1664d 100644 --- a/src/report.cpp +++ b/src/report.cpp @@ -2246,10 +2246,12 @@ std::string handleCustomReport ( std::string columnList = conf.get ("report." + report + ".columns"); std::vector columns; split (columns, columnList, ','); + validReportColumns (columns); std::string sortList = conf.get ("report." + report + ".sort"); std::vector sortOrder; split (sortOrder, sortList, ','); + validSortColumns (columns, sortOrder); std::string filterList = conf.get ("report." + report + ".filter"); std::vector filterArgs; @@ -2545,3 +2547,57 @@ std::string handleCustomReport ( } //////////////////////////////////////////////////////////////////////////////// +void validReportColumns (const std::vector & columns) +{ + std::vector bad; + + std::vector ::const_iterator it; + for (it = columns.begin (); it != columns.end (); ++it) + if (*it != "id" && + *it != "uuid" && + *it != "project" && + *it != "priority" && + *it != "entry" && + *it != "start" && + *it != "due" && + *it != "age" && + *it != "active" && + *it != "tags" && + *it != "description") + bad.push_back (*it); + + if (bad.size ()) + { + std::string error; + join (error, ", ", bad); + throw std::string ("Unrecognized column name: ") + error; + } +} + +//////////////////////////////////////////////////////////////////////////////// +void validSortColumns ( + const std::vector & columns, + const std::vector & sortColumns) +{ + std::vector bad; + std::vector ::const_iterator sc; + for (sc = sortColumns.begin (); sc != sortColumns.end (); ++sc) + { + std::vector ::const_iterator co; + for (co = columns.begin (); co != columns.end (); ++co) + if (sc->substr (0, sc->length () - 1) == *co) + break; + + if (co == columns.end ()) + bad.push_back (*sc); + } + + if (bad.size ()) + { + std::string error; + join (error, ", ", bad); + throw std::string ("Sort column is not part of the report: ") + error; + } +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/task.h b/src/task.h index 57b19f652..151794c7d 100644 --- a/src/task.h +++ b/src/task.h @@ -103,6 +103,8 @@ std::string handleReportOldest (TDB&, T&, Config&); std::string handleReportNewest (TDB&, T&, Config&); std::string handleCustomReport (TDB&, T&, Config&, const std::string&); +void validReportColumns (const std::vector &); +void validSortColumns (const std::vector &, const std::vector &); // util.cpp bool confirm (const std::string&);