summaryrefslogtreecommitdiff
path: root/source3
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
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')
-rw-r--r--source3/auth/auth_sam.c21
-rw-r--r--source3/auth/pass_check.c10
-rw-r--r--source3/client/client.c26
-rw-r--r--source3/client/clitar.c2
-rw-r--r--source3/client/smbctool.c30
-rw-r--r--source3/lib/replace.c101
-rw-r--r--source3/lib/time.c38
-rw-r--r--source3/nmbd/nmbd_namelistdb.c20
-rw-r--r--source3/nmbd/nmbd_winsserver.c6
-rw-r--r--source3/printing/lpq_parse.c119
-rw-r--r--source3/smbd/lanman.c3
-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
-rw-r--r--source3/web/statuspage.c2
16 files changed, 276 insertions, 141 deletions
diff --git a/source3/auth/auth_sam.c b/source3/auth/auth_sam.c
index e8018eb13d..ec405dd2be 100644
--- a/source3/auth/auth_sam.c
+++ b/source3/auth/auth_sam.c
@@ -77,6 +77,7 @@ static BOOL logon_hours_ok(struct samu *sampass)
const uint8 *hours;
struct tm *utctime;
time_t lasttime;
+ const char *asct;
uint8 bitmask, bitpos;
hours = pdb_get_hours(sampass);
@@ -87,6 +88,11 @@ static BOOL logon_hours_ok(struct samu *sampass)
lasttime = time(NULL);
utctime = gmtime(&lasttime);
+ if (!utctime) {
+ DEBUG(1, ("logon_hours_ok: failed to get gmtime. Failing logon for user %s\n",
+ pdb_get_username(sampass) ));
+ return False;
+ }
/* find the corresponding byte and bit */
bitpos = (utctime->tm_wday * 24 + utctime->tm_hour) % 168;
@@ -94,15 +100,24 @@ static BOOL logon_hours_ok(struct samu *sampass)
if (! (hours[bitpos/8] & bitmask)) {
struct tm *t = localtime(&lasttime);
+ if (!t) {
+ asct = "INVALID TIME";
+ } else {
+ asct = asctime(t);
+ if (!asct) {
+ asct = "INVALID TIME";
+ }
+ }
+
DEBUG(1, ("logon_hours_ok: Account for user %s not allowed to "
"logon at this time (%s).\n",
- pdb_get_username(sampass),
- t ? asctime(t) : "INVALID TIME"));
+ pdb_get_username(sampass), asct ));
return False;
}
+ asct = asctime(utctime);
DEBUG(5,("logon_hours_ok: user %s allowed to logon at this time (%s)\n",
- pdb_get_username(sampass), asctime(utctime) ));
+ pdb_get_username(sampass), asct ? asct : "UNKNOWN TIME" ));
return True;
}
diff --git a/source3/auth/pass_check.c b/source3/auth/pass_check.c
index 507e8a3836..d0a900b80f 100644
--- a/source3/auth/pass_check.c
+++ b/source3/auth/pass_check.c
@@ -92,6 +92,7 @@ check on a DCE/DFS authentication
********************************************************************/
static BOOL dfs_auth(char *user, char *password)
{
+ struct tm *t;
error_status_t err;
int err2;
int prterr;
@@ -341,8 +342,13 @@ static BOOL dfs_auth(char *user, char *password)
set_effective_uid(0);
set_effective_gid(0);
- DEBUG(0,
- ("DCE context expires: %s", asctime(localtime(&expire_time))));
+ t = localtime(&expire_time);
+ if (t) {
+ const char *asct = asctime(t);
+ if (asct) {
+ DEBUG(0,("DCE context expires: %s", asct));
+ }
+ }
dcelogin_atmost_once = 1;
return (True);
diff --git a/source3/client/client.c b/source3/client/client.c
index 1fbee70645..aa25d9aba5 100644
--- a/source3/client/client.c
+++ b/source3/client/client.c
@@ -379,7 +379,7 @@ static void display_finfo(file_info *finfo)
finfo->name,
attrib_string(finfo->mode),
(double)finfo->size,
- asctime(localtime(&t)));
+ time_to_asc(&t));
dir_total += finfo->size;
}
}
@@ -2111,6 +2111,7 @@ static int cmd_stat(void)
fstring mode_str;
SMB_STRUCT_STAT sbuf;
struct cli_state *targetcli;
+ struct tm *lt;
pstring targetname;
if (!SERVER_HAS_UNIX_CIFS(cli)) {
@@ -2165,13 +2166,28 @@ static int cmd_stat(void)
(unsigned int)sbuf.st_uid,
(unsigned int)sbuf.st_gid);
- strftime(mode_str, sizeof(mode_str), "%F %T %z", localtime(&sbuf.st_atime));
+ lt = localtime(&sbuf.st_atime);
+ if (lt) {
+ strftime(mode_str, sizeof(mode_str), "%F %T %z", lt);
+ } else {
+ fstrcpy(mode_str, "unknown");
+ }
d_printf("Access: %s\n", mode_str);
- strftime(mode_str, sizeof(mode_str), "%F %T %z", localtime(&sbuf.st_mtime));
+ lt = localtime(&sbuf.st_mtime);
+ if (lt) {
+ strftime(mode_str, sizeof(mode_str), "%F %T %z", lt);
+ } else {
+ fstrcpy(mode_str, "unknown");
+ }
d_printf("Modify: %s\n", mode_str);
- strftime(mode_str, sizeof(mode_str), "%F %T %z", localtime(&sbuf.st_ctime));
+ lt = localtime(&sbuf.st_ctime);
+ if (lt) {
+ strftime(mode_str, sizeof(mode_str), "%F %T %z", lt);
+ } else {
+ fstrcpy(mode_str, "unknown");
+ }
d_printf("Change: %s\n", mode_str);
return 0;
@@ -2339,7 +2355,7 @@ static int cmd_newer(void)
if (ok && (sys_stat(buf,&sbuf) == 0)) {
newer_than = sbuf.st_mtime;
DEBUG(1,("Getting files newer than %s",
- asctime(localtime(&newer_than))));
+ time_to_asc(&newer_than)));
} else {
newer_than = 0;
}
diff --git a/source3/client/clitar.c b/source3/client/clitar.c
index a7bc4bfde3..14c28acfc5 100644
--- a/source3/client/clitar.c
+++ b/source3/client/clitar.c
@@ -1641,7 +1641,7 @@ int tar_parseargs(int argc, char *argv[], const char *Optarg, int Optind)
if (sys_stat(argv[Optind], &stbuf) == 0) {
newer_than = stbuf.st_mtime;
DEBUG(1,("Getting files newer than %s",
- asctime(localtime(&newer_than))));
+ time_to_asc(&newer_than)));
newOptind++;
Optind++;
} else {
diff --git a/source3/client/smbctool.c b/source3/client/smbctool.c
index ba095aee84..5b21d195b7 100644
--- a/source3/client/smbctool.c
+++ b/source3/client/smbctool.c
@@ -445,7 +445,7 @@ static void display_finfo(file_info *finfo)
finfo->name,
attrib_string(finfo->mode),
(double)finfo->size,
- asctime(localtime(&t)));
+ time_to_asc(&t));
dir_total += finfo->size;
}
}
@@ -458,7 +458,7 @@ static void display_stat(char *name, struct stat *st)
{
time_t t = st->st_mtime;
pstring time_str;
- pstrcpy(time_str, asctime(localtime(&t)));
+ pstrcpy(time_str, time_to_asc(&t));
time_str[strlen(time_str)-1] = 0;
d_printf("> %-30s", name);
d_printf("%10.10s %8.0f %s\n", *mode_t_string(st->st_mode), (double)st->st_size, time_str);
@@ -2303,6 +2303,7 @@ static int cmd_stat(void)
fstring mode_str;
SMB_STRUCT_STAT sbuf;
struct cli_state *targetcli;
+ struct tm *lt;
pstring targetname;
if (!SERVER_HAS_UNIX_CIFS(cli)) {
@@ -2357,15 +2358,30 @@ static int cmd_stat(void)
(unsigned int)sbuf.st_uid,
(unsigned int)sbuf.st_gid);
- strftime(mode_str, sizeof(mode_str), "%F %T %z", localtime(&sbuf.st_atime));
+ lt = localtime(&sbuf.st_atime);
+ if (lt) {
+ strftime(mode_str, sizeof(mode_str), "%F %T %z", lt);
+ } else {
+ fstrcpy(mode_str, "unknown");
+ }
d_printf("Access: %s\n", mode_str);
- strftime(mode_str, sizeof(mode_str), "%F %T %z", localtime(&sbuf.st_mtime));
+ lt = localtime(&sbuf.st_mtime);
+ if (lt) {
+ strftime(mode_str, sizeof(mode_str), "%F %T %z", lt);
+ } else {
+ fstrcpy(mode_str, "unknown");
+ }
d_printf("Modify: %s\n", mode_str);
- strftime(mode_str, sizeof(mode_str), "%F %T %z", localtime(&sbuf.st_ctime));
+ lt = localtime(&sbuf.st_ctime);
+ if (lt) {
+ strftime(mode_str, sizeof(mode_str), "%F %T %z", lt);
+ } else {
+ fstrcpy(mode_str, "unknown");
+ }
d_printf("Change: %s\n", mode_str);
-
+
return 0;
}
@@ -2538,7 +2554,7 @@ static int cmd_newer(void)
if (ok && (sys_stat(buf,&sbuf) == 0)) {
newer_than = sbuf.st_mtime;
DEBUG(1,("Getting files newer than %s",
- asctime(localtime(&newer_than))));
+ time_to_asc(&newer_than)));
} else {
newer_than = 0;
}
diff --git a/source3/lib/replace.c b/source3/lib/replace.c
index 9ef3503d39..19b37af938 100644
--- a/source3/lib/replace.c
+++ b/source3/lib/replace.c
@@ -95,49 +95,50 @@ Corrections by richard.kettlewell@kewill.com
#define YEAR 365*DAY
time_t mktime(struct tm *t)
{
- struct tm *u;
- time_t epoch = 0;
- int n;
- int mon [] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
- y, m, i;
-
- if(t->tm_year < 70)
- return((time_t)-1);
-
- n = t->tm_year + 1900 - 1;
- epoch = (t->tm_year - 70) * YEAR +
- ((n / 4 - n / 100 + n / 400) - (1969 / 4 - 1969 / 100 + 1969 / 400)) * DAY;
-
- y = t->tm_year + 1900;
- m = 0;
-
- for(i = 0; i < t->tm_mon; i++) {
- epoch += mon [m] * DAY;
- if(m == 1 && y % 4 == 0 && (y % 100 != 0 || y % 400 == 0))
- epoch += DAY;
+ struct tm *u;
+ time_t epoch = 0;
+ int n;
+ int mon [] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
+ y, m, i;
+
+ if(t->tm_year < 70) {
+ return((time_t)-1);
+ }
+
+ n = t->tm_year + 1900 - 1;
+ epoch = (t->tm_year - 70) * YEAR + ((n / 4 - n / 100 + n / 400) - (1969 / 4 - 1969 / 100 + 1969 / 400)) * DAY;
+
+ y = t->tm_year + 1900;
+ m = 0;
+
+ for(i = 0; i < t->tm_mon; i++) {
+ epoch += mon [m] * DAY;
+ if(m == 1 && y % 4 == 0 && (y % 100 != 0 || y % 400 == 0)) {
+ epoch += DAY;
+ }
- if(++m > 11) {
- m = 0;
- y++;
- }
- }
-
- epoch += (t->tm_mday - 1) * DAY;
- epoch += t->tm_hour * HOUR + t->tm_min * MINUTE + t->tm_sec;
+ if(++m > 11) {
+ m = 0;
+ y++;
+ }
+ }
+
+ epoch += (t->tm_mday - 1) * DAY;
+ epoch += t->tm_hour * HOUR + t->tm_min * MINUTE + t->tm_sec;
- if((u = localtime(&epoch)) != NULL) {
- t->tm_sec = u->tm_sec;
- t->tm_min = u->tm_min;
- t->tm_hour = u->tm_hour;
- t->tm_mday = u->tm_mday;
- t->tm_mon = u->tm_mon;
- t->tm_year = u->tm_year;
- t->tm_wday = u->tm_wday;
- t->tm_yday = u->tm_yday;
- t->tm_isdst = u->tm_isdst;
- }
-
- return(epoch);
+ if((u = localtime(&epoch)) != NULL) {
+ t->tm_sec = u->tm_sec;
+ t->tm_min = u->tm_min;
+ t->tm_hour = u->tm_hour;
+ t->tm_mday = u->tm_mday;
+ t->tm_mon = u->tm_mon;
+ t->tm_year = u->tm_year;
+ t->tm_wday = u->tm_wday;
+ t->tm_yday = u->tm_yday;
+ t->tm_isdst = u->tm_isdst;
+ }
+
+ return(epoch);
}
#endif /* !HAVE_MKTIME */
@@ -147,15 +148,15 @@ Corrections by richard.kettlewell@kewill.com
/* Rename a file. (from libiberty in GNU binutils) */
int rename(const char *zfrom, const char *zto)
{
- if (link (zfrom, zto) < 0)
- {
- if (errno != EEXIST)
- return -1;
- if (unlink (zto) < 0
- || link (zfrom, zto) < 0)
- return -1;
- }
- return unlink (zfrom);
+ if (link (zfrom, zto) < 0) {
+ if (errno != EEXIST) {
+ return -1;
+ }
+ if (unlink (zto) < 0 || link (zfrom, zto) < 0) {
+ return -1;
+ }
+ }
+ return unlink (zfrom);
}
#endif /* HAVE_RENAME */
diff --git a/source3/lib/time.c b/source3/lib/time.c
index f8a1538910..9a539d415e 100644
--- a/source3/lib/time.c
+++ b/source3/lib/time.c
@@ -615,16 +615,19 @@ char *http_timestring(time_t t)
static fstring buf;
struct tm *tm = localtime(&t);
- if (!tm)
+ if (!tm) {
slprintf(buf,sizeof(buf)-1,"%ld seconds since the Epoch",(long)t);
- else
+ } else {
#ifndef HAVE_STRFTIME
- fstrcpy(buf, asctime(tm));
- if(buf[strlen(buf)-1] == '\n')
+ const char *asct = asctime(tm);
+ fstrcpy(buf, asct ? asct : "unknown");
+ }
+ if(buf[strlen(buf)-1] == '\n') {
buf[strlen(buf)-1] = 0;
#else /* !HAVE_STRFTIME */
strftime(buf, sizeof(buf)-1, "%a, %d %b %Y %H:%M:%S %Z", tm);
#endif /* !HAVE_STRFTIME */
+ }
return buf;
}
@@ -672,13 +675,15 @@ char *timestring(BOOL hires)
}
#else
if (hires) {
+ const char *asct = asctime(tm);
slprintf(TimeBuf,
sizeof(TimeBuf)-1,
"%s.%06ld",
- asctime(tm),
+ asct ? asct : "unknown",
(long)tp.tv_usec);
} else {
- fstrcpy(TimeBuf, asctime(tm));
+ const char *asct = asctime(tm);
+ fstrcpy(TimeBuf, asct ? asct : "unknown");
}
#endif
}
@@ -1050,3 +1055,24 @@ struct timespec get_create_timespec(SMB_STRUCT_STAT *st,BOOL fake_dirs)
return ret;
}
#endif
+
+/****************************************************************************
+ Utility function that always returns a const string even if localtime
+ and asctime fail.
+****************************************************************************/
+
+const char *time_to_asc(const time_t *t)
+{
+ const char *asct;
+ struct tm *lt = localtime(t);
+
+ if (!lt) {
+ return "unknown time";
+ }
+
+ asct = asctime(lt);
+ if (!asct) {
+ return "unknown time";
+ }
+ return asct;
+}
diff --git a/source3/nmbd/nmbd_namelistdb.c b/source3/nmbd/nmbd_namelistdb.c
index 60023a7ed5..fb32ce1aad 100644
--- a/source3/nmbd/nmbd_namelistdb.c
+++ b/source3/nmbd/nmbd_namelistdb.c
@@ -564,15 +564,31 @@ void dump_name_record( struct name_record *namerec, XFILE *fp)
x_fprintf(fp,"Source = %s\nb_flags = %x\t", src_type, namerec->data.nb_flags);
if(namerec->data.death_time != PERMANENT_TTL) {
+ const char *asct;
tm = localtime(&namerec->data.death_time);
- x_fprintf(fp, "death_time = %s\t", asctime(tm));
+ if (!tm) {
+ return;
+ }
+ asct = asctime(tm);
+ if (!asct) {
+ return;
+ }
+ x_fprintf(fp, "death_time = %s\t", asct);
} else {
x_fprintf(fp, "death_time = PERMANENT\t");
}
if(namerec->data.refresh_time != PERMANENT_TTL) {
+ const char *asct;
tm = localtime(&namerec->data.refresh_time);
- x_fprintf(fp, "refresh_time = %s\n", asctime(tm));
+ if (!tm) {
+ return;
+ }
+ asct = asctime(tm);
+ if (!asct) {
+ return;
+ }
+ x_fprintf(fp, "refresh_time = %s\n", asct);
} else {
x_fprintf(fp, "refresh_time = PERMANENT\n");
}
diff --git a/source3/nmbd/nmbd_winsserver.c b/source3/nmbd/nmbd_winsserver.c
index 198d90f35a..29d5c41de8 100644
--- a/source3/nmbd/nmbd_winsserver.c
+++ b/source3/nmbd/nmbd_winsserver.c
@@ -2222,7 +2222,13 @@ void wins_write_name_record(struct name_record *namerec, XFILE *fp)
char *ts, *nl;
tm = localtime(&namerec->data.death_time);
+ if (!tm) {
+ return;
+ }
ts = asctime(tm);
+ if (!ts) {
+ return;
+ }
nl = strrchr( ts, '\n' );
if( NULL != nl ) {
*nl = '\0';
diff --git a/source3/printing/lpq_parse.c b/source3/printing/lpq_parse.c
index 7e81b5187c..324d0fa90d 100644
--- a/source3/printing/lpq_parse.c
+++ b/source3/printing/lpq_parse.c
@@ -25,50 +25,62 @@ static const char *Months[13] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
/*******************************************************************
-process time fields
+ Process time fields
********************************************************************/
+
static time_t EntryTime(fstring tok[], int ptr, int count, int minimum)
{
- time_t jobtime,jobtime1;
-
- jobtime = time(NULL); /* default case: take current time */
- if (count >= minimum) {
- struct tm *t;
- int i, day, hour, min, sec;
- char *c;
-
- for (i=0; i<13; i++) if (!strncmp(tok[ptr], Months[i],3)) break; /* Find month */
- if (i<12) {
- t = localtime(&jobtime);
- day = atoi(tok[ptr+1]);
- c=(char *)(tok[ptr+2]);
- *(c+2)=0;
- hour = atoi(c);
- *(c+5)=0;
- min = atoi(c+3);
- if(*(c+6) != 0)sec = atoi(c+6);
- else sec=0;
-
- if ((t->tm_mon < i)||
- ((t->tm_mon == i)&&
- ((t->tm_mday < day)||
- ((t->tm_mday == day)&&
- (t->tm_hour*60+t->tm_min < hour*60+min)))))
- t->tm_year--; /* last year's print job */
-
- t->tm_mon = i;
- t->tm_mday = day;
- t->tm_hour = hour;
- t->tm_min = min;
- t->tm_sec = sec;
- jobtime1 = mktime(t);
- if (jobtime1 != (time_t)-1)
- jobtime = jobtime1;
- }
- }
- return jobtime;
-}
+ time_t jobtime,jobtime1;
+
+ jobtime = time(NULL); /* default case: take current time */
+ if (count >= minimum) {
+ struct tm *t;
+ int i, day, hour, min, sec;
+ char *c;
+
+ for (i=0; i<13; i++) {
+ if (!strncmp(tok[ptr], Months[i],3)) {
+ break; /* Find month */
+ }
+ }
+ if (i<12) {
+ t = localtime(&jobtime);
+ if (!t) {
+ return (time_t)-1;
+ }
+ day = atoi(tok[ptr+1]);
+ c=(char *)(tok[ptr+2]);
+ *(c+2)=0;
+ hour = atoi(c);
+ *(c+5)=0;
+ min = atoi(c+3);
+ if(*(c+6) != 0) {
+ sec = atoi(c+6);
+ } else {
+ sec=0;
+ }
+
+ if ((t->tm_mon < i)|| ((t->tm_mon == i)&&
+ ((t->tm_mday < day)||
+ ((t->tm_mday == day)&&
+ (t->tm_hour*60+t->tm_min < hour*60+min))))) {
+ t->tm_year--; /* last year's print job */
+ }
+
+ t->tm_mon = i;
+ t->tm_mday = day;
+ t->tm_hour = hour;
+ t->tm_min = min;
+ t->tm_sec = sec;
+ jobtime1 = mktime(t);
+ if (jobtime1 != (time_t)-1) {
+ jobtime = jobtime1;
+ }
+ }
+ }
+ return jobtime;
+}
/****************************************************************************
parse a lpq line
@@ -190,24 +202,27 @@ With lprng 3.16 The lpq time looks like
static time_t LPRng_time(char *time_string)
{
time_t jobtime;
- struct tm t;
+ struct tm *t;
jobtime = time(NULL); /* default case: take current time */
- t = *localtime(&jobtime);
+ t = localtime(&jobtime);
+ if (!t) {
+ return (time_t)-1;
+ }
if ( atoi(time_string) < 24 ){
- t.tm_hour = atoi(time_string);
- t.tm_min = atoi(time_string+3);
- t.tm_sec = atoi(time_string+6);
+ t->tm_hour = atoi(time_string);
+ t->tm_min = atoi(time_string+3);
+ t->tm_sec = atoi(time_string+6);
} else {
- t.tm_year = atoi(time_string)-1900;
- t.tm_mon = atoi(time_string+5)-1;
- t.tm_mday = atoi(time_string+8);
- t.tm_hour = atoi(time_string+11);
- t.tm_min = atoi(time_string+14);
- t.tm_sec = atoi(time_string+17);
+ t->tm_year = atoi(time_string)-1900;
+ t->tm_mon = atoi(time_string+5)-1;
+ t->tm_mday = atoi(time_string+8);
+ t->tm_hour = atoi(time_string+11);
+ t->tm_min = atoi(time_string+14);
+ t->tm_sec = atoi(time_string+17);
}
- jobtime = mktime(&t);
+ jobtime = mktime(t);
return jobtime;
}
diff --git a/source3/smbd/lanman.c b/source3/smbd/lanman.c
index 4d4d9d2259..7d1a2ade54 100644
--- a/source3/smbd/lanman.c
+++ b/source3/smbd/lanman.c
@@ -2212,6 +2212,9 @@ static BOOL api_NetRemoteTOD(connection_struct *conn,uint16 vuid, char *param,ch
/* the client expects to get localtime, not GMT, in this bit
(I think, this needs testing) */
t = localtime(&unixdate);
+ if (!t) {
+ return False;
+ }
SIVAL(p,4,0); /* msecs ? */
SCVAL(p,8,t->tm_hour);
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;
}
diff --git a/source3/web/statuspage.c b/source3/web/statuspage.c
index 7430f4ebf5..769ab217b3 100644
--- a/source3/web/statuspage.c
+++ b/source3/web/statuspage.c
@@ -101,7 +101,7 @@ static char *mapPid2Machine (struct process_id pid)
static char *tstring(time_t t)
{
static pstring buf;
- pstrcpy(buf, asctime(localtime(&t)));
+ pstrcpy(buf, time_to_asc(&t));
all_string_sub(buf," ","&nbsp;",sizeof(buf));
return buf;
}