This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug middle-end/59125] [4.8/4.9 Regression] gcc triggers wrong strncpy_chk


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59125

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
We already re-fold whenever we fold the object-size stmt via fold_stmt looking
up the def of the address - inlining for example triggers this.  We'd later
pick up more opportunities if we'd enhance forwprop for this for example with:

@@ -1002,6 +1003,22 @@ forward_propagate_addr_expr (tree name,
       bool result;
       tree use_rhs;

+      /* If the use is a __builtin_object_size call then try folding it.  */
+      if (gimple_call_builtin_p (use_stmt, BUILT_IN_OBJECT_SIZE))
+       {
+         tree res = fold_builtin_object_size (rhs,
+                                              gimple_call_arg (use_stmt, 1));
+         if (res
+             && TREE_CODE (res) == INTEGER_CST
+             && !integer_all_onesp (res))
+           {
+             gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
+             update_call_from_tree (&gsi, res);
+             update_stmt (gsi_stmt (gsi));
+             continue;
+           }
+       }
+
       /* If the use is not in a simple assignment statement, then
         there is nothing we can do.  */
       if (gimple_code (use_stmt) != GIMPLE_ASSIGN)

but it does not help this testcase as explained, but it helps the following
one:

#include <string.h>
struct s {
    union u {
        struct {
            char vi[8];
            char pi[16];
        };
        char all[8+16+4];
    } u;
    int x;
};
void f(struct s *s)
{
  char vi[8+1];
  char pi[16+1];
  strncpy(vi, s->u.vi, sizeof(s->u.vi));
  strncpy(s->u.all, "AbcdefghAbcdefghijklmnopAbcd", sizeof(s->u.all));
}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]