[LLVMdev] Clang: strange malloc usage

Valery Pushkar pollnossa at gmail.com
Wed Jul 29 08:16:24 PDT 2015


Hello everyone!

I've found weird lines of code in tools/libclang/CIndex.cpp file in clang
sources. Function clang_tokenize holds the following lines:

  *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size());
  memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size());

Since malloc function can return null pointer and there are no checks for
this case, memmove invocation leads to segmentation fault.

The same case can be found in tools/libclang/CXString.cpp file in
function createDup:

  char *Spelling = static_cast<char *>(malloc(String.size() + 1));
  memmove(Spelling, String.data(), String.size());


Could somebody clarify me if I'm wrong?

Thanks,
Valeriy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150729/2277a450/attachment.html>


More information about the llvm-dev mailing list