time_t definition

Polytropon freebsd at edvax.de
Fri Jan 18 03:58:22 UTC 2013


On Thu, 17 Jan 2013 09:24:27 -0500, Lowell Gilbert wrote:
> Polytropon <freebsd at edvax.de> writes:
> 
> > On Wed, 16 Jan 2013 10:21:03 -0800, Michael Sierchio wrote:
> >> Top posting for brevity - the fact is, the code in your original
> >> example is wrong.  There are reasons to complain about argument size
> >> mismatches, esp. in print functions that call (versions of) malloc.
> >> You should cast the time_t value explicitly, or use %d instead of %ld.
> >
> > This advice looks correct. If you use the source Luke,
> > you'll find the following (taken from a 8.2-STABLE/i386
> > system source tree):
> >
> > /usr/src/sys/sys/types.h (line 253):
> >
> > 	typedef __time_t time_t;
> >
> > /usr/src/sys/i386/include/_types.h (line 97):
> >
> > 	typedef __int32_t __time_t;
> >
> > /usr/src/sys/i386/include/_types.h (line 55):
> >
> > 	typedef int __int32_t;
> >
> > So it boils down to (int), but %ld expects (long). This
> > is the exact content of the warning. You can either
> > case the (time_t) value to (long), or change %ld to %d
> > to avoid the warning.
> 
> Even if the representations boil down to the same thing, the cast is
> still a good idea. You may *know* (for example) that time_t is really an
> int, but you don't know that it always will be.

That's fully correct. The same way as input data should be
validated (instead of blindly trusted), types that are not
"the simple and obvious ones" can explicitely be cast into
the required form. The -W options for cc can help here.



> printf() (like other
> variadic functions) loses type information, so make *sure* you cast the
> type to what the format says it is, because the Usual Arithmetic
> Conversions cannot come in to save your bacon if (when) you're wrong.

Relying on what one knows about one specific architecture
should not be transitioned to "everywhere". This idea makes
code portable.

Especially the system's types that do not "sound like" what
one usually associates to the format descriptors, e. g. %d
is for (int), %ld is for (long), %c is for (char), should
be cast into the respective format-related type if the
variable accessed is of some "obscure" (time_t), (size_t)
or any other "non-obvious" type. And if you're "accidentally"
casting (int) to (int) - no problem, bacon saved. :-)



-- 
Polytropon
Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...


More information about the freebsd-questions mailing list