<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 - calloc() overflow test eliminated by optimization"
   href="https://bugs.llvm.org/show_bug.cgi?id=37304">37304</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>calloc() overflow test eliminated by optimization
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>6.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

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

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>-New Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>wellons@nullprogram.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Minimal example:

    #include <stdint.h>
    #include <stdlib.h>
    int test_calloc(void) {
        return !!calloc(SIZE_MAX, 2);
    }

With -O1:

    define i32 @test_calloc() local_unnamed_addr #0 {
        ret i32 1
    }

The calloc() is eliminated because the returned pointer is unused. It's
replaced by a simulated successful return despite the allocation being
impossible, at least on the targeted platforms. In a more complicated scenario,
a later part of the function — such an uneliminated malloc() of the same
allocation size — may rely on calloc()'s overflow test for correct results.

A contrived example of this:

    void *test_overflow(size_t n, size_t m) {
        return calloc(n, m) ? malloc(n * m) : 0;
    }

Compiles to:

    define noalias i8* @test_overflow(i64, i64) local_unnamed_addr #0 {
        %3 = mul i64 %1, %0
        %4 = tail call noalias i8* @malloc(i64 %3) #2
        ret i8* %4
    }

An eliminated calloc() should be replaced by an overflow test.</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>