]> WPIA git - cassiopeia.git/blobdiff - src/util.cpp
fix: check cert before operating on it
[cassiopeia.git] / src / util.cpp
index 40fcf25ac0dc2f5c9c6cbb3a6ab839404c20921d..55d586405d738cf94343de66361b0f4490e6319b 100644 (file)
@@ -29,7 +29,7 @@ std::string writeBackFile( const std::string& serial, const std::string& cert, c
 
     if( 0 != mkdir( filename.c_str(), 0755 ) ) {
         if( EEXIST != errno ) {
-            return "";
+            throw std::runtime_error( "Storage location could not be determined" );
         }
 
         //! \FIXME: Check this is a directory
@@ -89,13 +89,14 @@ std::pair<bool, time_t> parseDate( const std::string& date ) {
         return std::pair<bool, time_t>( false, 0 );
     }
 
-    std::tm t;
+    std::tm t = {};
     t.tm_sec = 0;
     t.tm_min = 0;
     t.tm_hour = 0;
-    t.tm_year = std::stoi( date.substr( 0, 4 ) ) - 1900;
-    t.tm_mon = std::stoi( date.substr( 5, 2 ) ) - 1;
     t.tm_mday = std::stoi( date.substr( 8, 2 ) );
+    t.tm_mon = std::stoi( date.substr( 5, 2 ) ) - 1;
+    t.tm_year = std::stoi( date.substr( 0, 4 ) ) - 1900;
+
     setenv( "TZ", "UTC", 1 );
     tzset();
     std::time_t res = mktime( &t );
@@ -143,7 +144,7 @@ std::pair<bool, time_t> parseMonthInterval( std::time_t t, const std::string& da
 
     try {
         size_t end = 0;
-        int num = std::stoi( date.substr( 0, date.size() - 1 ) , &end );
+        int num = std::stoi( date.substr( 0, date.size() - 1 ), &end );
 
         if( end != date.size() - 1 ) {
             return  std::pair<bool, time_t>( false, 0 );
@@ -190,16 +191,18 @@ std::unique_ptr<std::ofstream> openLogfile( const std::string &name ) {
     auto res = std::make_unique<std::ofstream>( tname );
 
     if( ! res->good() ) {
-        throw std::string( "Failed to open file for logging: " ) + name;
+        throw std::runtime_error( std::string( "Failed to open file for logging: " ) + name );
     }
 
     return res;
 }
 
-std::string timestamp(){
+std::string timestamp() {
     time_t c_time;
+
     if( time( &c_time ) == -1 ) {
         throw std::runtime_error( "Error while fetching time?" );
     }
+
     return std::to_string( c_time );
 }