Fixed unsafe use of localtime

Since localtime is not thread-safe it should not be used in multithreaded
contexts. For this reason all calls to localtime were changed to localtime_r
in code where concurrency issues were possible.

Internal tests were left unchanged because they aren't multithreaded.
This commit is contained in:
Johan Wikman
2015-11-19 15:36:40 +02:00
committed by Markus Makela
parent 84d2c72db2
commit 6164b7f301
9 changed files with 43 additions and 51 deletions

View File

@ -1986,9 +1986,9 @@ dprintOneDCB(DCB *pdcb, DCB *dcb)
if (dcb->persistentstart)
{
char buff[20];
struct tm * timeinfo;
timeinfo = localtime (&dcb->persistentstart);
strftime(buff, sizeof(buff), "%b %d %H:%M:%S", timeinfo);
struct tm timeinfo;
localtime_r(&dcb->persistentstart, &timeinfo);
strftime(buff, sizeof(buff), "%b %d %H:%M:%S", &timeinfo);
dcb_printf(pdcb, "\t\tAdded to persistent pool: %s\n", buff);
}
}
@ -2160,9 +2160,9 @@ dprintDCB(DCB *pdcb, DCB *dcb)
if (dcb->persistentstart)
{
char buff[20];
struct tm * timeinfo;
timeinfo = localtime (&dcb->persistentstart);
strftime(buff, sizeof(buff), "%b %d %H:%M:%S", timeinfo);
struct tm timeinfo;
localtime_r(&dcb->persistentstart, &timeinfo);
strftime(buff, sizeof(buff), "%b %d %H:%M:%S", &timeinfo);
dcb_printf(pdcb, "\t\tAdded to persistent pool: %s\n", buff);
}
}

View File

@ -516,6 +516,8 @@ static bool file_write_footer(
return succp;
}
// Documentation says 26 bytes is enough, but 32 is a nice round number.
#define ASCTIME_BUF_LEN 32
static bool file_write_header(
FILE* outfile)
{
@ -524,10 +526,10 @@ static bool file_write_header(
size_t len2;
size_t len3;
const char* header_buf1;
char* header_buf2 = NULL;
char header_buf2[ASCTIME_BUF_LEN];
const char* header_buf3;
time_t* t = NULL;
struct tm* tm = NULL;
time_t t;
struct tm tm;
#if defined(LAPTOP_TEST)
struct timespec ts1;
ts1.tv_sec = 0;
@ -538,23 +540,10 @@ static bool file_write_header(
return true;
#endif
if ((t = (time_t *)malloc(sizeof(time_t))) == NULL) {
goto return_succp;
}
if ((tm = (struct tm *)malloc(sizeof(struct tm))) == NULL) {
goto return_succp;
}
*t = time(NULL);
*tm = *localtime(t);
localtime_r(&t, &tm);
header_buf1 = "\n\nMariaDB Corporation MaxScale " MAXSCALE_VERSION "\t";
header_buf2 = strdup(asctime(tm));
if (header_buf2 == NULL) {
goto return_succp;
}
asctime_r(&tm, header_buf2);
header_buf3 = "------------------------------------------------------\n";
len1 = strlen(header_buf1);
@ -570,16 +559,6 @@ static bool file_write_header(
succp = true;
return_succp:
if (tm != NULL) {
free(tm);
}
if (t != NULL) {
free(t);
}
if (header_buf2 != NULL) {
free(header_buf2);
}
return succp;
}

View File

@ -530,7 +530,8 @@ module_feedback_send(void* data) {
int http_send = 0;
now = time(NULL);
now_tm = localtime(&now);
struct tm now_result;
now_tm = localtime_r(&now, &now_result);
hour = now_tm->tm_hour;
FEEDBACK_CONF *feedback_config = (FEEDBACK_CONF *) data;