On Sat, Feb 6, 2010 at 11:49 AM, Arthur David Olson
<olsona@elsie.nci.nih.gov> wrote:
Here are the proposed changes, revised to incorporate KRE's suggestion
on how to handle NULLs passed to asctime_r.
--ado
------- asctime.c -------
*** /tmp/geta27327 Sat Feb 6 14:46:35 2010
--- /tmp/getb27327 Sat Feb 6 14:46:35 2010
[...]
***************
*** 91,96 ****
--- 91,101 ----
char year[INT_STRLEN_MAXIMUM(int) + 2];
char result[MAX_ASCTIME_BUF_SIZE];
+ if (timeptr == NULL) {
+ errno = EINVAL;
+ (void) strcpy(buf, "??? ??? ?? ??:??:?? ????\n");
+ return buf;
+ }
if (timeptr->tm_wday < 0 || timeptr->tm_wday >= DAYSPERWEEK)
wn = "???";
else wn = wday_name[timeptr->tm_wday];
You could avoid the cast by using:
return strcpy(buf, "??? ??? ?? ??:??:?? ????\n");
This is one of the few occasions when the return value from strcpy() actually is useful. (So often, it would be more useful if it returned a pointer to the NUL '\0' at the end of the string.)