>From bd5541b903747082c984aa92ebe16faeae7b0c71 Mon Sep 17 00:00:00 2001 From: Gwen Fu <gwen3293940...@gmail.com> Date: Thu, 27 Mar 2025 15:55:23 +0800 Subject: [PATCH] [Description of 119222 bug] Conversion of inf to integer is not diagnosed
[How addresses it] In c-warn.cc, function: warnings_for_convert_and_check: Since the conversion of inf to int occurs when result is FIX_TRUNC_EXPR, this is the main basis for judgment and processing . Since expr may be a variable declaration, DECL_INITIAL is required . After that, the program discusses three situations from the view of floating-point constants, floating-point expressions,and real-point division expressions to determine whether it is infinity. The flag bit warn_flag is used to indicate whether an illegal situation occurs. [Test cases] double c = 3.3/4.4-4.4 ; double b = 1/static_cast<double>(0) ; int m = b ; int n = c ; int f = 1/0 ; int g = 1/static_cast<double>(0) ; int h = 1/static_cast<double>(0) / static_cast<double>(0) / static_cast<double>(0) ; [Bootstrapping and testing] host:Ubuntu24 x86_64 (And I think this pathch suitable for any machine type) result: int m = b ; //successful warning int n = c ; //missing a warning int f = 1/0;//successful warning int g =...; int h = ... //successful warning [Notice] "int n = c" didn't send a warning , """ REAL_VALUE_TYPE value = TREE_REAL_CST(dst); bool is_inf = REAL_VALUE_ISINF(value); """ And when I execute"call debug_tree(dst)" to clerify the value of c , it displayed as: constant -3.6500000000000003552713678800500929355621337890625e+0> .It is not infinity. But actually the value mast be infinity --- gcc/c-family/c-warn.cc | 51 +++++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 11 deletions(-) [Changelog] gcc/c-family/ChangeLog: * c-warn.cc (conversion_warning): (warnings_for_convert_and_check): I use "./contrib/mklog.py *patch " to generate that above . Is this Changelog?
From bd5541b903747082c984aa92ebe16faeae7b0c71 Mon Sep 17 00:00:00 2001 From: Gwen Fu <gwen3293940...@gmail.com> Date: Thu, 27 Mar 2025 15:55:23 +0800 Subject: [PATCH] [Description of 119222 bug] Conversion of inf to integer is not diagnosed [How addresses it] In c-warn.cc, function: warnings_for_convert_and_check: Since the conversion of inf to int occurs when result is FIX_TRUNC_EXPR, this is the main basis for judgment and processing . Since expr may be a variable declaration, DECL_INITIAL is required . After that, the program discusses three situations from the view of floating-point constants, floating-point expressions,and real-point division expressions to determine whether it is infinity. The flag bit warn_flag is used to indicate whether an illegal situation occurs. [Test cases] double c = 3.3/4.4-4.4 ; double b = 1/static_cast<double>(0) ; int m = b ; int n = c ; int f = 1/0 ; int g = 1/static_cast<double>(0) ; int h = 1/static_cast<double>(0) / static_cast<double>(0) / static_cast<double>(0) ; [Bootstrapping and testing] host:Ubuntu24 x86_64 (And I think this pathch suitable for any machine type) result: int m = b ; //successful warning int n = c ; //missing a warning int f = 1/0;//successful warning int g =...; int h = ... //successful warning [Notice] "int n = c" didn't send a warning , """ REAL_VALUE_TYPE value = TREE_REAL_CST(dst); bool is_inf = REAL_VALUE_ISINF(value); """ And when I execute"call debug_tree(dst)" to clerify the value of c , it displayed as: constant -3.6500000000000003552713678800500929355621337890625e+0> .It is not infinity. But actually the value mast be infinity --- gcc/c-family/c-warn.cc | 51 +++++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 11 deletions(-) [Changelog] gcc/c-family/ChangeLog: * c-warn.cc (conversion_warning): (warnings_for_convert_and_check): I use "./contrib/mklog.py *patch " to generate that above . Is this Changelog? diff --git a/gcc/c-family/c-warn.cc b/gcc/c-family/c-warn.cc index 406b591bfa0..16de902dae8 100644 --- a/gcc/c-family/c-warn.cc +++ b/gcc/c-family/c-warn.cc @@ -1283,17 +1283,6 @@ conversion_warning (location_t loc, tree type, tree expr, tree result) case CEIL_DIV_EXPR: case EXACT_DIV_EXPR: case RDIV_EXPR: - /*Issue a warning about infinity conversion to int*/ - if( TREE_CODE(type) == INTEGER_TYPE - && TREE_CODE (TREE_OPERAND(expr,1)) == REAL_CST && real_zerop (TREE_OPERAND(expr,1))) - { - bool ret = warning_at(loc , OPT_Wfloat_conversion , - "conversion from %qT to %qT changes infinity to maximum or minimum integer value", - expr_type , type) ; - if(!ret) warning_at(loc , OPT_Wconversion , - "conversion from %qT to %qT changes infinity to maximum or minimum integer value", - expr_type , type) ; - } arith_ops = 2; goto default_; @@ -1516,6 +1505,46 @@ warnings_for_convert_and_check (location_t loc, tree type, tree expr, "changes the value of %qE", exprtype, type, expr); } + /*"inifity to int" this operation belongs to FIX_TRUNC_EXPR*/ + else if (TREE_CODE(result) == FIX_TRUNC_EXPR + && TREE_CODE(exprtype) == REAL_TYPE) + { + bool warn_flag = false; + tree dst = expr; + /*Considering that it may be an assignment expression, + expr may be a variable declaration.*/ + if(TREE_CODE(dst) == VAR_DECL) + dst = DECL_INITIAL(dst) ; + + if (TREE_CODE(dst) == REAL_CST) + { + REAL_VALUE_TYPE value = TREE_REAL_CST(dst); + bool is_inf = REAL_VALUE_ISINF(value); + if (is_inf) + warn_flag = true; + } + else if (TREE_CODE(dst) == RDIV_EXPR + && TREE_CODE (TREE_OPERAND(dst, 1)) == REAL_CST + && real_zerop (TREE_OPERAND(dst, 1))) + warn_flag = true; + /*After getting the initial value of the variable, + the node type is FLOAT_EXPR*/ + else if (TREE_CODE(dst) == FLOAT_EXPR + && EXPR_P(TREE_OPERAND(dst, 0))) + { + tree op = TREE_OPERAND(dst, 0) ; + + tree denominator = TREE_OPERAND(op, 1) ; + if ((TREE_CODE(denominator) == INTEGER_CST + && integer_zerop(denominator))) + warn_flag = true; + } + if(warn_flag) + warning_at (loc, 0, "Undefined behavior: conversion " + "from %qT to %qT", expr, type); + + conversion_warning(loc , type , expr , result); + } else conversion_warning (loc, type, expr, result); } -- 2.45.2