public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/61240] New: Incorrect warning "integer overflow in expression" on pointer-pointer subtraction
@ 2014-05-19 23:10 Keith.S.Thompson at gmail dot com
  2014-05-20  8:38 ` [Bug c/61240] [4.8/4.9/4.10 Regression] " rguenth at gcc dot gnu.org
                   ` (11 more replies)
  0 siblings, 12 replies; 13+ messages in thread
From: Keith.S.Thompson at gmail dot com @ 2014-05-19 23:10 UTC (permalink / raw)
  To: gcc-bugs

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.
>From gcc-bugs-return-451976-listarch-gcc-bugs=gcc.gnu.org@gcc.gnu.org Mon May 19 23:53:14 2014
Return-Path: <gcc-bugs-return-451976-listarch-gcc-bugs=gcc.gnu.org@gcc.gnu.org>
Delivered-To: listarch-gcc-bugs@gcc.gnu.org
Received: (qmail 3316 invoked by alias); 19 May 2014 23:53:13 -0000
Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm
Precedence: bulk
List-Id: <gcc-bugs.gcc.gnu.org>
List-Archive: <http://gcc.gnu.org/ml/gcc-bugs/>
List-Post: <mailto:gcc-bugs@gcc.gnu.org>
List-Help: <mailto:gcc-bugs-help@gcc.gnu.org>
Sender: gcc-bugs-owner@gcc.gnu.org
Delivered-To: mailing list gcc-bugs@gcc.gnu.org
Received: (qmail 3257 invoked by uid 48); 19 May 2014 23:53:09 -0000
From: "ian at airs dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug go/61232] [4.10 Regression] link errors building libgo
Date: Mon, 19 May 2014 23:53:00 -0000
X-Bugzilla-Reason: CC
X-Bugzilla-Type: changed
X-Bugzilla-Watch-Reason: None
X-Bugzilla-Product: gcc
X-Bugzilla-Component: go
X-Bugzilla-Version: 4.10.0
X-Bugzilla-Keywords: build
X-Bugzilla-Severity: normal
X-Bugzilla-Who: ian at airs dot com
X-Bugzilla-Status: ASSIGNED
X-Bugzilla-Priority: P3
X-Bugzilla-Assigned-To: hubicka at gcc dot gnu.org
X-Bugzilla-Target-Milestone: 4.10.0
X-Bugzilla-Flags:
X-Bugzilla-Changed-Fields:
Message-ID: <bug-61232-4-dPayYuZMfT@http.gcc.gnu.org/bugzilla/>
In-Reply-To: <bug-61232-4@http.gcc.gnu.org/bugzilla/>
References: <bug-61232-4@http.gcc.gnu.org/bugzilla/>
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 7bit
X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/
Auto-Submitted: auto-generated
MIME-Version: 1.0
X-SW-Source: 2014-05/txt/msg01668.txt.bz2
Content-length: 219

https://gcc.gnu.org/bugzilla/show_bug.cgi?ida232

--- Comment #10 from Ian Lance Taylor <ian at airs dot com> ---
Yes, I think that would be clearer.

Your patch does seem to fix the problem building libgo.  Thanks.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c/61240] [4.8/4.9/4.10 Regression] Incorrect warning "integer overflow in expression" on pointer-pointer subtraction
  2014-05-19 23:10 [Bug c/61240] New: Incorrect warning "integer overflow in expression" on pointer-pointer subtraction Keith.S.Thompson at gmail dot com
