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

            Bug ID: 108745
           Summary: -Wanalyzer-deref-before-check false positives seen in
                    ImageMagick due to checks in macros
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: analyzer
          Assignee: dmalcolm at gcc dot gnu.org
          Reporter: dmalcolm at gcc dot gnu.org
  Target Milestone: ---

Created attachment 54441
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=54441&action=edit
Reproducer

Am seeing lots of false positives from -Wanalyzer-deref-before-check on
ImageMagick on code like this:

  if ((image->columns == 0) || (image->rows == 0))
    ThrowReaderException("MustSpecifyImageSize");
  return image;

where ThrowReaderException has an "if (image)" embedded within it:

https://godbolt.org/z/74d1aoqTa

<source>: In function 'ReadMAPImage':
<source>:38:6: warning: check of 'image' for NULL after already dereferencing
it [-Wanalyzer-deref-before-check]
   38 |   if ((image) != (Image *) NULL) \
      |      ^
<source>:52:5: note: in expansion of macro 'ThrowReaderException'
   52 |     ThrowReaderException("MustSpecifyImageSize");
      |     ^~~~~~~~~~~~~~~~~~~~
  'ReadMAPImage': event 1
    |
    |   51 |   if ((image->columns == 0) || (image->rows == 0))
    |      |        ~~~~~^~~~~~~~~
    |      |             |
    |      |             (1) pointer 'image' is dereferenced here
    |
  'ReadMAPImage': event 2
    |
    |   38 |   if ((image) != (Image *) NULL) \
    |      |      ^
    |      |      |
    |      |      (2) pointer 'image' is checked for NULL here but it was
already dereferenced at (1)
<source>:52:5: note: in expansion of macro 'ThrowReaderException'
    |   52 |     ThrowReaderException("MustSpecifyImageSize");
    |      |     ^~~~~~~~~~~~~~~~~~~~
    |
Compiler returned: 0

Presumably we shouldn't warn if the check is hidden inside a macro.

Reply via email to