From d4681529c20d5cb2630bfe09f4cf4c4ca4c98a23 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Wed, 18 Nov 2026 00:10:23 +0100 Subject: Restrict pointer subtraction by type --- gcc/gcc/elna-tree.cc | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'gcc') diff --git a/gcc/gcc/elna-tree.cc b/gcc/gcc/elna-tree.cc index 9612ae2..afc809b 100644 --- a/gcc/gcc/elna-tree.cc +++ b/gcc/gcc/elna-tree.cc @@ -138,11 +138,14 @@ namespace elna::gcc convert_expression = fold_build1(NEGATE_EXPR, size_type_node, convert_expression); return fold_build2_loc(operation_location, POINTER_PLUS_EXPR, pointer_type, left, convert_expression); } - else if (POINTER_TYPE_P(left_type) && POINTER_TYPE_P(right_type) && left_type == right_type) + else if (POINTER_TYPE_P(left_type) && POINTER_TYPE_P(right_type)) { - return fold_build2_loc(operation_location, POINTER_DIFF_EXPR, ptrdiff_type_node, left, right); + tree size_exp = pointer_stride(left_type, ptrdiff_type_node); + tree diff = fold_build2_loc(operation_location, POINTER_DIFF_EXPR, ptrdiff_type_node, left, right); + + return fold_build2_loc(operation_location, EXACT_DIV_EXPR, ptrdiff_type_node, diff, size_exp); } - return error_mark_node; + gcc_unreachable(); } gcc_unreachable(); } -- cgit v1.2.3