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

msebor at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Wed Dec 5 19:27:00 GMT 2018


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88372

Martin Sebor <msebor at gcc dot gnu.org> 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 <msebor at gcc dot gnu.org> ---
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:


More information about the Gcc-bugs mailing list