From 2acf0dac29c53ecc7ba393f68bc0ed6e222f1ccd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Mon, 11 Jun 2018 09:22:50 -0400
Subject: [PATCH] databasehelper: protect stoull call

stoull() can throw an exception here, must be placed in a try/catch
block

Change-Id: Ie8ae8262858d0307ad72ba734b72f56971346c37
Reviewed-by: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
---
 src/authority/databasehelper.cpp | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/src/authority/databasehelper.cpp b/src/authority/databasehelper.cpp
index 0c89068e..412b15f7 100644
--- a/src/authority/databasehelper.cpp
+++ b/src/authority/databasehelper.cpp
@@ -430,7 +430,17 @@ uint64_t
 getLastTimestamp(Database& db)
 {
     auto timestamps = db.select("MAX(timestamp)", "interactions", "1=1", {}).payloads;
-    return timestamps.empty() ? std::time(nullptr) : std::stoull(timestamps[0]);
+    auto result = std::time(nullptr);
+    try {
+        if (!timestamps.empty() && !timestamps[0].empty()) {
+            result = std::stoull(timestamps[0]);
+        }
+    } catch (const std::out_of_range& e) {
+        qDebug() << "database::getLastTimestamp, stoull throws an out_of_range exception: " << e.what();
+    } catch (const std::invalid_argument& e) {
+        qDebug() << "database::getLastTimestamp, stoull throws an invalid_argument exception: " << e.what();
+    }
+    return result;
 }
 
 } // namespace database
-- 
GitLab