Skip to content

Commit

Permalink
Fix crash when $TZ isn’t defined.
Browse files Browse the repository at this point in the history
  • Loading branch information
jralls committed Jan 14, 2017
1 parent c1e38d5 commit 3925106
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
47 changes: 26 additions & 21 deletions src/libqof/qof/gnc-timezone.cpp
Expand Up @@ -666,7 +666,8 @@ TimeZoneProvider::parse_file(const std::string& tzname)
zone_vector.push_back(zone_from_rule(max_year, last_rule));
}

TimeZoneProvider::TimeZoneProvider(const std::string& tzname) : zone_vector {}
bool
TimeZoneProvider::construct(const std::string& tzname)
{
try
{
Expand All @@ -679,29 +680,33 @@ TimeZoneProvider::TimeZoneProvider(const std::string& tzname) : zone_vector {}
TZ_Ptr zone(new PTZ(tzname));
zone_vector.push_back(std::make_pair(max_year, zone));
}
catch(const std::exception& err)
catch(std::exception& err)
{
try
{
parse_file(getenv("TZ"));
}
catch(const std::exception& err)
{

std::cerr << "Unable to use either provided tzname or TZ environment variable. Resorting to /etc/localtime.\n";
try
{
parse_file("/etc/localtime");
}
catch(const std::invalid_argument& env)
{
std::cerr << "/etc/localtime invalid, resorting to GMT.";
TZ_Ptr zone(new PTZ("UTC0"));
zone_vector.push_back(std::make_pair(max_year, zone));
}
}
return false;
}
}
return true;
}

TimeZoneProvider::TimeZoneProvider(const std::string& tzname) : zone_vector {}
{
if(construct(tzname))
return;
std::cerr << tzname << " invalid, trying TZ environment variable.\n";
const char* tz_env = getenv("TZ");
if(tz_env && construct(tz_env))
return;
std::cerr << "No valid $TZ, resorting to /etc/localtime.\n";
try
{
parse_file("/etc/localtime");
}
catch(const std::invalid_argument& env)
{
std::cerr << "/etc/localtime invalid, resorting to GMT.";
TZ_Ptr zone(new PTZ("UTC0"));
zone_vector.push_back(std::make_pair(max_year, zone));
}
}
#endif

Expand Down
1 change: 1 addition & 0 deletions src/libqof/qof/gnc-timezone.hpp
Expand Up @@ -59,6 +59,7 @@ class TimeZoneProvider
static const unsigned int max_year; //9999
private:
void parse_file(const std::string& tzname);
bool construct(const std::string& tzname);
TZ_Vector zone_vector;
#if PLATFORM(WINDOWS)
void load_windows_dynamic_tz(HKEY, time_zone_names);
Expand Down

0 comments on commit 3925106

Please sign in to comment.