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

            Bug ID: 78503
           Summary: -Wint-in-bool-context false positive on unsigned
                    multiplication
           Product: gcc
           Version: 7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

GCC issues a -Wint-in-bool-context warning on the following test case and
suggests using the AND expression instead.  The code is valid (it tries to
avoid calling malloc with a zero argument) and the warning is a false positive.
 The suggestion to use the AND expression also isn't very helpful here (it's
confusing).  There is no obvious way the AND expression could be used while
preserving the effects of the code.  Changing expression to (n * sizeof (int)
!= 0) does avoid the warning.

$ cat c.c && gcc -S -Wall c.c
void f (void*);

void g (unsigned n)
{
  void *p = (n * sizeof (int)) ? __builtin_malloc (n * sizeof (n)) : 0;
  f (p);
}
c.c: In function ‘g’:
c.c:5:16: warning: ‘*’ in boolean context, suggest ‘&&’ instead
[-Wint-in-bool-context]
   void *p = (n * sizeof (int)) ? __builtin_malloc (n * sizeof (n)) : 0;
             ~~~^~~~~~~~~~~~~~~


Note that the test case has been distilled from a modified definition of the
XALLOCAVEC macro in libiberty:

  #define XALLOCAVEC(T, N)   ((N) ? (T *) alloca (sizeof (T) * (N)) : (T *)0)

The modified definition which tries to prevent zero-size allocation then causes
GCC to fail to bootstrap with errors like the following:

/src/gcc/svn/gcc/fold-const.c:1499:33: error: ‘*’ in boolean context, suggest
‘&&’ instead [-Werror=int-in-bool-context]
  elts = XALLOCAVEC (tree, nelts * 4);
/src/gcc/svn/gcc/../include/libiberty.h:356:28: note: in definition of macro
‘XALLOCAVEC’
 #define XALLOCAVEC(T, N) ((N) ? (T *) alloca (sizeof (T) * (N)) : (T *)0)
                            ^

Reply via email to