Skip to content

Commit 0d5808c

Browse files
committed
Move to localtime_r
1 parent c9aeb20 commit 0d5808c

File tree

9 files changed

+35
-21
lines changed

9 files changed

+35
-21
lines changed

src/DS/rds.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -846,8 +846,9 @@ struct oscap_source *ds_rds_create_source(struct oscap_source *sds_source, struc
846846
struct stat file_stat;
847847
if (stat(tailoring_filepath, &file_stat) == 0) {
848848
const size_t max_timestamp_len = 32;
849+
struct tm result;
849850
tailoring_doc_timestamp = malloc(max_timestamp_len);
850-
strftime(tailoring_doc_timestamp, max_timestamp_len, "%Y-%m-%dT%H:%M:%S", localtime(&file_stat.st_mtime));
851+
strftime(tailoring_doc_timestamp, max_timestamp_len, "%Y-%m-%dT%H:%M:%S", localtime_r(&file_stat.st_mtime, &result));
851852
}
852853
}
853854

src/DS/sds.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -685,12 +685,13 @@ static int ds_sds_compose_add_component_internal(xmlDocPtr doc, xmlNodePtr datas
685685
struct stat file_stat;
686686
if (stat(filepath, &file_stat) == 0) {
687687
time_t mtime;
688+
struct tm result;
688689
char *source_date_epoch = getenv("SOURCE_DATE_EPOCH");
689690
if (source_date_epoch == NULL ||
690691
(mtime = (time_t)strtoll(source_date_epoch, NULL, 10)) <= 0 ||
691692
mtime > file_stat.st_mtime)
692693
mtime = file_stat.st_mtime;
693-
strftime(file_timestamp, 32, "%Y-%m-%dT%H:%M:%S", localtime(&mtime));
694+
strftime(file_timestamp, 32, "%Y-%m-%dT%H:%M:%S", localtime_r(&mtime, &result));
694695
} else {
695696
oscap_seterr(OSCAP_EFAMILY_GLIBC, "Could not find file %s: %s.", filepath, strerror(errno));
696697
// Return positive number, indicating less severe problem.
@@ -796,7 +797,7 @@ static int ds_sds_compose_catalog_has_uri(xmlDocPtr doc, xmlNodePtr catalog, con
796797

797798
return -1;
798799
}
799-
800+
800801
xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression(
801802
BAD_CAST expression,
802803
xpathCtx);

src/OVAL/oval_component.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1869,9 +1869,10 @@ static long unsigned int _comp_sec(int year, int month, int day, int hour, int m
18691869
{
18701870
time_t t;
18711871
struct tm *ts;
1872+
struct tm result;
18721873

18731874
t = time(NULL);
1874-
ts = localtime(&t);
1875+
ts = localtime_r(&t, &result);
18751876

18761877
ts->tm_year = year - 1900;
18771878
ts->tm_mon = month - 1;
@@ -1881,7 +1882,7 @@ static long unsigned int _comp_sec(int year, int month, int day, int hour, int m
18811882
ts->tm_sec = second;
18821883
ts->tm_isdst = -1;
18831884
t = mktime(ts);
1884-
ts = localtime(&t);
1885+
ts = localtime_r(&t, &result);
18851886

18861887
if (ts->tm_isdst == 1)
18871888
t -= 3600;
@@ -1969,9 +1970,10 @@ static long unsigned int _parse_fmt_sse(char *dt)
19691970
{
19701971
time_t t;
19711972
struct tm *ts;
1973+
struct tm result;
19721974

19731975
t = (time_t) atol(dt);
1974-
ts = localtime(&t);
1976+
ts = localtime_r(&t, &result);
19751977
if (ts->tm_isdst == 1)
19761978
t -= 3600;
19771979

src/OVAL/oval_generator.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ void oval_generator_update_timestamp(struct oval_generator *generator)
155155
struct tm *lt;
156156

157157
time(&et);
158-
lt = localtime(&et);
158+
struct tm localtime_state;
159+
lt = localtime_r(&et, &localtime_state);
159160
int timestamp_size = snprintf(NULL, 0, "%4d-%02d-%02dT%02d:%02d:%02d",
160161
1900 + lt->tm_year, 1 + lt->tm_mon, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec);
161162
if (timestamp_size < 0) {

src/OVAL/probes/unix/process58_probe.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ static int read_process(SEXP_t *cmd_ent, SEXP_t *pid_ent, probe_ctx *ctx)
477477
DIR *d;
478478
struct dirent *ent;
479479
oval_schema_version_t oval_version;
480+
struct tm result;
480481

481482
const char *prefix = getenv("OSCAP_PROBE_ROOT");
482483
snprintf(buf, PATH_MAX, "%s/proc", prefix ? prefix : "");
@@ -605,11 +606,11 @@ static int read_process(SEXP_t *cmd_ent, SEXP_t *pid_ent, probe_ctx *ctx)
605606

606607
// Calculate the start time
607608
s_time = time(NULL);
608-
now = localtime(&s_time);
609+
now = localtime_r(&s_time, &result);
609610
tyear = now->tm_year;
610611
tday = now->tm_yday;
611612
s_time = boot + (start / ticks);
612-
proc = localtime(&s_time);
613+
proc = localtime_r(&s_time, &result);
613614

614615
// Select format based on how long we've been running
615616
//
@@ -729,6 +730,7 @@ static int read_process(SEXP_t *cmd_ent, probe_ctx *ctx)
729730
int err = 1;
730731
DIR *d;
731732
struct dirent *ent;
733+
struct tm result;
732734

733735
d = opendir("/proc");
734736
if (d == NULL)
@@ -784,13 +786,13 @@ static int read_process(SEXP_t *cmd_ent, probe_ctx *ctx)
784786

785787
// Get the start time
786788
s_time = time(NULL);
787-
now = localtime(&s_time);
789+
now = localtime_r(&s_time, &result);
788790
tyear = now->tm_year;
789791
tday = now->tm_yday;
790792

791793
// Get current time
792794
s_time = psinfo->pr_start.tv_sec;
793-
proc = localtime(&s_time);
795+
proc = localtime_r(&s_time, &result);
794796

795797
// Select format based on how long we've been running
796798
//

src/OVAL/probes/unix/process_probe.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ static int read_process(SEXP_t *cmd_ent, probe_ctx *ctx)
200200
int err = 1;
201201
DIR *d;
202202
struct dirent *ent;
203+
struct tm result;
203204

204205
d = opendir("/proc");
205206
if (d == NULL)
@@ -299,11 +300,11 @@ static int read_process(SEXP_t *cmd_ent, probe_ctx *ctx)
299300

300301
// Calculate the start time
301302
s_time = time(NULL);
302-
now = localtime(&s_time);
303+
now = localtime_r(&s_time, &result);
303304
tyear = now->tm_year;
304305
tday = now->tm_yday;
305306
s_time = boot + (start / ticks);
306-
proc = localtime(&s_time);
307+
proc = localtime_r(&s_time, &result);
307308

308309
// Select format based on how long we've been running
309310
//
@@ -439,13 +440,13 @@ static int read_process(SEXP_t *cmd_ent, probe_ctx *ctx)
439440

440441
// Get the start time
441442
s_time = time(NULL);
442-
now = localtime(&s_time);
443+
now = localtime_r(&s_time, &result);
443444
tyear = now->tm_year;
444445
tday = now->tm_yday;
445446

446447
// Get current time
447448
s_time = psinfo->pr_start.tv_sec;
448-
proc = localtime(&s_time);
449+
proc = localtime_r(&s_time, &result);
449450

450451
// Select format based on how long we've been running
451452
//
@@ -595,7 +596,8 @@ static int read_process(SEXP_t *cmd_ent, probe_ctx *ctx)
595596
fmt = "%H:%M:%S";
596597
}
597598

598-
struct tm *loc_time = localtime(&start_time);
599+
struct tm result;
600+
struct tm *loc_time = localtime_r(&start_time, &result);
599601
strftime(start_buf, sizeof(start_buf), fmt, loc_time);
600602

601603
switch(proc->ki_tdev) {
@@ -604,7 +606,7 @@ static int read_process(SEXP_t *cmd_ent, probe_ctx *ctx)
604606
break;
605607
default:
606608
snprintf(tty_buf, sizeof(tty_buf), "%ld", proc->ki_tdev);
607-
r.tty = tty_buf;
609+
r.tty = tty_buf;
608610
break;
609611
}
610612

src/XCCDF/item.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,8 @@ xmlNode *xccdf_status_to_dom(struct xccdf_status *status, xmlDoc *doc, xmlNode *
542542

543543
time_t date_time = xccdf_status_get_date(status);
544544
if (date_time) {
545-
struct tm *date = localtime(&date_time);
545+
struct tm result;
546+
struct tm *date = localtime_r(&date_time, &result);
546547
/* "YYYY-DD-MM" */
547548
int date_len = snprintf(NULL, 0, "%04d-%02d-%02d", date->tm_year + 1900, date->tm_mon + 1, date->tm_mday);
548549
if (date_len < 0) {

src/XCCDF/result.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1770,11 +1770,12 @@ static inline const char *_get_timestamp(void)
17701770
static char timestamp[] = "yyyy-mm-ddThh:mm:ss+zz:zz";
17711771
time_t tm;
17721772
struct tm *lt;
1773+
struct tm result;
17731774
int tz_diff;
17741775
char tz_sign;
17751776

17761777
tm = time(NULL);
1777-
lt = localtime(&tm);
1778+
lt = localtime_r(&tm, &result);
17781779

17791780
if (!lt)
17801781
return NULL;

utils/oscap-info.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,11 @@ struct oscap_module OSCAP_INFO_MODULE = {
7676

7777
static void print_time(const char *file) {
7878
struct stat buffer;
79+
struct tm result;
7980
char timeStr[ 100 ] = "";
8081

8182
if(!stat(file, &buffer)) {
82-
strftime(timeStr, 100, "%Y-%m-%dT%H:%M:%S", localtime(&buffer.st_mtime));
83+
strftime(timeStr, 100, "%Y-%m-%dT%H:%M:%S", localtime_r(&buffer.st_mtime, &result));
8384
printf("Imported: %s\n", timeStr);
8485
}
8586
}
@@ -89,8 +90,10 @@ static inline void _print_xccdf_status(struct xccdf_status *status, const char *
8990
if (status) {
9091
printf("%sStatus: %s\n", prefix, xccdf_status_type_to_text(xccdf_status_get_status(status)));
9192
const time_t date_time = xccdf_status_get_date(status);
93+
struct tm result;
9294
if (date_time != 0) {
93-
struct tm *date = localtime(&date_time);
95+
struct tm result;
96+
struct tm *date = localtime_r(&date_time, &result);
9497
char date_str[] = "YYYY-DD-MM";
9598
int ret = snprintf(date_str, sizeof(date_str), "%04d-%02d-%02d", date->tm_year + 1900, date->tm_mon + 1, date->tm_mday);
9699
if (ret < 0) {

0 commit comments

Comments
 (0)