Thread View: comp.lang.c
1 messages
1 total messages
Started by Lawrence D’Oliveiro
Thu, 12 Nov 2026 06:59
Is This Sailing A Little Close To The Wind?
Author: Lawrence D’Oliveiro
Date: Thu, 12 Nov 2026 06:59
Date: Thu, 12 Nov 2026 06:59
42 lines
2762 bytes
2762 bytes
An interesting report was recently posted on comp.emacs about some
compiler warnings flagged when building Emacs. Here’s 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 “file_offset”, clearly meant to be called
from Lisp (it takes a “Lisp_Object” 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. Here’s 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?
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads