utils: clear struct tm before calling strptime

Some strptime implementations clear the struct tm while others do not.
Since we want zeros for everything other than the year, month, day, hour,
and minute, we need to explicitly memset the struct.

This wasn't noticed before since illumos libc falls into the category of
zeroes-tm-for-you implementations.

On systems that do not zero struct tm automatically, various random values
end up in tm_sec which causes the subsequent mktime to return a value that
doesn't represent the passed in string's meaning.

Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
1 files changed, 4 insertions(+), 1 deletions(-)

M utils.c
M utils.c +4 -1
@@ 1,5 1,5 @@ 
 /*
- * Copyright (c) 2011-2018 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ * Copyright (c) 2011-2021 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal

          
@@ 149,6 149,9 @@ time_t parse_time_cstr(const char *str)
 	if (!str)
 		return 0;
 
+	/* some implementations of strptime don't reset values not parsed */
+	memset(&tm, 0, sizeof(struct tm));
+
 	strptime(str, "%Y-%m-%d %H:%M", &tm);
 
 	return mktime(&tm);