tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: strftime(3) oddities with %s, %z



> Suppose you create a struct tm _without_ gmtime(3) or localtime(3),
> using designated initializers or memset for zero-initialization, with
> only what is included in POSIX:

> struct tm tm = {
> 	.tm_sec = 56,
> 	.tm_min = 34,
> 	.tm_hour = 12,
> 	.tm_mday = 1,
> 	.tm_mon = 12 - 1,	/* December */
> 	.tm_year = 2021 - 1900,
> 	.tm_wday = 3,		/* Wednesday */
> 	.tm_yday = 334,		/* zero-based day of year (%j - 1) */
> 	.tm_isdst = 0,
> };

This is fine.  But using memset is not; if struct tm contains a pointer
or a floating-point value, setting it to all-0-bits may produce a trap
representation - or, possibly worse, a valid value that means something
different from what you intend.

Unless POSIX was stupid enough to mandate that all-bits-0 is nil for
any pointer type and something well-defined for floating-point.  (I'd
be surprised by that, but standards bodies have surprised me often
enough in the past.)  Certainly C doesn't, at least not as of C99 - I
don't have a copy of anything newer.

/~\ The ASCII				  Mouse
\ / Ribbon Campaign
 X  Against HTML		mouse%rodents-montreal.org@localhost
/ \ Email!	     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B


Home | Main Index | Thread Index | Old Index