summaryrefslogtreecommitdiff
path: root/source3/utils
diff options
context:
space:
mode:
authorJeremy Allison <jra@samba.org>2006-06-14 21:36:49 +0000
committerGerald (Jerry) Carter <jerry@samba.org>2007-10-10 11:17:26 -0500
commita1e0a0e9286fbe90ca04cda9df38e72d8d18b0c1 (patch)
tree14c15c8333572c5a2e089611ee61d03010a9d2b4 /source3/utils
parent2b99951e7511d0ec2d928c06b05fe22b7b6572d1 (diff)
downloadsamba-a1e0a0e9286fbe90ca04cda9df38e72d8d18b0c1.tar.gz
samba-a1e0a0e9286fbe90ca04cda9df38e72d8d18b0c1.tar.bz2
samba-a1e0a0e9286fbe90ca04cda9df38e72d8d18b0c1.zip
r16230: Fix Klocwork #861 and others. localtime and asctime
can return NULL. Ensure we check all returns correctly. Jeremy. (This used to be commit 6c61dc8ed6d84f310ef391fb7700e93ef42c4afc)
Diffstat (limited to 'source3/utils')
-rw-r--r--source3/utils/net_cache.c26
-rw-r--r--source3/utils/net_status.c4
-rw-r--r--source3/utils/net_time.c5
-rw-r--r--source3/utils/status.c4
4 files changed, 27 insertions, 12 deletions
diff --git a/source3/utils/net_cache.c b/source3/utils/net_cache.c
index 0c107a5f01..69b3327f7a 100644
--- a/source3/utils/net_cache.c
+++ b/source3/utils/net_cache.c
@@ -37,24 +37,36 @@
static void print_cache_entry(const char* keystr, const char* datastr,
const time_t timeout, void* dptr)
{
- char* timeout_str;
+ char *timeout_str;
time_t now_t = time(NULL);
struct tm timeout_tm, *now_tm;
/* localtime returns statically allocated pointer, so timeout_tm
has to be copied somewhere else */
- memcpy(&timeout_tm, localtime(&timeout), sizeof(struct tm));
+
+ now_tm = localtime(&timeout);
+ if (!now_tm) {
+ return;
+ }
+ memcpy(&timeout_tm, now_tm, sizeof(struct tm));
now_tm = localtime(&now_t);
+ if (!now_tm) {
+ return;
+ }
/* form up timeout string depending whether it's today's date or not */
if (timeout_tm.tm_year != now_tm->tm_year ||
- timeout_tm.tm_mon != now_tm->tm_mon ||
- timeout_tm.tm_mday != now_tm->tm_mday) {
+ timeout_tm.tm_mon != now_tm->tm_mon ||
+ timeout_tm.tm_mday != now_tm->tm_mday) {
- timeout_str = asctime(&timeout_tm);
- timeout_str[strlen(timeout_str) - 1] = '\0'; /* remove tailing CR */
- } else
+ timeout_str = asctime(&timeout_tm);
+ if (!timeout_str) {
+ return;
+ }
+ timeout_str[strlen(timeout_str) - 1] = '\0'; /* remove tailing CR */
+ } else {
asprintf(&timeout_str, "%.2d:%.2d:%.2d", timeout_tm.tm_hour,
timeout_tm.tm_min, timeout_tm.tm_sec);
+ }
d_printf("Key: %s\t Timeout: %s\t Value: %s %s\n", keystr,
timeout_str, datastr, timeout > now_t ? "": "(expired)");
diff --git a/source3/utils/net_status.c b/source3/utils/net_status.c
index d85bd27b16..c68c9f6e2f 100644
--- a/source3/utils/net_status.c
+++ b/source3/utils/net_status.c
@@ -104,7 +104,7 @@ static int show_share(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
d_printf("%-10.10s %s %-12s %s",
crec.name,procid_str_static(&crec.pid),
crec.machine,
- asctime(localtime(&crec.start)));
+ time_to_asc(&crec.start));
return 0;
}
@@ -173,7 +173,7 @@ static int show_share_parseable(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
guest ? "" : gidtoname(ids->entries[i].gid),
crec.machine,
guest ? "" : ids->entries[i].hostname,
- asctime(localtime(&crec.start)));
+ time_to_asc(&crec.start));
return 0;
}
diff --git a/source3/utils/net_time.c b/source3/utils/net_time.c
index 1a7116d447..f6486286a6 100644
--- a/source3/utils/net_time.c
+++ b/source3/utils/net_time.c
@@ -69,12 +69,15 @@ static time_t nettime(int *zone)
}
/* return a time as a string ready to be passed to /bin/date */
-static char *systime(time_t t)
+static const char *systime(time_t t)
{
static fstring s;
struct tm *tm;
tm = localtime(&t);
+ if (!tm) {
+ return "unknown";
+ }
fstr_sprintf(s, "%02d%02d%02d%02d%04d.%02d",
tm->tm_mon+1, tm->tm_mday, tm->tm_hour,
diff --git a/source3/utils/status.c b/source3/utils/status.c
index 05075da444..2566c8a50d 100644
--- a/source3/utils/status.c
+++ b/source3/utils/status.c
@@ -155,7 +155,7 @@ static void print_share_mode(const struct share_mode_entry *e, const char *share
d_printf("NONE ");
}
- d_printf(" %s %s %s",sharepath, fname, asctime(localtime((time_t *)&e->time.tv_sec)));
+ d_printf(" %s %s %s",sharepath, fname, time_to_asc((time_t *)&e->time.tv_sec));
}
}
@@ -562,7 +562,7 @@ static int traverse_fn1(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *st
d_printf("%-10s %s %-12s %s",
crec.name,procid_str_static(&crec.pid),
crec.machine,
- asctime(localtime(&crec.start)));
+ time_to_asc(&crec.start));
return 0;
}