[Bug c++/42129] New: ICE in pointer difference with sizeof(int)>sizeof(void *)

kikairoya at gmail dot com gcc-bugzilla@gcc.gnu.org
Sat Nov 21 06:39:00 GMT 2009


ICE in pointer difference operation in C++ (without C).
This causes only sizeof(int)>sizeof(void *) platform such as h8300-elf with -mh
-mn -mint32.

test case:
==================================
typedef int *T;
T p, q;
int main() {
  int v = p - q;
}
==================================

Dump gimplified this,
 p - q
converted to
 ((int)(short int)p - (int)(short int)q) / sizeof(T) [with result_type = short
int]

This problem caused because of generated "/ sizeof(T)" as ptrdiff_t(= short
int) but should be int.
Most of platforms are sizeof(ptrdiff_t)>sizeof(int), this problem is not
appeared.
In specially case, T = char, "/ sizeof(T)" is folded and problem does not
appear.

To fix this, generate "/ sizeof(T)" and return type as same type of
"((int)(short int)p - (int)(short int)q)".




Index: gcc/cp/typeck.c
===================================================================
--- gcc/cp/typeck.c     (revision 154402)
+++ gcc/cp/typeck.c     (working copy)
@@ -4209,6 +4209,7 @@
   tree result;
   tree restype = ptrdiff_type_node;
   tree target_type = TREE_TYPE (ptrtype);
+  tree internal_type;

   if (!complete_type_or_else (target_type, NULL_TREE))
     return error_mark_node;
@@ -4229,6 +4230,8 @@
                            cp_convert (restype, op1),
                            tf_warning_or_error);

+  internal_type = TREE_TYPE (op0);
+
   /* This generates an error if op1 is a pointer to an incomplete type.  */
   if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
     error ("invalid use of a pointer to an incomplete type in pointer
arithmetic");
@@ -4239,8 +4242,8 @@

   /* Do the division.  */

-  result = build2 (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
-  return fold_if_not_in_template (result);
+  result = build2 (EXACT_DIV_EXPR, internal_type, op0, cp_convert
(internal_type, op1));
+  return fold_if_not_in_template (cp_convert (restype, result));
 }

 /* Construct and perhaps optimize a tree representation


-- 
           Summary: ICE in pointer difference with sizeof(int)>sizeof(void
                    *)
           Product: gcc
           Version: 4.5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: kikairoya at gmail dot com
GCC target triplet: h8300-elf


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42129



More information about the Gcc-bugs mailing list