[Bug tree-optimization/88372] alloc_size attribute is ignored on function pointers

2018-12-14 Thread msebor at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88372

Martin Sebor  changed:

   What|Removed |Added

   See Also||https://gcc.gnu.org/bugzill
   ||a/show_bug.cgi?id=88506

--- Comment #7 from Martin Sebor  ---
Bug 88506 tracks the missing warning.

[Bug tree-optimization/88372] alloc_size attribute is ignored on function pointers

2018-12-14 Thread msebor at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88372

Martin Sebor  changed:

   What|Removed |Added

 Status|ASSIGNED|RESOLVED
 Resolution|--- |FIXED

--- Comment #6 from Martin Sebor  ---
Fixed for GCC 9 in r267158.  A warning when a function without the attribute is
assigned to a pointer with it should also be implemented to help detect bugs
that this might lead to.

[Bug tree-optimization/88372] alloc_size attribute is ignored on function pointers

2018-12-14 Thread msebor at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88372

--- Comment #5 from Martin Sebor  ---
Author: msebor
Date: Fri Dec 14 22:45:55 2018
New Revision: 267158

URL: https://gcc.gnu.org/viewcvs?rev=267158=gcc=rev
Log:
PR tree-optimization/88372 - alloc_size attribute is ignored on function
pointers

gcc/ChangeLog:

PR tree-optimization/88372
* calls.c (maybe_warn_alloc_args_overflow): Handle function pointers.
* tree-object-size.c (alloc_object_size): Same.  Simplify.
* doc/extend.texi (Object Size Checking): Update.
(Other Builtins): Add __builtin_object_size.
(Common Type Attributes): Add alloc_size.
(Common Variable Attributes): Ditto.

gcc/testsuite/ChangeLog:

PR tree-optimization/88372
* gcc.dg/Walloc-size-larger-than-18.c: New test.
* gcc.dg/builtin-object-size-19.c: Same.


Added:
trunk/gcc/testsuite/gcc.dg/Walloc-size-larger-than-18.c
trunk/gcc/testsuite/gcc.dg/builtin-object-size-19.c
Modified:
trunk/gcc/ChangeLog
trunk/gcc/calls.c
trunk/gcc/doc/extend.texi
trunk/gcc/testsuite/ChangeLog
trunk/gcc/tree-object-size.c

[Bug tree-optimization/88372] alloc_size attribute is ignored on function pointers

2018-12-06 Thread msebor at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88372

Martin Sebor  changed:

   What|Removed |Added

   Keywords||patch

--- Comment #4 from Martin Sebor  ---
Patch: https://gcc.gnu.org/ml/gcc-patches/2018-12/msg00401.html

[Bug tree-optimization/88372] alloc_size attribute is ignored on function pointers

2018-12-06 Thread msebor at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88372

Martin Sebor  changed:

   What|Removed |Added

 Status|NEW |ASSIGNED
   Assignee|unassigned at gcc dot gnu.org  |msebor at gcc dot 
gnu.org

--- Comment #3 from Martin Sebor  ---
Testing a patch.

[Bug tree-optimization/88372] alloc_size attribute is ignored on function pointers

2018-12-06 Thread rguenth at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88372

--- Comment #2 from Richard Biener  ---
+  /* If there is no function, look at the type of the called
+expression in case it's been declared attribute alloc_size.  */
+  callee = gimple_call_fn (call);
+  if (callee && TREE_CODE (TREE_TYPE (callee)) == POINTER_TYPE)
+   callee = TREE_TYPE (callee);

you should alway suse gimple_call_fntype here.

[Bug tree-optimization/88372] alloc_size attribute is ignored on function pointers

2018-12-05 Thread msebor at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88372

Martin Sebor  changed:

   What|Removed |Added

 Status|UNCONFIRMED |NEW
   Last reconfirmed||2018-12-05
 CC||msebor at gcc dot gnu.org
  Component|c   |tree-optimization
 Ever confirmed|0   |1

--- Comment #1 from Martin Sebor  ---
This attribute is already accepted on the function pointer so it should work
just like alloc_align does.  The reason why __builtin_object_size doesn't
report the size is because it only considers attributes on the called functions
in function calls and doesn't try to look for attributes on the types of
function pointers.  So I'd consider this limitation a bug rather than
enhancement request.  With that, accepting [[gnu::alloc_size(N)]] should make
sense as well.

With the very lightly tested patch below __builtin_object_size reports the same
size in both functions in the test case. 

Index: gcc/tree-object-size.c
===
--- gcc/tree-object-size.c  (revision 266799)
+++ gcc/tree-object-size.c  (working copy)
@@ -414,8 +414,18 @@ alloc_object_size (const gcall *call, int object_s

   gcc_assert (is_gimple_call (call));

+  /* Lopok for the called function.  */
   callee = gimple_call_fndecl (call);
   if (!callee)
+{
+  /* If there is no function, look at the type of the called
+expression in case it's been declared attribute alloc_size.  */
+  callee = gimple_call_fn (call);
+  if (callee && TREE_CODE (TREE_TYPE (callee)) == POINTER_TYPE)
+   callee = TREE_TYPE (callee);
+}
+
+  if (!callee)
 return unknown[object_size_type];

   alloc_size = lookup_attribute ("alloc_size",
@@ -429,7 +439,8 @@ alloc_object_size (const gcall *call, int object_s
 arg2 = TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (p)))-1;
 }

-  if (DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
+  if (DECL_P (callee)
+  && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
 switch (DECL_FUNCTION_CODE (callee))
   {
   case BUILT_IN_CALLOC: