public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug analyzer/106394] New: Possible false positive from -Wanalyzer-allocation-size with empty array
@ 2022-07-21 19:07 dmalcolm at gcc dot gnu.org
  2022-07-21 21:56 ` [Bug analyzer/106394] " tlange at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: dmalcolm at gcc dot gnu.org @ 2022-07-21 19:07 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 106394
           Summary: Possible false positive from
                    -Wanalyzer-allocation-size with empty array
           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
                CC: tlange at gcc dot gnu.org
            Blocks: 106358
  Target Milestone: ---

Given:

struct msm_gpu {
  // [...snip...]
  const struct msm_gpu_perfcntr *perfcntrs;
  // [...snip...]
};

struct msm_gpu_perfcntr {
  // [...snip...]
  const char *name;
};

static const struct msm_gpu_perfcntr perfcntrs[] = {};

struct msm_gpu *test(struct msm_gpu *gpu) {
  // [...snip...]
  gpu->perfcntrs = perfcntrs;
  // [...snip...]
  return gpu;
}

I see this with -fanalyzer and trunk:

../../src/a2xx_gpu.c: In function ‘test’:
../../src/a2xx_gpu.c:16:18: warning: allocated buffer size is not a multiple of
the pointee's size [CWE-131] [-Wanalyzer-allocation-size]
   16 |   gpu->perfcntrs = perfcntrs;
      |   ~~~~~~~~~~~~~~~^~~~~~~~~~~
  event 1
    |
    |   12 | static const struct msm_gpu_perfcntr perfcntrs[] = {};
    |      |                                      ^~~~~~~~~
    |      |                                      |
    |      |                                      (1) allocated 0 bytes here
    |
    +--> ‘test’: events 2-3
           |
           |   14 | struct msm_gpu *test(struct msm_gpu *gpu) {
           |      |                 ^~~~
           |      |                 |
           |      |                 (2) entry to ‘test’
           |   15 |   // [...snip...]
           |   16 |   gpu->perfcntrs = perfcntrs;
           |      |   ~~~~~~~~~~~~~~~~~~~~~~~~~~
           |      |                  |
           |      |                  (3) assigned to ‘const struct
msm_gpu_perfcntr *’ here; ‘sizeof (const struct msm_gpu_perfcntr)’ is ‘8’
           |

which looks like a false positive.

Reduced from Linux kernel's drivers/gpu/drm/msm/adreno/a2xx_gpu.c: function
‘a2xx_gpu_init’:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/gpu/drm/msm/adreno/a2xx_gpu.c#n521
which has:

drivers/gpu/drm/msm/adreno/a2xx_gpu.c: In function ‘a2xx_gpu_init’:
drivers/gpu/drm/msm/adreno/a2xx_gpu.c:521:24: error: allocated buffer size is
not a multiple of the pointee's size [CWE-131]
[-Werror=analyzer-allocation-size]
  521 |         gpu->perfcntrs = perfcntrs;
      |         ~~~~~~~~~~~~~~~^~~~~~~~~~~
  event 1
    |
    |  493 | static const struct msm_gpu_perfcntr perfcntrs[] = {
    |      |                                      ^~~~~~~~~
    |      |                                      |
    |      |                                      (1) allocated 0 bytes here
    |
    +--> ‘a2xx_gpu_init’: events 2-7
           |
           |  497 | struct msm_gpu *a2xx_gpu_init(struct drm_device *dev)
           |      |                 ^~~~~~~~~~~~~
           |      |                 |
           |      |                 (2) entry to ‘a2xx_gpu_init’
           |......
           |  506 |         if (!pdev) {
           |      |            ~     
           |      |            |
           |      |            (3) following ‘false’ branch (when ‘pdev’ is
non-NULL)...
           |......
           |  512 |         a2xx_gpu = kzalloc(sizeof(*a2xx_gpu), GFP_KERNEL);
           |      |                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
           |      |                    |
           |      |                    (4) ...to here
           |  513 |         if (!a2xx_gpu) {
           |      |            ~     
           |      |            |
           |      |            (5) following ‘false’ branch (when ‘a2xx_gpu’ is
non-NULL)...
           |......
           |  518 |         adreno_gpu = &a2xx_gpu->base;
           |      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
           |      |                    |
           |      |                    (6) ...to here
           |......
           |  521 |         gpu->perfcntrs = perfcntrs;
           |      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~
           |      |                        |
           |      |                        (7) assigned to ‘const struct
msm_gpu_perfcntr *’ here; ‘sizeof (const struct msm_gpu_perfcntr)’ is ‘24’
           |


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106358
[Bug 106358] [meta-bug] tracker bug for building the Linux kernel with
-fanalyzer

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug analyzer/106394] Possible false positive from -Wanalyzer-allocation-size with empty array
  2022-07-21 19:07 [Bug analyzer/106394] New: Possible false positive from -Wanalyzer-allocation-size with empty array dmalcolm at gcc dot gnu.org
@ 2022-07-21 21:56 ` tlange at gcc dot gnu.org
  2022-07-21 23:04 ` dmalcolm at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: tlange at gcc dot gnu.org @ 2022-07-21 21:56 UTC (permalink / raw)
  To: gcc-bugs

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

Tim Lange <tlange at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2022-07-21
     Ever confirmed|0                           |1

--- Comment #1 from Tim Lange <tlange at gcc dot gnu.org> ---
I've noticed earlier that I produced a different behavior for structs and other
types, i.e. for struct I check for 'alloc_size >= pointee_size' while for other
types I check for 'alloc_size % pointee_size == 0'. I already had it fixed in
the first draft patch I sent for PR106181.

Long story short, it is a simple fix and regression tests are running. I'll
post the fix to the gcc-patches mailing list when the regression tests passed.

[...]
--- a/gcc/analyzer/region-model.cc
+++ b/gcc/analyzer/region-model.cc
@@ -2956,7 +2956,7 @@ capacity_compatible_with_type (tree cst, tree
pointee_size_tree,
   unsigned HOST_WIDE_INT alloc_size = TREE_INT_CST_LOW (cst);

   if (is_struct)
-    return alloc_size >= pointee_size;
+    return alloc_size == 0 || alloc_size >= pointee_size;
   return alloc_size % pointee_size == 0;
 }
[...]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug analyzer/106394] Possible false positive from -Wanalyzer-allocation-size with empty array
  2022-07-21 19:07 [Bug analyzer/106394] New: Possible false positive from -Wanalyzer-allocation-size with empty array dmalcolm at gcc dot gnu.org
  2022-07-21 21:56 ` [Bug analyzer/106394] " tlange at gcc dot gnu.org
