diff options
author | jakub <jakub@138bc75d-0d04-0410-961f-82ee72b054a4> | 2014-12-17 09:26:49 +0000 |
---|---|---|
committer | jakub <jakub@138bc75d-0d04-0410-961f-82ee72b054a4> | 2014-12-17 09:26:49 +0000 |
commit | 35ea013980810be2b15a088d125ba949a90ea109 (patch) | |
tree | 3e4f830105b96d05efc3b91b0cae82022f8bb826 /gcc/c/c-convert.c | |
parent | 7ac2fb259bf9827e4e54653215041e4c469c7f1f (diff) | |
download | gcc-35ea013980810be2b15a088d125ba949a90ea109.tar.gz |
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.
* c-c++-common/ubsan/pr64289.c: New test.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@218811 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'gcc/c/c-convert.c')
-rw-r--r-- | gcc/c/c-convert.c | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/gcc/c/c-convert.c b/gcc/c/c-convert.c index 95be453fa3f..ba565da1218 100644 --- a/gcc/c/c-convert.c +++ b/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 @@ -109,6 +110,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; |