@ 2014-05-20  8:38 ` rguenth at gcc dot gnu.org
  2014-05-22  9:07 ` rguenth at gcc dot gnu.org
                   ` (10 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu.org @ 2014-05-20  8:38 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61240

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2014-05-20
   Target Milestone|---                         |4.8.3
            Summary|Incorrect warning "integer  |[4.8/4.9/4.10 Regression]
                   |overflow in expression" on  |Incorrect warning "integer
                   |pointer-pointer subtraction |overflow in expression" on
                   |                            |pointer-pointer subtraction
     Ever confirmed|0                           |1

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
We warn for

 <integer_cst 0x7ffff6d54e28 type <integer_type 0x7ffff6c407e0 long int>
constant public overflow 1>

via c-common.c:overflow_warning called from

#1  0x000000000065b80e in parser_build_binary_op (location=5653, 
    code=MINUS_EXPR, arg1=..., arg2=...)
    at /space/rguenther/src/svn/trunk/gcc/c/c-typeck.c:3411
#2  0x000000000068f5ed in c_parser_binary_expression (parser=0x7ffff6d67000, 
    after=0x0, omp_atomic_lhs=<tree 0x0>)
    at /space/rguenther/src/svn/trunk/gcc/c/c-parser.c:6282
#3  0x000000000068dfee in c_parser_conditional_expression (
    parser=0x7ffff6d67000, after=0x0, omp_atomic_lhs=<tree 0x0>)
    at /space/rguenther/src/svn/trunk/gcc/c/c-parser.c:5934
#4  0x000000000068dd75 in c_parser_expr_no_commas (parser=0x7ffff6d67000, 
    after=0x0, omp_atomic_lhs=<tree 0x0>)
    at /space/rguenther/src/svn/trunk/gcc/c/c-parser.c:5852

when building p - (p + -1U) which gets simplified to - -1U -> 1U (with overflow
set - as it's sizetype arithmetic).  pointer_diff is guilty here which calls

  /* First do the subtraction as integers;
     then drop through to build the divide operator.
     Do not do default conversions on the minus operator
     in case restype is a short type.  */

  op0 = build_binary_op (loc,
                         MINUS_EXPR, convert (inttype, op0),
                         convert (inttype, op1), 0);

doing 0 - -1U, converting them to inttype (long int) first.  I suggest
to do that conversion and strip overflow bits in the POINTER_PLUS_EXPR
decomposition part.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c/61240] [4.8/4.9/4.10 Regression] Incorrect warning "integer overflow in expression" on pointer-pointer subtraction
  2014-05-19 23:10 [Bug c/61240] New: Incorrect warning "integer overflow in expression" on pointer-pointer subtraction Keith.S.Thompson at gmail dot com
  2014-05-20  8:38 ` [Bug c/61240] [4.8/4.9/4.10 Regression] " rguenth at gcc dot gnu.org
@ 2014-05-22  9:07 ` rguenth at gcc dot gnu.org
  2014-07-01 20:09 ` jakub at gcc dot gnu.org
                   ` (9 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu.org @ 2014-05-22  9:07 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61240

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|4.8.3                       |4.8.4

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
GCC 4.8.3 is being released, adjusting target milestone.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c/61240] [4.8/4.9/4.10 Regression] Incorrect warning "integer overflow in expression" on pointer-pointer subtraction
  2014-05-19 23:10 [Bug c/61240] New: Incorrect warning "integer overflow in expression" on pointer-pointer subtraction Keith.S.Thompson at gmail dot com
  2014-05-20  8:38 ` [Bug c/61240] [4.8/4.9/4.10 Regression] " rguenth at gcc dot gnu.org
  2014-05-22  9:07 ` rguenth at gcc dot gnu.org
@ 2014-07-01 20:09 ` jakub at gcc dot gnu.org
  2014-08-04  7:21 ` mpolacek at gcc dot gnu.org
                   ` (8 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: jakub at gcc dot gnu.org @ 2014-07-01 20:09 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61240

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Started with r184965.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c/61240] [4.8/4.9/4.10 Regression] Incorrect warning "integer overflow in expression" on pointer-pointer subtraction
  2014-05-19 23:10 [Bug c/61240] New: Incorrect warning "integer overflow in expression" on pointer-pointer subtraction Keith.S.Thompson at gmail dot com
                   ` (2 preceding siblings ...)
  2014-07-01 20:09 ` jakub at gcc dot gnu.org
