A
Andre Majorel
How do you compute an off_t with overflow detection ?
Ideally, the target language is C89/C90 and the target platform
is reasonably recent versions of the major Unixen. If there is
no practical way to do that without limiting the target platform
set to FreeBSD + Linux + NetBSD + OpenBSD or adding the
requirement of conformance to some combination of SUS v2, SUS v3
and C99, I'll settle for that.
Overflow-safe versions of + and * would do but not even a
relatively recent standard like SUS v2 provides OFF_MIN and
OFF_MAX and since off_t is a signed integer type, overflows lead
to undefined behaviour.
What are we supposed to do ? Compare sizeof (off_t) with sizeof
(int/long/long long[1]) and use INT_MAX/LONG_MAX/LLONG_MAX[1] ?
Carry the calculation with doubles, cast to off_t and cast back
to double for verification[2] ? Use bignum ?
[1] Neither of which is not in C89 or SUS v2, by the way.
[2] But is the result of casting a double outside of
INT_MIN..INT_MAX to int defined ?
Ideally, the target language is C89/C90 and the target platform
is reasonably recent versions of the major Unixen. If there is
no practical way to do that without limiting the target platform
set to FreeBSD + Linux + NetBSD + OpenBSD or adding the
requirement of conformance to some combination of SUS v2, SUS v3
and C99, I'll settle for that.
Overflow-safe versions of + and * would do but not even a
relatively recent standard like SUS v2 provides OFF_MIN and
OFF_MAX and since off_t is a signed integer type, overflows lead
to undefined behaviour.
What are we supposed to do ? Compare sizeof (off_t) with sizeof
(int/long/long long[1]) and use INT_MAX/LONG_MAX/LLONG_MAX[1] ?
Carry the calculation with doubles, cast to off_t and cast back
to double for verification[2] ? Use bignum ?
[1] Neither of which is not in C89 or SUS v2, by the way.
[2] But is the result of casting a double outside of
INT_MIN..INT_MAX to int defined ?