@ 2022-07-21 23:04 ` dmalcolm at gcc dot gnu.org
  2022-07-22 19:50 ` [Bug analyzer/106394] False " cvs-commit at gcc dot gnu.org
  2022-07-22 19:52 ` tlange at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: dmalcolm at gcc dot gnu.org @ 2022-07-21 23:04 UTC (permalink / raw)
  To: gcc-bugs

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

David Malcolm <dmalcolm at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|dmalcolm at gcc dot gnu.org        |tlange at gcc dot gnu.org
             Status|NEW                         |ASSIGNED

--- Comment #2 from David Malcolm <dmalcolm at gcc dot gnu.org> ---
Thanks - I'll mark this as assigned to you then.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug analyzer/106394] False positive from -Wanalyzer-allocation-size with empty array
  2022-07-21 19:07 [Bug analyzer/106394] New: Possible false positive from -Wanalyzer-allocation-size with empty array dmalcolm at gcc dot gnu.org
  2022-07-21 21:56 ` [Bug analyzer/106394] " tlange at gcc dot gnu.org
  2022-07-21 23:04 ` dmalcolm at gcc dot gnu.org
@ 2022-07-22 19:50 ` cvs-commit at gcc dot gnu.org
  2022-07-22 19:52 ` tlange at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-07-22 19:50 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Tim Lange <tlange@gcc.gnu.org>:

https://gcc.gnu.org/g:b4cc945c045db74f719ab030969806c14e2d5fc3

commit r13-1802-gb4cc945c045db74f719ab030969806c14e2d5fc3
Author: Tim Lange <mail@tim-lange.me>
Date:   Fri Jul 22 21:44:07 2022 +0200

    Fix handling of zero capacity regions in -Wanalyzer-allocation-size
[PR106394]

    This patch unifies the handling of zero capacity regions for structs
    and other types in the allocation size checker.
    Regression-tested on x86_64 Linux.

    2022-07-22  Tim Lange  <mail@tim-lange.me>

    gcc/analyzer/ChangeLog:

            PR analyzer/106394
            * region-model.cc (capacity_compatible_with_type): Always return
true
            if alloc_size is zero.

    gcc/testsuite/ChangeLog:

            PR analyzer/106394
            * gcc.dg/analyzer/pr106394.c: New test.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug analyzer/106394] False positive from -Wanalyzer-allocation-size with empty array
  2022-07-21 19:07 [Bug analyzer/106394] New: Possible false positive from -Wanalyzer-allocation-size with empty array dmalcolm at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2022-07-22 19:50 ` [Bug analyzer/106394] False " cvs-commit at gcc dot gnu.org
@ 2022-07-22 19:52 ` tlange at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: tlange at gcc dot gnu.org @ 2022-07-22 19:52 UTC (permalink / raw)
  To: gcc-bugs

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

Tim Lange <tlange at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED

--- Comment #4 from Tim Lange <tlange at gcc dot gnu.org> ---
Marking this fixed as the fix landed in trunk.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-07-22 19:52 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-21 19:07 [Bug analyzer/106394] New: Possible false positive from -Wanalyzer-allocation-size with empty array dmalcolm at gcc dot gnu.org
2022-07-21 21:56 ` [Bug analyzer/106394] " tlange at gcc dot gnu.org
2022-07-21 23:04 ` dmalcolm at gcc dot gnu.org
2022-07-22 19:50 ` [Bug analyzer/106394] False " cvs-commit at gcc dot gnu.org
2022-07-22 19:52 ` tlange at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).