]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
getdate+nts+rtc: avoid some coverity false positives
authorMiroslav Lichvar <mlichvar@redhat.com>
Wed, 21 May 2025 06:43:12 +0000 (08:43 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Wed, 21 May 2025 10:41:13 +0000 (12:41 +0200)
Modify the code to avoid making the following calls incorrectly reported
as important findings by the coverity static analyzer:

- memset() of size 0 at the end of an array
- mktime() on a struct tm that has uninitialized tm_yday

getdate.y
nts_ke_server.c
rtc_linux.c

index c305fb7c66d39dbe5046780902c7a1cd30671929..8fd56879dba65fcba289439f7c6bbf6d4fb44850 100644 (file)
--- a/getdate.y
+++ b/getdate.y
@@ -907,6 +907,7 @@ get_date (const char *p, const time_t *now)
   yyHour = tmp->tm_hour;
   yyMinutes = tmp->tm_min;
   yySeconds = tmp->tm_sec;
+  memset(&tm, 0, sizeof (tm));
   tm.tm_isdst = tmp->tm_isdst;
   yyMeridian = MER24;
   yyRelSeconds = 0;
@@ -943,7 +944,6 @@ get_date (const char *p, const time_t *now)
   tm.tm_hour += yyRelHour;
   tm.tm_min += yyRelMinutes;
   tm.tm_sec += yyRelSeconds;
-  tm.tm_wday = 0;
 
   /* Let mktime deduce tm_isdst if we have an absolute timestamp,
      or if the relative timestamp mentions days, months, or years.  */
index 26cc0cf04ee8d59309db478ba5732a44bf98f775..33aef0820c421dbc7cc7d7be33bd48bf5ae4e36b 100644 (file)
@@ -536,7 +536,8 @@ generate_key(int index)
   BRIEF_ASSERT(key_length <= sizeof (key->key));
 
   UTI_GetRandomBytesUrandom(key->key, key_length);
-  memset(key->key + key_length, 0, sizeof (key->key) - key_length);
+  if (key_length < sizeof (key->key))
+    memset(key->key + key_length, 0, sizeof (key->key) - key_length);
   UTI_GetRandomBytes(&key->id, sizeof (key->id));
 
   /* Encode the index in the lowest bits of the ID */
index 31f495b3d9d94a944fbc447be4172cd17a570f56..d15c78f98422546f848f5a597e2a9bc1e218f212 100644 (file)
@@ -359,13 +359,13 @@ t_from_rtc(struct rtc_time *rtc_raw, int utc)
   time_t t1, t2;
 
   /* Convert to seconds since 1970 */
+  memset(&rtc_tm, 0, sizeof (rtc_tm));
   rtc_tm.tm_sec = rtc_raw->tm_sec;
   rtc_tm.tm_min = rtc_raw->tm_min;
   rtc_tm.tm_hour = rtc_raw->tm_hour;
   rtc_tm.tm_mday = rtc_raw->tm_mday;
   rtc_tm.tm_mon = rtc_raw->tm_mon;
   rtc_tm.tm_year = rtc_raw->tm_year;
-  rtc_tm.tm_wday = 0;
 
   temp1 = rtc_tm;
   temp1.tm_isdst = 0;