Bug 104369 - False positive from -Wanalyzer-use-of-uninitialized-value with realloc moving buffer
Summary: False positive from -Wanalyzer-use-of-uninitialized-value with realloc moving...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: analyzer (show other bugs)
Version: 12.0
: P3 normal
Target Milestone: ---
Assignee: David Malcolm
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-02-03 14:55 UTC by David Malcolm
Modified: 2022-02-03 22:52 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
Reduced reproducer (710 bytes, text/plain)
2022-02-03 14:55 UTC, David Malcolm
Details

Note You need to log in before you can comment on or make changes to this bug.
Description David Malcolm 2022-02-03 14:55:24 UTC
Created attachment 52343 [details]
Reduced reproducer

The attached reproducer emits two false positives from -Wanalyzer-use-of-uninitialized-value, both "when 'realloc' succeeds, moving buffer", the first of which is:

<source>: In function 'main':
<source>:79:34: warning: use of uninitialized value '*pollfds.fd' [CWE-457] [-Wanalyzer-use-of-uninitialized-value]
   79 |       pollfds[nsockets - 1].fd = accept(pollfds[0].fd, &remote, &len);
      |                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  'main': events 1-7
    |
    |   62 |   if (!pollfds) {
    |      |      ^
    |      |      |
    |      |      (1) following 'false' branch (when 'pollfds' is non-NULL)...
    |......
    |   67 |     rc = ppoll(pollfds, nsockets, NULL, NULL);
    |      |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    |      |          |
    |      |          (2) ...to here
    |......
    |   74 |       newpollfds = realloc(pollfds, nsockets * sizeof(*pollfds));
    |      |                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    |      |                    |
    |      |                    (3) when 'realloc' succeeds, moving buffer
    |      |                    (4) region created on heap here
    |   75 |       if (!newpollfds) {
    |      |          ~
    |      |          |
    |      |          (5) following 'false' branch (when 'newpollfds' is non-NULL)...
    |......
    |   78 |       pollfds = newpollfds;
    |      |       ~~~~~~~~~~~~~~~~~~~~
    |      |               |
    |      |               (6) ...to here
    |   79 |       pollfds[nsockets - 1].fd = accept(pollfds[0].fd, &remote, &len);
    |      |                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    |      |                                  |
    |      |                                  (7) use of uninitialized value '*pollfds.fd' here
    |

On Compiler Explorer:
  https://godbolt.org/z/EKrnsoaY4

From downstream report:
  https://bugzilla.redhat.com/show_bug.cgi?id=2047926#c5
Comment 1 GCC Commits 2022-02-03 22:48:26 UTC
The master branch has been updated by David Malcolm <dmalcolm@gcc.gnu.org>:

https://gcc.gnu.org/g:3ef328c293a336df0aead2d72c0c5ed9781a9861

commit r12-7040-g3ef328c293a336df0aead2d72c0c5ed9781a9861
Author: David Malcolm <dmalcolm@redhat.com>
Date:   Wed Feb 2 16:39:12 2022 -0500

    analyzer: fixes to realloc-handling [PR104369]
    
    This patch fixes various issues with how -fanalyzer handles "realloc"
    seen when debugging PR analyzer/104369.
    
    Previously it wasn't correctly copying over the contents of the old
    buffer for the success-with-move case, leading to false
    -Wanalyzer-use-of-uninitialized-value diagnostics.
    
    I also noticed that -fanalyzer failed to properly handle "realloc" for
    cases where the ptr's region had unknown dynamic extents, and an ICE
    for the case where a tainted value is used as a realloc size argument.
    
    This patch fixes these issues, including the false uninit diagnostics
    seen in PR analyzer/104369.
    
    gcc/analyzer/ChangeLog:
            PR analyzer/104369
            * engine.cc (exploded_graph::process_node): Use the node for any
            diagnostics, avoiding ICE if a bifurcation update adds a
            saved_diagnostic, such as for a tainted realloc size.
            * region-model-impl-calls.cc
            (region_model::impl_call_realloc::success_no_move::update_model):
            Require the old pointer to be non-NULL to be able successfully
            grow in place.  Use model->deref_rvalue rather than maybe_get_region
            to support the old pointer being symbolic.
            (region_model::impl_call_realloc::success_with_move::update_model):
            Likewise.  Add a constraint that the new pointer != the old pointer.
            Use a sized_region when setting the value of the new region.
            Handle the case where we don't know the dynamic size of the old
            region by marking the new region as unknown.
            * sm-taint.cc (tainted_allocation_size::tainted_allocation_size):
            Update assertion to also allow for MEMSPACE_UNKNOWN.
            (tainted_allocation_size::emit): Likewise.
            (region_model::check_dynamic_size_for_taint): Likewise.
    
    gcc/testsuite/ChangeLog:
            PR analyzer/104369
            * gcc.dg/analyzer/pr104369-1.c: New test.
            * gcc.dg/analyzer/pr104369-2.c: New test.
            * gcc.dg/analyzer/realloc-3.c: New test.
            * gcc.dg/analyzer/realloc-4.c: New test.
            * gcc.dg/analyzer/taint-realloc.c: New test.
    
    Signed-off-by: David Malcolm <dmalcolm@redhat.com>
Comment 2 David Malcolm 2022-02-03 22:52:43 UTC
Should be fixed by the above commit.