diff options
author | wtc%google.com <devnull@localhost> | 2008-10-22 00:56:28 +0000 |
---|---|---|
committer | wtc%google.com <devnull@localhost> | 2008-10-22 00:56:28 +0000 |
commit | 05a0799f98318ecbfad406b8e49cbc2b6c1ffeec (patch) | |
tree | f3aff1ea9ef8cf6241af8adfec93cf1bafec8fe9 | |
parent | f297bb4c3418c890b6b131b3d9de068c12fc5a7f (diff) | |
download | nspr-hg-05a0799f98318ecbfad406b8e49cbc2b6c1ffeec.tar.gz |
Bug 455556: Added a null pointer check of the 'tm' argument toNSPR_4_7_2_RTM
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
-rw-r--r-- | pr/src/misc/prtime.c | 40 | ||||
-rw-r--r-- | pr/tests/formattm.c | 29 |
2 files changed, 50 insertions, 19 deletions
diff --git a/pr/src/misc/prtime.c b/pr/src/misc/prtime.c index d73b08cd..5f05d5ef 100644 --- a/pr/src/misc/prtime.c +++ b/pr/src/misc/prtime.c @@ -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; + + 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. - */ + /* + * 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. diff --git a/pr/tests/formattm.c b/pr/tests/formattm.c index c3c758b5..8b04cd37 100644 --- a/pr/tests/formattm.c +++ b/pr/tests/formattm.c @@ -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; } |