diff options
author | jakub <jakub@138bc75d-0d04-0410-961f-82ee72b054a4> | 2008-07-31 08:02:49 +0000 |
---|---|---|
committer | jakub <jakub@138bc75d-0d04-0410-961f-82ee72b054a4> | 2008-07-31 08:02:49 +0000 |
commit | 2c281b159b2d95362f0f6462e9856349b36ce0d8 (patch) | |
tree | f01ea79c042dfd15dfdd549c69c554a94dd8b22a /gcc/builtins.c | |
parent | 01d2a17db0fa25559298a08191b9235ad2e6ff8b (diff) | |
download | gcc-2c281b159b2d95362f0f6462e9856349b36ce0d8.tar.gz |
PR c/36970
* builtins.c (maybe_emit_free_warning): New function.
(expand_builtin): Process BUILT_IN_FREE even at -O0. Call
maybe_emit_free_warning for BUILT_IN_FREE.
* gcc.dg/free-1.c: New test.
* gcc.dg/free-2.c: New test.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@138362 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'gcc/builtins.c')
-rw-r--r-- | gcc/builtins.c | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/gcc/builtins.c b/gcc/builtins.c index ca551915c50..2dffd53e604 100644 --- a/gcc/builtins.c +++ b/gcc/builtins.c @@ -207,6 +207,7 @@ static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode, enum built_in_function); static void maybe_emit_chk_warning (tree, enum built_in_function); static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function); +static void maybe_emit_free_warning (tree); static tree fold_builtin_object_size (tree, tree); static tree fold_builtin_strcat_chk (tree, tree, tree, tree); static tree fold_builtin_strncat_chk (tree, tree, tree, tree, tree); @@ -6130,7 +6131,8 @@ expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode, if (!optimize && !called_as_built_in (fndecl) && DECL_ASSEMBLER_NAME_SET_P (fndecl) - && fcode != BUILT_IN_ALLOCA) + && fcode != BUILT_IN_ALLOCA + && fcode != BUILT_IN_FREE) return expand_call (exp, target, ignore); /* The built-in function expanders test for target == const0_rtx @@ -7007,6 +7009,10 @@ expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode, maybe_emit_sprintf_chk_warning (exp, fcode); break; + case BUILT_IN_FREE: + maybe_emit_free_warning (exp); + break; + default: /* just do library call, if unknown builtin */ break; } @@ -11981,6 +11987,27 @@ maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode) } } +/* Emit warning if a free is called with address of a variable. */ + +static void +maybe_emit_free_warning (tree exp) +{ + tree arg = CALL_EXPR_ARG (exp, 0); + + STRIP_NOPS (arg); + if (TREE_CODE (arg) != ADDR_EXPR) + return; + + arg = get_base_address (TREE_OPERAND (arg, 0)); + if (arg == NULL || INDIRECT_REF_P (arg)) + return; + + if (SSA_VAR_P (arg)) + warning (0, "%Kattempt to free a non-heap object %qD", exp, arg); + else + warning (0, "%Kattempt to free a non-heap object", exp); +} + /* Fold a call to __builtin_object_size with arguments PTR and OST, if possible. */ |