Call to gmtime is potentially dangerous, replace with gmtime_r

This commit is contained in:
Lowly Worm 2018-11-11 15:52:50 -08:00
parent a6a04332f8
commit 8885dc4fd1
3 changed files with 13 additions and 8 deletions

View File

@ -195,6 +195,7 @@ const char *file_fmttime(ut32 v, int local) {
char *pp;
time_t t = (time_t)v;
struct tm *tm;
struct tm timestruct;
if (local) {
pp = ctime(&t);
@ -216,7 +217,7 @@ const char *file_fmttime(ut32 v, int local) {
#endif /* HAVE_DAYLIGHT */
if (daylight)
t += 3600;
tm = gmtime(&t);
tm = gmtime_r(&t, &timestruct);
if (!tm)
return "*Invalid time*";
pp = asctime (tm);

View File

@ -33,6 +33,7 @@ R_API int r_print_date_hfs(RPrint *p, const ut8 *buf, int len) {
char s[256];
int ret = 0;
const struct tm* time;
struct tm timestruct;
if (p && len >= sizeof (ut32)) {
t = r_read_ble32 (buf, p->big_endian);
@ -40,7 +41,7 @@ R_API int r_print_date_hfs(RPrint *p, const ut8 *buf, int len) {
if (p->datefmt[0]) {
t += p->datezone * (60*60);
t += hfs_unix_delta;
time = (const struct tm*)gmtime((const time_t*)&t);
time = (const struct tm*)gmtime_r((const time_t*)&t, &timestruct);
if (time) {
ret = strftime (s, sizeof (s), p->datefmt, time);
if (ret) {
@ -60,13 +61,14 @@ R_API int r_print_date_unix(RPrint *p, const ut8 *buf, int len) {
char s[256];
int ret = 0;
const struct tm* time;
struct tm timestruct;
if (p && len >= sizeof (ut32)) {
t = r_read_ble32 (buf, p->big_endian);
// "%d:%m:%Y %H:%M:%S %z",
if (p->datefmt[0]) {
t += p->datezone * (60*60);
time = (const struct tm*)gmtime((const time_t*)&t);
time = (const struct tm*)gmtime_r((const time_t*)&t, &timestruct);
if (time) {
ret = strftime (s, sizeof (s), p->datefmt, time);
if (ret) {
@ -131,8 +133,9 @@ R_API int r_print_date_w32(RPrint *p, const ut8 *buf, int len) {
t = (time_t) l; // TODO limit above!
// "%d:%m:%Y %H:%M:%S %z",
if (p->datefmt[0]) {
struct tm time;
ret = strftime(datestr, 256, p->datefmt,
(const struct tm*) gmtime((const time_t*)&t));
(const struct tm*) gmtime_r ((const time_t*)&t, &time));
if (ret) {
p->cb_printf("%s\n", datestr);
ret = true;

View File

@ -445,6 +445,7 @@ static int r_print_format_string(const RPrint* p, ut64 seeki, ut64 addr64, ut64
static void r_print_format_time(const RPrint* p, int endian, int mode,
const char* setval, ut64 seeki, ut8* buf, int i, int size) {
ut64 addr;
struct tm timestruct;
int elem = -1;
if (size >= ARRAYINDEX_COEF) {
elem = size/ARRAYINDEX_COEF-1;
@ -454,7 +455,7 @@ static void r_print_format_time(const RPrint* p, int endian, int mode,
if (MUSTSET) {
p->cb_printf ("wv4 %s @ 0x%08"PFMT64x"\n", setval, seeki+((elem>=0)?elem*4:0));
} else if (MUSTSEE) {
char *timestr = strdup(asctime (gmtime ((time_t*)&addr)));
char *timestr = strdup(asctime (gmtime_r ((time_t*)&addr, &timestruct)));
*(timestr+24) = '\0';
if (!SEEVALUE) {
p->cb_printf ("0x%08" PFMT64x " = ", seeki + ((elem >= 0) ? elem * 4 : 0));
@ -468,7 +469,7 @@ static void r_print_format_time(const RPrint* p, int endian, int mode,
while (size--) {
updateAddr (buf + i, size - i, endian, &addr, NULL);
free (timestr);
timestr = strdup (asctime (gmtime ((time_t*)&addr)));
timestr = strdup (asctime (gmtime_r ((time_t*)&addr, &timestruct)));
*(timestr+24) = '\0';
if (elem == -1 || elem == 0) {
p->cb_printf ("%s", timestr);
@ -490,7 +491,7 @@ static void r_print_format_time(const RPrint* p, int endian, int mode,
}
free (timestr);
} else if (MUSTSEEJSON) {
char *timestr = strdup (asctime (gmtime ((time_t*)&addr)));
char *timestr = strdup (asctime (gmtime_r ((time_t*)&addr, &timestruct)));
*(timestr+24) = '\0';
if (size==-1) {
p->cb_printf ("\"%s\"", timestr);
@ -499,7 +500,7 @@ static void r_print_format_time(const RPrint* p, int endian, int mode,
while (size--) {
updateAddr (buf + i, size - i, endian, &addr, NULL);
free (timestr);
timestr = strdup (asctime (gmtime ((time_t*)&addr)));
timestr = strdup (asctime (gmtime_r ((time_t*)&addr, &timestruct)));
*(timestr+24) = '\0';
if (elem == -1 || elem == 0) {
p->cb_printf ("\"%s\"", timestr);