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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2019-10-17
     Ever confirmed|0                           |1

--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
Confirmed.  The warning sees the '_1 = (char[8] *) ivtmp.6_10;' DEF_STMT but
ignores the cast to (char[8] *) and considers the size of ivtmp.6_9(3) as the
size of the array that could store the longest string.

e ()
{
  unsigned long ivtmp.6;
  int f;
  char[8] * _1;

  <bb 2> [local count: 107374]:
  ivtmp.6_14 = (unsigned long) &c;

  <bb 3> [local count: 1073741824]:
  # ivtmp.6_10 = PHI <ivtmp.6_14(2), ivtmp.6_9(3)>
  _1 = (char[8] *) ivtmp.6_10;
  strncpy (&b, _1, 8);
  ivtmp.6_9 = ivtmp.6_10 + 8;
  goto <bb 3>; [100.00%]

}


The warning could be changed to consider the type of the cast instead, trading
the false positive for a false negative in code like:

  char a[23];

  void g (char *b)
  {
    typedef char A[8];
    A *p = (A *)a;
    for (int i = 0; i != 3; ++i)
      strncpy (b, p + i, 8);
  }


My guess is the former is more common than the latter so the trade-off might
make sense.  Alternatively, the warning could try to take both the cast and the
type of the object into consideration and use some heuristic to decide between
the two.

Reply via email to