Is This Sailing A Little Close To The Wind?
From
Lawrence =?iso-8859-13?q?D=FFOliveiro?=@ldo@nz.invalid to
comp.lang.c on Thu Nov 12 06:59:57 2026
From Newsgroup: comp.lang.c
An interesting report was recently posted on comp.emacs about some
compiler warnings flagged when building Emacs. HererCOs an example (the
others are somewhat similar):
/usr/opt/Emacs-31.1/emacs-31.1/src/fileio.c:3960:31: warning: implicit
conversion from 'off_t' (aka 'long long') to 'double' changes value
from
9223372036854775807 to 9223372036854775808
[-Wimplicit-const-int-float-conversion]
The above (according to the Emacs 30.2 sources I happen to have handy)
is in a C function called rCLfile_offsetrCY, clearly meant to be called
from Lisp (it takes a rCLLisp_ObjectrCY argument), which expects a numeric argument and converts it to an off_t. It accepts either a Lisp integer
or float object, and converts it accordingly. HererCOs the part that
converts a float:
else if (FLOATP (val))
{
double v = XFLOAT_DATA (val);
if (0 <= v && v < 1.0 + TYPE_MAXIMUM (off_t))
{
off_t o = v;
if (o == v)
return o;
}
}
Now, for 64-bit off_t (the usual state of affairs these days), the
TYPE_MAXIMUM macro returns 9223372036854775807. Converting that to
double, it gets rounded to 9223372036854775808. And then the addition
of 1.0 ... gets lost in the rounding, so the result is still 9223372036854775808, which is what the addition of 1.0 was supposed to
produce in the first place.
In other words, the limit check still works!
I would feel pretty nervous writing code like this. At the very least,
I would add an explanatory comment detailing what is going on. Or
maybe even find some less diabolically clever way of doing it ...
Thoughts?
--- Synchronet 3.22a-Linux NewsLink 1.2