Browse Source

Replace calls to gmtime with gmtime_r and localtime with localtime_r.

(cherry picked from commit 4b3aec50c01ea079f4c303e2986484b4771dc918)
Marcel Admiraal 5 years ago
parent
commit
5f1dd48496
1 changed files with 24 additions and 22 deletions
  1. 24 22
      drivers/unix/os_unix.cpp

+ 24 - 22
drivers/unix/os_unix.cpp

@@ -194,52 +194,54 @@ uint64_t OS_Unix::get_system_time_msecs() const {
 }
 }
 
 
 OS::Date OS_Unix::get_date(bool utc) const {
 OS::Date OS_Unix::get_date(bool utc) const {
-
 	time_t t = time(NULL);
 	time_t t = time(NULL);
-	struct tm *lt;
-	if (utc)
-		lt = gmtime(&t);
-	else
-		lt = localtime(&t);
+	struct tm lt;
+	if (utc) {
+		gmtime_r(&t, &lt);
+	} else {
+		localtime_r(&t, &lt);
+	}
 	Date ret;
 	Date ret;
-	ret.year = 1900 + lt->tm_year;
+	ret.year = 1900 + lt.tm_year;
 	// Index starting at 1 to match OS_Unix::get_date
 	// Index starting at 1 to match OS_Unix::get_date
 	//   and Windows SYSTEMTIME and tm_mon follows the typical structure
 	//   and Windows SYSTEMTIME and tm_mon follows the typical structure
 	//   of 0-11, noted here: http://www.cplusplus.com/reference/ctime/tm/
 	//   of 0-11, noted here: http://www.cplusplus.com/reference/ctime/tm/
-	ret.month = (Month)(lt->tm_mon + 1);
-	ret.day = lt->tm_mday;
-	ret.weekday = (Weekday)lt->tm_wday;
-	ret.dst = lt->tm_isdst;
+	ret.month = (Month)(lt.tm_mon + 1);
+	ret.day = lt.tm_mday;
+	ret.weekday = (Weekday)lt.tm_wday;
+	ret.dst = lt.tm_isdst;
 
 
 	return ret;
 	return ret;
 }
 }
 
 
 OS::Time OS_Unix::get_time(bool utc) const {
 OS::Time OS_Unix::get_time(bool utc) const {
 	time_t t = time(NULL);
 	time_t t = time(NULL);
-	struct tm *lt;
-	if (utc)
-		lt = gmtime(&t);
-	else
-		lt = localtime(&t);
+	struct tm lt;
+	if (utc) {
+		gmtime_r(&t, &lt);
+	} else {
+		localtime_r(&t, &lt);
+	}
 	Time ret;
 	Time ret;
-	ret.hour = lt->tm_hour;
-	ret.min = lt->tm_min;
-	ret.sec = lt->tm_sec;
+	ret.hour = lt.tm_hour;
+	ret.min = lt.tm_min;
+	ret.sec = lt.tm_sec;
 	get_time_zone_info();
 	get_time_zone_info();
 	return ret;
 	return ret;
 }
 }
 
 
 OS::TimeZoneInfo OS_Unix::get_time_zone_info() const {
 OS::TimeZoneInfo OS_Unix::get_time_zone_info() const {
 	time_t t = time(NULL);
 	time_t t = time(NULL);
-	struct tm *lt = localtime(&t);
+	struct tm lt;
+	localtime_r(&t, &lt);
 	char name[16];
 	char name[16];
-	strftime(name, 16, "%Z", lt);
+	strftime(name, 16, "%Z", &lt);
 	name[15] = 0;
 	name[15] = 0;
 	TimeZoneInfo ret;
 	TimeZoneInfo ret;
 	ret.name = name;
 	ret.name = name;
 
 
 	char bias_buf[16];
 	char bias_buf[16];
-	strftime(bias_buf, 16, "%z", lt);
+	strftime(bias_buf, 16, "%z", &lt);
 	int bias;
 	int bias;
 	bias_buf[15] = 0;
 	bias_buf[15] = 0;
 	sscanf(bias_buf, "%d", &bias);
 	sscanf(bias_buf, "%d", &bias);