<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 - Memory leak when `clang_parsetranslationunit2fullargv` fails due to bad compiler flags"
   href="https://bugs.llvm.org/show_bug.cgi?id=47832">47832</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Memory leak when `clang_parsetranslationunit2fullargv` fails due to bad compiler flags
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>10.0
          </td>
        </tr>

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

        <tr>
          <th>OS</th>
          <td>Linux
          </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>libclang
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>boris.staletic@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>klimek@google.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=24051" name="attach_24051" title="C file leaking memory">attachment 24051</a> <a href="attachment.cgi?id=24051&action=edit" title="C file leaking memory">[details]</a></span>
C file leaking memory

Since LLVM10, if compiler flags are bad and result in a failed compiler
invokation, and if the CXUnsavedFile array isn't empty,
`clang_parsetranslationunit2fullargv()` leaks memory.

Attached is a C file that leaks due to the above.

The amount of leaked memory seems to be proportional to the `CXUnsavedFile[]`
that is passed to `clang_parsetranslationunit2fullargv()`.

When run with valgrind, the attached snippet has the following output:


==25857== Memcheck, a memory error detector
==25857== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==25857== Using Valgrind-3.16.1 and LibVEX; rerun with -h for copyright info
==25857== Command: ./a.out
==25857==
Clang Parse Error: 4
==25857==
==25857== HEAP SUMMARY:
==25857==     in use at exit: 288,195 bytes in 1,711 blocks
==25857==   total heap usage: 7,257 allocs, 5,546 frees, 865,848 bytes
allocated
==25857==
==25857== 49 bytes in 1 blocks are definitely lost in loss record 1,355 of
1,706
==25857==    at 0x483B07F: operator new(unsigned long, std::nothrow_t const&)
(vg_replace_malloc.c:385)
==25857==    by 0x5D6B409:
llvm::WritableMemoryBuffer::getNewUninitMemBuffer(unsigned long, llvm::Twine
const&) (in /home/bstaletic/work/ycmd/third_party/clang/lib/libclang.so.10)
==25857==    by 0x5D6B78E:
llvm::MemoryBuffer::getMemBufferCopy(llvm::StringRef, llvm::Twine const&) (in
/home/bstaletic/work/ycmd/third_party/clang/lib/libclang.so.10)
==25857==    by 0x506A402: clang_parseTranslationUnit_Impl(void*, char const*,
char const* const*, int, llvm::ArrayRef<CXUnsavedFile>, unsigned int,
CXTranslationUnitImpl**) (in
/home/bstaletic/work/ycmd/third_party/clang/lib/libclang.so.10)
==25857==    by 0x506B203: void llvm::function_ref<void
()>::callback_fn<clang_parseTranslationUnit2FullArgv::{lambda()#1}>(long) (in
/home/bstaletic/work/ycmd/third_party/clang/lib/libclang.so.10)
==25857==    by 0x5D59C3F:
llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (in
/home/bstaletic/work/ycmd/third_party/clang/lib/libclang.so.10)
==25857==    by 0x5D59CA3: RunSafelyOnThread_Dispatch(void*) (in
/home/bstaletic/work/ycmd/third_party/clang/lib/libclang.so.10)
==25857==    by 0x5DD042C: threadFuncSync(void*) (in
/home/bstaletic/work/ycmd/third_party/clang/lib/libclang.so.10)
==25857==    by 0xA4C43E8: start_thread (in /usr/lib/libpthread-2.32.so)
==25857==    by 0xA3EC292: clone (in /usr/lib/libc-2.32.so)
==25857==
==25857== LEAK SUMMARY:
==25857==    definitely lost: 49 bytes in 1 blocks
==25857==    indirectly lost: 0 bytes in 0 blocks
==25857==      possibly lost: 0 bytes in 0 blocks
==25857==    still reachable: 288,146 bytes in 1,710 blocks
==25857==         suppressed: 0 bytes in 0 blocks
==25857== Reachable blocks (those to which a pointer was found) are not shown.
==25857== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==25857==
==25857== For lists of detected and suppressed errors, rerun with: -s
==25857== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)</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>