@ 2014-08-04  7:21 ` mpolacek at gcc dot gnu.org
  2014-08-04  7:30 ` jakub at gcc dot gnu.org
                   ` (7 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2014-08-04  7:21 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61240

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
           Assignee|unassigned at gcc dot gnu.org      |mpolacek at gcc dot gnu.org

--- Comment #5 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Maybe best would be to remove the optimization in pointer_diff altogether. 
Mine for now.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c/61240] [4.8/4.9/4.10 Regression] Incorrect warning "integer overflow in expression" on pointer-pointer subtraction
  2014-05-19 23:10 [Bug c/61240] New: Incorrect warning "integer overflow in expression" on pointer-pointer subtraction Keith.S.Thompson at gmail dot com
                   ` (3 preceding siblings ...)
  2014-08-04  7:21 ` mpolacek at gcc dot gnu.org
@ 2014-08-04  7:30 ` jakub at gcc dot gnu.org
  2014-08-04  7:39 ` mpolacek at gcc dot gnu.org
                   ` (6 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: jakub at gcc dot gnu.org @ 2014-08-04  7:30 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61240

--- Comment #6 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
But in that case we should have an adequate replacement on the
match_and_simplify side.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c/61240] [4.8/4.9/4.10 Regression] Incorrect warning "integer overflow in expression" on pointer-pointer subtraction
  2014-05-19 23:10 [Bug c/61240] New: Incorrect warning "integer overflow in expression" on pointer-pointer subtraction Keith.S.Thompson at gmail dot com
                   ` (4 preceding siblings ...)
  2014-08-04  7:30 ` jakub at gcc dot gnu.org
