<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </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 --- - Find size_t overflows in calls to malloc, realloc"
   href="https://llvm.org/bugs/show_bug.cgi?id=27729">27729</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Find size_t overflows in calls to malloc, realloc
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>Macintosh
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>MacOS X
          </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>Static Analyzer
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>mark.rogers@powermapper.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Would be very useful to warn about malloc/realloc overflows. Code like the
following is a fairly common source of exploits, because an attacker can trick
malloc into allocating less memory than the code expects, and overwrite memory
outside the malloc'd block with a payload:

wchar_t* MallocOverflow( const PdfStringTest & rString )
{
    size_t  lLen = rString.GetCharacterLength();

    if( !lLen )
        return NULL;

    // should warn about size_t overflow: 
    // if lLen == 1GB, and sizeof(wchar_t)=4,
    // then malloc'd buffer is 4 bytes long on 32-bit system
    wchar_t* pDest = static_cast<wchar_t*>(malloc( sizeof(wchar_t) * (lLen + 1)
));

    return pDest;
}

Any multiplication involving size_t is suspect since you sometimes see code
like this:

size_t buffLen = sizeof(wchar_t) * (lLen + 1);
wchar_t* pDest = static_cast<wchar_t*>(malloc(buffLen));

Best Regards
Mark</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>