<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - false positive for realloc()?"
   href="https://bugs.llvm.org/show_bug.cgi?id=44310">44310</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>false positive for realloc()?
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>9.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Windows NT
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Static Analyzer
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>dcoughlin@apple.com
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>rgerhards@hq.adiscon.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dcoughlin@apple.com, llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>I think I found a false positive in static analyzer's understanding of
realloc(). Please have a look here:

<a href="http://ubuntu16.rsyslog.com/19-12-13_07-32-59/2019-12-13-073355-8933-1/report-da528b.html#EndPath">http://ubuntu16.rsyslog.com/19-12-13_07-32-59/2019-12-13-073355-8933-1/report-da528b.html#EndPath</a>

I do a realloc here:
uchar *const pbuf = realloc(mem->data, mem->len + write_size + 1);

Static analyzer assumes that this causes the memory (mem->data) to be released.
This is fine as it also assume a non-null return value. However, slightly later
in the code I reassign the "freed" memory pointer to the new buffer address
returned by realloc():

mem->data = pbuf;

That seems to be ignored by static analyzer as later in the process it claims
that the memory is still freed (report cause 18, after return from function). I
used "const" wherever appropriate and so there should be no doubt about
aliasing in regard to the new pointer.

In the end result, static analyzer diagnoses both a double free and
use-after-free. IMHO this is the effect of not honoring the reassignment of the
buffer pointer.

I have also checked other realloc() uses inside the same code base. They follow
all a similar pattern, but none of them triggers any alert.

I may have overlooked something, but I really don't have any idea anymore of
what could be the real error cause.

As a last resort, I will now go ahead and make the code in question invisible
to static analyzer via preprocessor #if, but I really don't like this idea.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>