From ae75ca209507cd1098432d25f42d6e6f97edc505 Mon Sep 17 00:00:00 2001 From: "S. Gilles" Date: Tue, 23 Jan 2024 21:50:08 -0500 Subject: [PATCH] do not crash on malformed schedule files --- session.cc | 46 +++++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/session.cc b/session.cc index 5a39388..4bb9766 100644 --- a/session.cc +++ b/session.cc @@ -32,6 +32,7 @@ std::string Session::salt = "will be overwritten"; * etc. */ Session::Session(const std::filesystem::path& data_dir) : + data_dir(data_dir), session_start(std::chrono::system_clock::now()), current_action(Currently_doing::Trivial), @@ -39,6 +40,7 @@ Session::Session(const std::filesystem::path& data_dir) : card_status(), schedule_files_to_clean(), cards_to_delete() + { /* * Read all the cards in the schedule directory, figure out what @@ -65,27 +67,29 @@ Session::Session(const std::filesystem::path& data_dir) : std::smatch ymd_matches; struct tm this_tm = {}; - std::regex_search(ymd, ymd_matches, ymd_matcher); - this_tm.tm_year = std::stoi(ymd_matches[1]) - 1900; - this_tm.tm_mon = std::stoi(ymd_matches[2]) - 1; - this_tm.tm_mday = std::stoi(ymd_matches[3]); - const std::time_t sched_time = mktime(&this_tm); - - if (sched_time < now) { - schedule_files_to_clean.push_back(p); - std::ifstream sched_is(p); - std::string line; - - /* - * The schedule file just contains a number of - * lines, and "~/.data/sispare/cards/" is - * the card to review. - */ - while (getline(sched_is, line)) { - try { - cards.insert(std::move(Card::mk(data_dir / "cards" / line))); - } catch (const std::exception& ex) { - cards_to_delete.insert(data_dir / "cards" / line); + if (std::regex_search(ymd, ymd_matches, ymd_matcher)) { + this_tm.tm_year = std::stoi(ymd_matches[1]) - 1900; + this_tm.tm_mon = std::stoi(ymd_matches[2]) - 1; + this_tm.tm_mday = std::stoi(ymd_matches[3]); + const std::time_t sched_time = mktime(&this_tm); + + if (sched_time < now) { + schedule_files_to_clean.push_back(p); + std::ifstream sched_is(p); + std::string line; + + /* + * The schedule file just contains a + * number of lines, and + * "~/.data/sispare/cards/" is the + * card to review. + */ + while (getline(sched_is, line)) { + try { + cards.insert(std::move(Card::mk(data_dir / "cards" / line))); + } catch (const std::exception& ex) { + cards_to_delete.insert(data_dir / "cards" / line); + } } } } -- 2.11.4.GIT