From: martijn Date: Wed, 22 May 2024 08:44:02 +0000 (+0000) Subject: When localtime() fails, throw an error instead of returning a X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=f7af79d42719ad5fb6d136005c195d90f18edba3;p=openbsd When localtime() fails, throw an error instead of returning a non-sensical 0 filled string. OK florian@ --- diff --git a/libexec/snmpd/snmpd_metrics/mib.c b/libexec/snmpd/snmpd_metrics/mib.c index 2cda1dd0e4e..625f8791744 100644 --- a/libexec/snmpd/snmpd_metrics/mib.c +++ b/libexec/snmpd/snmpd_metrics/mib.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mib.c,v 1.8 2024/04/28 16:42:53 florian Exp $ */ +/* $OpenBSD: mib.c,v 1.9 2024/05/22 08:44:02 martijn Exp $ */ /* * Copyright (c) 2022 Martijn van Duren @@ -296,29 +296,31 @@ mib_hrsystemdate(struct agentx_varbind *vb) int tzoffset; unsigned short year; - memset(s, 0, sizeof(s)); (void)time(&now); ptm = localtime(&now); - if (ptm != NULL) { - year = htons(ptm->tm_year + 1900); - memcpy(s, &year, 2); - s[2] = ptm->tm_mon + 1; - s[3] = ptm->tm_mday; - s[4] = ptm->tm_hour; - s[5] = ptm->tm_min; - s[6] = ptm->tm_sec; - s[7] = 0; - - tzoffset = ptm->tm_gmtoff; - if (tzoffset < 0) - s[8] = '-'; - else - s[8] = '+'; - - s[9] = abs(tzoffset) / 3600; - s[10] = (abs(tzoffset) - (s[9] * 3600)) / 60; + if (ptm == NULL) { + log_warnx("localtime"); + agentx_varbind_error(vb); + return; } + year = htons(ptm->tm_year + 1900); + memcpy(s, &year, 2); + s[2] = ptm->tm_mon + 1; + s[3] = ptm->tm_mday; + s[4] = ptm->tm_hour; + s[5] = ptm->tm_min; + s[6] = ptm->tm_sec; + s[7] = 0; + + tzoffset = ptm->tm_gmtoff; + if (tzoffset < 0) + s[8] = '-'; + else + s[8] = '+'; + + s[9] = abs(tzoffset) / 3600; + s[10] = (abs(tzoffset) - (s[9] * 3600)) / 60; agentx_varbind_nstring(vb, s, sizeof(s)); }