Bug 455556: Added a null pointer check of the 'tm' argument to

PR_FormatTime.  Added a test case for the output string when PR_FormatTime
fails.  r=julien.pierre.
Modified Files:
	src/misc/prtime.c tests/formattm.c


git-svn-id: svn://10.0.0.236/trunk@254717 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
wtc%google.com
2008-10-22 00:56:28 +00:00
parent 79c9f85505
commit 9babcc6b25
2 changed files with 50 additions and 19 deletions

View File

@@ -1698,29 +1698,37 @@ PR_FormatTime(char *buf, int buflen, const char *fmt, const PRExplodedTime *tm)
{
size_t rv;
struct tm a;
a.tm_sec = tm->tm_sec;
a.tm_min = tm->tm_min;
a.tm_hour = tm->tm_hour;
a.tm_mday = tm->tm_mday;
a.tm_mon = tm->tm_month;
a.tm_wday = tm->tm_wday;
a.tm_year = tm->tm_year - 1900;
a.tm_yday = tm->tm_yday;
a.tm_isdst = tm->tm_params.tp_dst_offset ? 1 : 0;
struct tm *ap;
/*
* On some platforms, for example SunOS 4, struct tm has two additional
* fields: tm_zone and tm_gmtoff.
*/
if (tm) {
ap = &a;
a.tm_sec = tm->tm_sec;
a.tm_min = tm->tm_min;
a.tm_hour = tm->tm_hour;
a.tm_mday = tm->tm_mday;
a.tm_mon = tm->tm_month;
a.tm_wday = tm->tm_wday;
a.tm_year = tm->tm_year - 1900;
a.tm_yday = tm->tm_yday;
a.tm_isdst = tm->tm_params.tp_dst_offset ? 1 : 0;
/*
* On some platforms, for example SunOS 4, struct tm has two
* additional fields: tm_zone and tm_gmtoff.
*/
#if defined(SUNOS4) || (__GLIBC__ >= 2) || defined(XP_BEOS) \
|| defined(NETBSD) || defined(OPENBSD) || defined(FREEBSD) \
|| defined(DARWIN) || defined(SYMBIAN)
a.tm_zone = NULL;
a.tm_gmtoff = tm->tm_params.tp_gmt_offset + tm->tm_params.tp_dst_offset;
a.tm_zone = NULL;
a.tm_gmtoff = tm->tm_params.tp_gmt_offset +
tm->tm_params.tp_dst_offset;
#endif
} else {
ap = NULL;
}
rv = strftime(buf, buflen, fmt, &a);
rv = strftime(buf, buflen, fmt, ap);
if (!rv && buf && buflen > 0) {
/*
* When strftime fails, the contents of buf are indeterminate.

View File

@@ -44,16 +44,39 @@
int main()
{
char buffer[256];
char small_buffer[8];
PRTime now;
PRExplodedTime tod;
now = PR_Now();
PR_ExplodeTime(now, PR_LocalTimeParameters, &tod);
(void)PR_FormatTime(buffer, sizeof(buffer),
"%a %b %d %H:%M:%S %Z %Y", &tod);
printf("%s\n", buffer);
if (PR_FormatTime(buffer, sizeof(buffer),
"%a %b %d %H:%M:%S %Z %Y", &tod) != 0) {
printf("%s\n", buffer);
} else {
fprintf(stderr, "PR_FormatTime(buffer) failed\n");
return 1;
}
small_buffer[0] = '?';
if (PR_FormatTime(small_buffer, sizeof(small_buffer),
"%a %b %d %H:%M:%S %Z %Y", &tod) == 0) {
if (small_buffer[0] != '\0') {
fprintf(stderr, "PR_FormatTime(small_buffer) did not output "
"an empty string on failure\n");
return 1;
}
printf("%s\n", small_buffer);
} else {
fprintf(stderr, "PR_FormatTime(small_buffer) succeeded "
"unexpectedly\n");
return 1;
}
(void)PR_FormatTimeUSEnglish(buffer, sizeof(buffer),
"%a %b %d %H:%M:%S %Z %Y", &tod);
printf("%s\n", buffer);
return 0;
}