GNU bug report logs -
#8545
issues with recent doprnt-related changes
Previous Next
Reported by: Paul Eggert <eggert <at> cs.ucla.edu>
Date: Mon, 25 Apr 2011 05:48:01 UTC
Severity: normal
Done: Eli Zaretskii <eliz <at> gnu.org>
Bug is archived. No further changes may be made.
Full log
View this message in rfc822 format
On 04/30/11 14:03, Richard Stallman wrote:
> long
> foo (char *p, int i)
> {
> return &p[i + 1] - &p[i];
> }
> ...
> i+1 is computed as an integer, but then it gets converted to a long.
Unfortunately that doesn't explain FOO's behavior.
If i+1 is computed as an int and if wraparound is required, then
when i is INT_MAX, i+1 must be INT_MIN. (FOO does not convert
i+1 to long; but even if it did, INT_MIN's value would be unchanged by
that conversion.) If p is pointing into a large array, &p[INT_MIN]
and &p[INT_MAX] can both be valid addresses, and in that case
foo (p, INT_MAX) would have to yield -2**32 + 1 on a typical 64-bit host
where signed integer arithmetic wrapped around.
But in my experience no compiler does it that way. FOO always returns 1.
> What happens here seems to be an issue about type conversion combined
> with addition -- not addition itself.
I'm afraid not. First, FOO doesn't have any type conversions.
If I is an int, A[I] doesn't convert I to any other type; it
simply uses I's value without conversion.
Second, it's easy to construct an example that involves only "int":
int
bar (int i)
{
return i < i + 1;
}
With many compilers, BAR always returns 1, even when i == INT_MAX.
> These compilers are taking a strange liberty.
> Why isn't that a bug?
Well, for starters, most programmers *expect* FOO to return 1,
and similarly for BAR. Why would a programmer file a bug report
when the program is behaving as expected?
> printf ("%d", INT_MAX+1);
> will output INT_MIN.
That's true for all systems I have ready access to, yes. And I
expect there are other cases where int arithmetic wraps around
reliably. But there are many practical cases where it doesn't.
We cannot simply advise programmers to assume that adding 1
to INT_MAX always results in INT_MIN, as that assumption is
often incorrect nowadays.
This bug report was last modified 4 years and 251 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.