On Thu, Nov 12, 2009 at 10:46 AM, Mark Bucciarelli <[email protected]> wrote:
> How can I tell if casting a off_t (e.g, sb.st_size) to size_t risks
> an overflow?
>
> What about casting ptrdiff_to to a uint64_t or a long?
>
> The full table of all such possible integer type casts must be large
> (and full of dragons?).  How do you all keep it straight?

The easy way: different types are different.  Don't convert between them.

The harder way: Why are you casting an off_t to a size_t?  They are
used for measuring different things.  Sounds like you are taking the
naive approach (I'll just allocate as much memory as the file is big).
 This is silly for more reasons than overflow alone, because even if
it doesn't overflow, not everybody has 20 gigabytes of memory.  So
don't do it.

The answer you probably wanted, but you shouldn't use: In OpenBSD, the
rules are somewhat simple, but not all systems follow these rules.
Again, if you have to care, you're probably making a mistake.

long long == off_t == int64_t == 64 bits

long == size_t == ptrdiff_t == void *

int == int32_t == 32 bits


> Can someone recommend a good mailng list for
> these kinds of generic *nix questions, so I don't have to
> abuse the generous hackers here on misc.

comp.unix.programmer.  There's also comp.lang.c, but they are rather
hostile to questions like this, precisely because the answers are
nonportable.

Reply via email to