From faa43301ceb4ea5892c9d7a04e8ad7cd88ad2fed Mon Sep 17 00:00:00 2001 From: jakub Date: Wed, 17 Dec 2014 10:38:43 +0000 Subject: [PATCH] svn merge -r218810:218811 svn+ssh://gcc.gnu.org/svn/gcc/trunk git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/branches/redhat@218815 138bc75d-0d04-0410-961f-82ee72b054a4 --- gcc-4_9-branch/gcc/c/ChangeLog | 8 ++++++++ gcc-4_9-branch/gcc/c/c-convert.c | 15 +++++++++++++++ gcc-4_9-branch/gcc/testsuite/ChangeLog | 5 +++++ gcc-4_9-branch/gcc/testsuite/c-c++-common/ubsan/pr64289.c | 9 +++++++++ 4 files changed, 37 insertions(+) create mode 100644 gcc-4_9-branch/gcc/testsuite/c-c++-common/ubsan/pr64289.c diff --git a/gcc-4_9-branch/gcc/c/ChangeLog b/gcc-4_9-branch/gcc/c/ChangeLog index d3e3d1c5580..7b78ac7fc67 100644 --- a/gcc-4_9-branch/gcc/c/ChangeLog +++ b/gcc-4_9-branch/gcc/c/ChangeLog @@ -1,3 +1,11 @@ +2014-12-17 Jakub Jelinek + + PR sanitizer/64289 + * c-convert.c: Include ubsan.h. + (convert): For real -> integral casts and + -fsanitize=float-cast-overflow don't call convert_to_integer, but + instead instrument the float cast directly. + 2014-10-30 Release Manager * GCC 4.9.2 released. diff --git a/gcc-4_9-branch/gcc/c/c-convert.c b/gcc-4_9-branch/gcc/c/c-convert.c index 38bacdaa4df..87a0dc6517c 100644 --- a/gcc-4_9-branch/gcc/c/c-convert.c +++ b/gcc-4_9-branch/gcc/c/c-convert.c @@ -34,6 +34,7 @@ along with GCC; see the file COPYING3. If not see #include "c-tree.h" #include "langhooks.h" #include "target.h" +#include "ubsan.h" /* Change of width--truncation and extension of integers or reals-- is represented with NOP_EXPR. Proper functioning of many things @@ -110,6 +111,20 @@ convert (tree type, tree expr) case INTEGER_TYPE: case ENUMERAL_TYPE: + if (flag_sanitize & SANITIZE_FLOAT_CAST + && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE + && COMPLETE_TYPE_P (type) + && current_function_decl != NULL_TREE + && !lookup_attribute ("no_sanitize_undefined", + DECL_ATTRIBUTES (current_function_decl))) + { + expr = c_save_expr (expr); + tree check = ubsan_instrument_float_cast (loc, type, expr); + expr = fold_build1 (FIX_TRUNC_EXPR, type, expr); + if (check == NULL) + return expr; + return fold_build2 (COMPOUND_EXPR, TREE_TYPE (expr), check, expr); + } ret = convert_to_integer (type, e); goto maybe_fold; diff --git a/gcc-4_9-branch/gcc/testsuite/ChangeLog b/gcc-4_9-branch/gcc/testsuite/ChangeLog index cd6e90679fe..564c8e8590c 100644 --- a/gcc-4_9-branch/gcc/testsuite/ChangeLog +++ b/gcc-4_9-branch/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2014-12-17 Jakub Jelinek + + PR sanitizer/64289 + * c-c++-common/ubsan/pr64289.c: New test. + 2014-12-15 Jakub Jelinek PR tree-optimization/63551 diff --git a/gcc-4_9-branch/gcc/testsuite/c-c++-common/ubsan/pr64289.c b/gcc-4_9-branch/gcc/testsuite/c-c++-common/ubsan/pr64289.c new file mode 100644 index 00000000000..1e38e6d3c29 --- /dev/null +++ b/gcc-4_9-branch/gcc/testsuite/c-c++-common/ubsan/pr64289.c @@ -0,0 +1,9 @@ +/* PR sanitizer/64289 */ +/* { dg-do compile } */ +/* { dg-options "-fsanitize=float-cast-overflow" } */ + +int +foo (int a) +{ + return (int) (0 ? 0 : a ? a : 0.5); +} -- 2.11.4.GIT