Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 667be14

Browse files
committedOct 23, 2024
Merge branch 'V5-9-patches'
* V5-9-patches: apps, libsnmp, snmpd: Check the localtime() return value
2 parents e029fb5 + 0fcc15c commit 667be14

File tree

7 files changed

+101
-74
lines changed

7 files changed

+101
-74
lines changed
 

‎agent/mibgroup/hardware/sensors/dummy_sensors.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@ void netsnmp_sensor_arch_init( void ) {
1212

1313
int
1414
netsnmp_sensor_arch_load(netsnmp_cache *cache, void *vp) {
15-
time_t now;
16-
struct tm *tm;
15+
static const struct tm zero_tm;
16+
time_t now;
17+
const struct tm *tm;
1718
netsnmp_sensor_info *sp;
1819

1920
time(&now);
2021
tm = localtime(&now);
22+
if (!tm)
23+
tm = &zero_tm;
2124

2225
DEBUGMSGTL(("sensors:arch", "Reload Dummy Sensors module\n"));
2326

‎apps/snmpps.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,10 @@ int snmptop(int argc, char **argv)
886886

887887
clock = time(NULL);
888888
ptm = localtime(&clock);
889-
strftime(timestr, sizeof(timestr), "%H:%M:%S", ptm);
889+
if (ptm)
890+
strftime(timestr, sizeof(timestr), "%H:%M:%S", ptm);
891+
else
892+
snprintf(timestr, sizeof(timestr), "(unknown)");
890893

891894
clear();
892895
move(0, 0);

‎apps/snmptrapd.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1321,12 +1321,18 @@ main(int argc, char *argv[])
13211321
if (snmp_get_do_logging()) {
13221322
struct tm *tm;
13231323
time_t timer;
1324+
13241325
time(&timer);
13251326
tm = localtime(&timer);
1326-
snmp_log(LOG_INFO,
1327+
if (tm) {
1328+
snmp_log(LOG_INFO,
13271329
"%.4d-%.2d-%.2d %.2d:%.2d:%.2d NET-SNMP version %s Stopped.\n",
13281330
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour,
13291331
tm->tm_min, tm->tm_sec, netsnmp_get_version());
1332+
} else {
1333+
snmp_log(LOG_INFO, "NET-SNMP version %s Stopped.\n",
1334+
netsnmp_get_version());
1335+
}
13301336
}
13311337
snmp_log(LOG_INFO, "Stopping snmptrapd\n");
13321338

‎apps/snmptrapd_log.c

Lines changed: 65 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -520,64 +520,67 @@ realloc_handle_time_fmt(u_char ** buf, size_t * buf_len, size_t * out_len,
520520
parsed_time = localtime(&time_val);
521521
}
522522

523-
switch (fmt_cmd) {
524-
525-
/*
526-
* Output year. The year field is unusual: if there's a restriction
527-
* on precision, we want to truncate from the left of the number,
528-
* not the right, so someone printing the year 1972 with 2 digit
529-
* precision gets "72" not "19".
530-
*/
531-
case CHR_CUR_YEAR:
532-
case CHR_UP_YEAR:
533-
sprintf(safe_bfr, "%d", parsed_time->tm_year + 1900);
534-
break;
535-
536-
/*
537-
* output month
538-
*/
539-
case CHR_CUR_MONTH:
540-
case CHR_UP_MONTH:
541-
sprintf(safe_bfr, "%d", parsed_time->tm_mon + 1);
542-
break;
543-
544-
/*
545-
* output day of month
546-
*/
547-
case CHR_CUR_MDAY:
548-
case CHR_UP_MDAY:
549-
sprintf(safe_bfr, "%d", parsed_time->tm_mday);
550-
break;
551-
552-
/*
553-
* output hour
554-
*/
555-
case CHR_CUR_HOUR:
556-
case CHR_UP_HOUR:
557-
sprintf(safe_bfr, "%d", parsed_time->tm_hour);
558-
break;
559-
560-
/*
561-
* output minute
562-
*/
563-
case CHR_CUR_MIN:
564-
case CHR_UP_MIN:
565-
sprintf(safe_bfr, "%d", parsed_time->tm_min);
566-
break;
567-
568-
/*
569-
* output second
570-
*/
571-
case CHR_CUR_SEC:
572-
case CHR_UP_SEC:
573-
sprintf(safe_bfr, "%d", parsed_time->tm_sec);
574-
break;
575-
576-
/*
577-
* unknown format command - just output the character
578-
*/
579-
default:
580-
sprintf(safe_bfr, "%c", fmt_cmd);
523+
if (!parsed_time) {
524+
sprintf(safe_bfr, "(unknown)");
525+
} else {
526+
switch (fmt_cmd) {
527+
/*
528+
* Output year. The year field is unusual: if there's a
529+
* restriction on precision, we want to truncate from the left
530+
* of the number, not the right, so someone printing the year
531+
* 1972 with 2 digit precision gets "72" not "19".
532+
*/
533+
case CHR_CUR_YEAR:
534+
case CHR_UP_YEAR:
535+
sprintf(safe_bfr, "%d", parsed_time->tm_year + 1900);
536+
break;
537+
538+
/*
539+
* output month
540+
*/
541+
case CHR_CUR_MONTH:
542+
case CHR_UP_MONTH:
543+
sprintf(safe_bfr, "%d", parsed_time->tm_mon + 1);
544+
break;
545+
546+
/*
547+
* output day of month
548+
*/
549+
case CHR_CUR_MDAY:
550+
case CHR_UP_MDAY:
551+
sprintf(safe_bfr, "%d", parsed_time->tm_mday);
552+
break;
553+
554+
/*
555+
* output hour
556+
*/
557+
case CHR_CUR_HOUR:
558+
case CHR_UP_HOUR:
559+
sprintf(safe_bfr, "%d", parsed_time->tm_hour);
560+
break;
561+
562+
/*
563+
* output minute
564+
*/
565+
case CHR_CUR_MIN:
566+
case CHR_UP_MIN:
567+
sprintf(safe_bfr, "%d", parsed_time->tm_min);
568+
break;
569+
570+
/*
571+
* output second
572+
*/
573+
case CHR_CUR_SEC:
574+
case CHR_UP_SEC:
575+
sprintf(safe_bfr, "%d", parsed_time->tm_sec);
576+
break;
577+
578+
/*
579+
* unknown format command - just output the character
580+
*/
581+
default:
582+
sprintf(safe_bfr, "%c", fmt_cmd);
583+
}
581584
}
582585
}
583586

@@ -1320,10 +1323,13 @@ realloc_format_plain_trap(u_char ** buf, size_t * buf_len,
13201323
*/
13211324
time(&now);
13221325
now_parsed = localtime(&now);
1323-
sprintf(safe_bfr, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d ",
1326+
if (now_parsed)
1327+
sprintf(safe_bfr, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d ",
13241328
now_parsed->tm_year + 1900, now_parsed->tm_mon + 1,
13251329
now_parsed->tm_mday, now_parsed->tm_hour,
13261330
now_parsed->tm_min, now_parsed->tm_sec);
1331+
else
1332+
sprintf(safe_bfr, "(unknown)");
13271333
if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, safe_bfr)) {
13281334
return 0;
13291335
}

‎apps/snmptrapd_sql.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -757,14 +757,16 @@ _sql_save_trap_info(sql_buf *sqlb, netsnmp_pdu *pdu,
757757
/** time */
758758
(void) time(&now);
759759
cur_time = localtime(&now);
760-
sqlb->time.year = cur_time->tm_year + 1900;
761-
sqlb->time.month = cur_time->tm_mon + 1;
762-
sqlb->time.day = cur_time->tm_mday;
763-
sqlb->time.hour = cur_time->tm_hour;
764-
sqlb->time.minute = cur_time->tm_min;
765-
sqlb->time.second = cur_time->tm_sec;
766-
sqlb->time.second_part = 0;
767-
sqlb->time.neg = 0;
760+
if (cur_time) {
761+
sqlb->time.year = cur_time->tm_year + 1900;
762+
sqlb->time.month = cur_time->tm_mon + 1;
763+
sqlb->time.day = cur_time->tm_mday;
764+
sqlb->time.hour = cur_time->tm_hour;
765+
sqlb->time.minute = cur_time->tm_min;
766+
sqlb->time.second = cur_time->tm_sec;
767+
sqlb->time.second_part = 0;
768+
sqlb->time.neg = 0;
769+
}
768770

769771
/** host name */
770772
buf_host_len_t = 0;

‎snmplib/snmp-tc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ date_n_time(const time_t * when, size_t * length)
136136
* Null time
137137
*/
138138
if (when == NULL || *when == 0 || *when == (time_t) - 1) {
139+
invalid_time:
139140
string[0] = 0;
140141
string[1] = 0;
141142
string[2] = 1;
@@ -153,6 +154,9 @@ date_n_time(const time_t * when, size_t * length)
153154
* Basic 'local' time handling
154155
*/
155156
tm_p = localtime(when);
157+
if (!tm_p)
158+
goto invalid_time;
159+
156160
yauron = tm_p->tm_year + 1900;
157161
string[0] = (u_char)(yauron >> 8);
158162
string[1] = (u_char)yauron;

‎snmplib/snmp_logging.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -546,9 +546,12 @@ sprintf_stamp(time_t * now, char *sbuf)
546546
time(now);
547547
}
548548
tm = localtime(now);
549-
sprintf(sbuf, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d ",
550-
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
551-
tm->tm_hour, tm->tm_min, tm->tm_sec);
549+
if (tm)
550+
sprintf(sbuf, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d ",
551+
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
552+
tm->tm_hour, tm->tm_min, tm->tm_sec);
553+
else
554+
sprintf(sbuf, "(unknown)");
552555
return sbuf;
553556
}
554557

0 commit comments

Comments
 (0)
Please sign in to comment.