@ 2014-08-04  7:39 ` mpolacek at gcc dot gnu.org
  2014-12-01 12:26 ` [Bug c/61240] [4.8/4.9/5 " rguenth at gcc dot gnu.org
                   ` (5 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2014-08-04  7:39 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61240

--- Comment #7 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
But C++ has its own pointer_diff version that doesn't do such optimization. 
With my change the C FE would generate the same expr as the C++ FE.  And FEs
shouldn't perform such optimizations anyway.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c/61240] [4.8/4.9/5 Regression] Incorrect warning "integer overflow in expression" on pointer-pointer subtraction
  2014-05-19 23:10 [Bug c/61240] New: Incorrect warning "integer overflow in expression" on pointer-pointer subtraction Keith.S.Thompson at gmail dot com
                   ` (5 preceding siblings ...)
  2014-08-04  7:39 ` mpolacek at gcc dot gnu.org
@ 2014-12-01 12:26 ` rguenth at gcc dot gnu.org
  2014-12-01 12:35 ` mpolacek at gcc dot gnu.org
                   ` (4 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu.org @ 2014-12-01 12:26 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61240

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic,
                   |                            |missed-optimization
           Priority|P3                          |P2

--- Comment #8 from Richard Biener <rguenth at gcc dot gnu.org> ---
So this is also a missed optimization on the C++ side?  Btw, the C++ FE warns
for me as well (on trunk).


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c/61240] [4.8/4.9/5 Regression] Incorrect warning "integer overflow in expression" on pointer-pointer subtraction
  2014-05-19 23:10 [Bug c/61240] New: Incorrect warning "integer overflow in expression" on pointer-pointer subtraction Keith.S.Thompson at gmail dot com
                   ` (6 preceding siblings ...)
  2014-12-01 12:26 ` [Bug c/61240] [4.8/4.9/5 " rguenth at gcc dot gnu.org
@ 2014-12-01 12:35 ` mpolacek at gcc dot gnu.org
  2014-12-19 13:28 ` jakub at gcc dot gnu.org
                   ` (3 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2014-12-01 12:35 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61240

--- Comment #9 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
I view this as a problem that we fold too early - I think this bug (and its
kin) are to be solved with delayed folding.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c/61240] [4.8/4.9/5 Regression] Incorrect warning "integer overflow in expression" on pointer-pointer subtraction
  2014-05-19 23:10 [Bug c/61240] New: Incorrect warning "integer overflow in expression" on pointer-pointer subtraction Keith.S.Thompson at gmail dot com
                   ` (7 preceding siblings ...)
  2014-12-01 12:35 ` mpolacek at gcc dot gnu.org
@ 2014-12-19 13:28 ` jakub at gcc dot gnu.org
  2015-02-13  8:45 ` mpolacek at gcc dot gnu.org
                   ` (2 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: jakub at gcc dot gnu.org @ 2014-12-19 13:28 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61240

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|4.8.4                       |4.8.5

--- Comment #10 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
GCC 4.8.4 has been released.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c/61240] [4.8/4.9/5 Regression] Incorrect warning "integer overflow in expression" on pointer-pointer subtraction
  2014-05-19 23:10 [Bug c/61240] New: Incorrect warning "integer overflow in expression" on pointer-pointer subtraction Keith.S.Thompson at gmail dot com
                   ` (8 preceding siblings ...)
  2014-12-19 13:28 ` jakub at gcc dot gnu.org
@ 2015-02-13  8:45 ` mpolacek at gcc dot gnu.org
  2015-04-27 17:54 ` [Bug c/61240] [4.8/4.9/5/6 " ktietz at gcc dot gnu.org
  2015-06-30 22:49 ` [Bug c/61240] [4.9/5/6 " divyajyotidas15 at gmail dot com
  11 siblings, 0 replies; 13+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2015-02-13  8:45 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61240

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|4.8.5                       |6.0


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c/61240] [4.8/4.9/5/6 Regression] Incorrect warning "integer overflow in expression" on pointer-pointer subtraction
  2014-05-19 23:10 [Bug c/61240] New: Incorrect warning "integer overflow in expression" on pointer-pointer subtraction Keith.S.Thompson at gmail dot com
                   ` (9 preceding siblings ...)
  2015-02-13  8:45 ` mpolacek at gcc dot gnu.org
@ 2015-04-27 17:54 ` ktietz at gcc dot gnu.org
  2015-06-30 22:49 ` [Bug c/61240] [4.9/5/6 " divyajyotidas15 at gmail dot com
  11 siblings, 0 replies; 13+ messages in thread
From: ktietz at gcc dot gnu.org @ 2015-04-27 17:54 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61240

Kai Tietz <ktietz at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ktietz at gcc dot gnu.org

--- Comment #11 from Kai Tietz <ktietz at gcc dot gnu.org> ---
Issue is fixed for C++ delayed folding.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c/61240] [4.9/5/6 Regression] Incorrect warning "integer overflow in expression" on pointer-pointer subtraction
  2014-05-19 23:10 [Bug c/61240] New: Incorrect warning "integer overflow in expression" on pointer-pointer subtraction Keith.S.Thompson at gmail dot com
                   ` (10 preceding siblings ...)
  2015-04-27 17:54 ` [Bug c/61240] [4.8/4.9/5/6 " ktietz at gcc dot gnu.org
@ 2015-06-30 22:49 ` divyajyotidas15 at gmail dot com
  11 siblings, 0 replies; 13+ messages in thread
From: divyajyotidas15 at gmail dot com @ 2015-06-30 22:49 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61240

Divya Jyoti Das <divyajyotidas15 at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |divyajyotidas15 at gmail dot com

--- Comment #12 from Divya Jyoti Das <divyajyotidas15 at gmail dot com> ---
(In reply to Marek Polacek from comment #7)
> But C++ has its own pointer_diff version that doesn't do such optimization. 
> With my change the C FE would generate the same expr as the C++ FE.  And FEs
> shouldn't perform such optimizations anyway.


^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2015-06-30 22:49 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-05-19 23:10 [Bug c/61240] New: Incorrect warning "integer overflow in expression" on pointer-pointer subtraction Keith.S.Thompson at gmail dot com
2014-05-20  8:38 ` [Bug c/61240] [4.8/4.9/4.10 Regression] " rguenth at gcc dot gnu.org
2014-05-22  9:07 ` rguenth at gcc dot gnu.org
2014-07-01 20:09 ` jakub at gcc dot gnu.org
2014-08-04  7:21 ` mpolacek at gcc dot gnu.org
2014-08-04  7:30 ` jakub at gcc dot gnu.org
2014-08-04  7:39 ` mpolacek at gcc dot gnu.org
2014-12-01 12:26 ` [Bug c/61240] [4.8/4.9/5 " rguenth at gcc dot gnu.org
2014-12-01 12:35 ` mpolacek at gcc dot gnu.org
2014-12-19 13:28 ` jakub at gcc dot gnu.org
2015-02-13  8:45 ` mpolacek at gcc dot gnu.org
2015-04-27 17:54 ` [Bug c/61240] [4.8/4.9/5/6 " ktietz at gcc dot gnu.org
2015-06-30 22:49 ` [Bug c/61240] [4.9/5/6 " divyajyotidas15 at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).