<div dir="ltr"><div><div>Yes, if malloc returns NULL in either of those cases, it will (probably, depending on architecture) crash. Are you actually seeing cases where this happens?<br><br>"Fixing" this is hard, since it's pretty much just going to end up with `if it's NULL exit with a message of out of memory`, and I'm not sure you want that either... <br><br>There are probably several thousand other allocations all over clang/llvm where the result is not checked from malloc/new, because basically the assumption is that allocating memory always works.<br><br>Note that at least in linux, because the OS allows "overcommit", it's nearly impossible to write a meaningful program that runs out of memory and gets NULL back from malloc - it gets killed by OOM-killer before it gets to the point of returning NULL from malloc - I haven't tried in Windows for a long time.<br><br></div>--<br></div>Mats<br></div><div class="gmail_extra"><br><div class="gmail_quote">On 29 July 2015 at 16:16, Valery Pushkar <span dir="ltr"><<a href="mailto:pollnossa@gmail.com" target="_blank">pollnossa@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hello everyone!<div><br></div><div>I've found weird lines of code in tools/libclang/CIndex.cpp file in clang sources. Function clang_tokenize holds the following lines:</div><div>  </div><div>  *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size());</div><div>  memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size());</div><div><br></div><div>Since malloc function can return null pointer and there are no checks for this case, memmove invocation leads to segmentation fault.</div><div><br></div><div>The same case can be found in tools/libclang/CXString.cpp file in function createDup:</div><div><br></div><div><div>  char *Spelling = static_cast<char *>(malloc(String.size() + 1));</div><div>  memmove(Spelling, String.data(), String.size());</div></div><div><br></div><div><br></div><div>Could somebody clarify me if I'm wrong?</div><div><br></div><div>Thanks,</div><div>Valeriy</div><div><br></div></div>
<br>_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu" rel="noreferrer" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" rel="noreferrer" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
<br></blockquote></div><br></div>