summaryrefslogtreecommitdiff
path: root/source3/lib
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/lib
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/lib')
-rw-r--r--source3/lib/replace.c101
-rw-r--r--source3/lib/time.c38
2 files changed, 83 insertions, 56 deletions
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;
+}