https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61240
Bug ID: 61240
Summary: Incorrect warning "integer overflow in expression" on
pointer-pointer subtraction
Product: gcc
Version: 4.8.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: Keith.S.Thompson at gmail dot com
Credit goes to "Lumbering Lummox", the author of this Stack Overflow post:
http://stackoverflow.com/q/23747641/827263
I see this problem with gcc versions 4.8.0 and 4.9.0, both compiled from
source, on Linux Mint 14 on x86_64.
Source program:
int main(void) {
int i;
int *p = &i;
int *q = &i + 1;
p - (p - 1);
q - (q - 1);
}
Compiler output:
% /usr/local/apps/gcc-4.8.0/bin/gcc gcc-bug-integer-overflow.c
gcc-bug-integer-overflow.c: In function ‘main’:
gcc-bug-integer-overflow.c:5:7: warning: integer overflow in expression
[-Woverflow]
p - (p - 1);
^
gcc-bug-integer-overflow.c:6:7: warning: integer overflow in expression
[-Woverflow]
q - (q - 1);
^
A warning would be appropriate for "p - (p - 1)", since (p - 1) has undefined
behavior -- but since it's pointer arithmetic, not integer arithmetic, the
"integer overflow" warning is at least incorrectly worded. Furthermore, the
error message points to the first "-", which is not the problem.
As for "q - (q - 1)", no warning should be issued at all, since both "(q - 1)"
and "q - (q - 1)" are valid expressions with well defined behavior (yielding &i
and (ptrdiff_t)1, respectively).
This might be related to bug #48267.