<html><head><meta http-equiv="Content-Type" content="text/html charset=iso-8859-1"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On Feb 19, 2013, at 10:18 PM, Anton Yartsev <<a href="mailto:anton.yartsev@gmail.com">anton.yartsev@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">Hi, Jordan. Thanx for the review!<br><br>Attached is the new version of the patch with all the comments addressed. Also added support for directly called operator new()/new[]() and operator delete()<br><br>There is currently one problem with handling of operator delete(). The following test<br><br>void testDeleteOp1() {<br> int *p = (int *)malloc(sizeof(int));<br> operator delete(p); // FIXME: should complain "Argument to operator delete() was allocated by malloc(), not operator new"<br>}<br><br>produce no warnings as attached RefState seem to be missing at the point when checkPostStmt(const CallExpr *CE, CheckerContext &C) callback is called for operator delete(p).<br>I haven't investigated the problem deeply yet, intend to address it parallel with the review.<br><br><blockquote type="cite">+ if (NE->getNumPlacementArgs())<br>+ return;<br>+ // skip placement new operators as they may not allocate memory<br><br>Two comments here:<br>- Please make sure all comments are complete, capitalized, and punctuated sentences. (This has the important one, "complete"....just missing capitalization and punctuation.)<br></blockquote>I'll try. Unfortunately I am not as good in English as I want to be, so sorry for my grammar, syntax, and punctuation.<br><br>-- <br>Anton<br><br><span><MallocChecker_v2.patch></span></blockquote><div><br></div>Anton, it's great that you are working on this! See the comments/feedback below.</div><div><br></div><div>+// Numeration is added for convenient mapping to RefState::Kind</div><div>Should be "Enumeration"</div><div><br></div><div><div> enum Kind { // Reference to allocated memory.</div><div> Allocated,</div></div><div>....</div><div><div>+</div><div>+ // Mapped DeallocatorKind. </div><div>+ // Expected kind of a deallocator; used to check if a real </div><div>+ // kind of a deallocator matches expected one</div><div>+ Free = 0x4,</div><div>+ Delete = 0x8,</div><div>+ DeleteArray = 0xC</div></div><div><br></div><div>Wouldn't it be cleaner and simpler if the Kind enumeration just had something like this:</div><div> AllocatedWithMalloc </div><div> AllocatedWithNew</div><div> AllocatedWithNewArray</div><div><br></div><div>Or, if we want the name to be based on the deallocator function, we could use something like "ToBeReleasedWithFree, ToBeReleasedWithNew, ..". </div><div><br></div><div>Just adding another kind as extra enumeration values does not seem right. Another option is to make Kind be a pointer to a static array, which contains objects recording all necessary info about each kind (<span style="color: rgb(0, 97, 65); font-family: Monaco; font-size: 11px; ">MacOSKeychainAPIChecker</span> uses this approach). This is probably an overkill for now, but is another option.</div><div><br></div><div><div>+ const FunctionDecl *getCalleeDecl() const { return CalleeDecl; }</div></div><div>Do we only store the name of the allocator declaration here? Do we need to store this in the state? (Growing states implies memory overhead.) Can't this be easily implied from the kind?</div><div><br></div><div><div>One thing to keep in mind here, is that it would be beneficial to make this all work well with ownership annotations (even though they are not supported now, they are something we might want to have in the future). So feel free to push back on simplification suggestions if there is a clear benefit with respect to the ownership annotations support. On the other hand, we don't need to design a solution that would fit the annotations now either.</div></div><div><br></div><div><div>+ bool isDefaultNonptrplacementNewDelete(const FunctionDecl *FD,</div></div><div>Camel case should be used</div><div><br></div><div><div>+ } else if (isDefaultNonptrplacementNewDelete(FD, C)) {</div><div>+ OverloadedOperatorKind K = FD->getDeclName().getCXXOverloadedOperator();</div><div>+ if (K == OO_New)</div><div>+ State = MallocMemAux(C, CE, CE->getArg(0), UndefinedVal(), State,</div><div>+ D_delete);</div><div>+ else if (K == OO_Array_New)</div><div>+ State = MallocMemAux(C, CE, CE->getArg(0), UndefinedVal(), State,</div><div>+ D_deleteArray);</div><div>+ else</div><div>+ State = FreeMemAux(C, CE, State, 0, false, ReleasedAllocatedMemory);</div></div><div>In the last else, we assume that delete/delete[] was called based on the logic in isDefaultNonptrplacementNewDelete. Please either check the kind again or assert this. We want to make sure they don't go out of sync.</div><div><br></div><div><div>+void MallocChecker::checkPostStmt(const CXXNewExpr *NE, </div><div>+ CheckerContext &C) const {NE</div><div>+</div><div>+ FunctionDecl *OperatorNew = NE->getOperatorNew();</div><div>+ if (!OperatorNew)</div><div>+ return;</div><div>+</div><div>+ // Skip custom new operators</div><div>+ if (!OperatorNew->isImplicit() &&</div><div>+ !C.getSourceManager().isInSystemHeader(OperatorNew->getLocStart()) &&</div><div>+ !NE->isGlobalNew())</div><div>+ return;</div><div>+</div><div>+ // Skip standard global placement operator new/new[](std::size_t, void * p);</div><div>+ // process all other standard new/new[] operators including placement</div><div>+ // operators new/new[](std::size_t, const std::nothrow_t&)</div><div>+ if (OperatorNew->isReservedGlobalPlacementOperator())</div><div>+ return;</div></div><div><br></div><div>Is there a reason why we first process each operator new in "checkPostStmt(const callExpr" and finish processing in "checkPostStmt(const CXXNewExpr" ? I think the code would be simpler if everything could be done in a single callback. </div><div><br></div><div>Also, the tests above are similar to what is done in isDefaultNonptrplacementNewDelete. These should be factored out into a subroutine to avoid inconsistency. (Ex: Shouldn't "NE->isGlobalNew()" check be done in both?)</div><div><br></div><div>Same remarks go for "checkPreStmt(const CXXDeleteExpr".</div><div><br></div><div><div>+void MallocChecker::PrintExpectedAllocName(raw_ostream &os, CheckerContext &C, </div><div>+ const Expr *E) const {</div><div>+ DeallocatorKind dKind = GetDeallocKind(C, E);</div><div>+</div><div>+ switch(dKind) {</div><div>+ case D_free: os << "malloc()"; return;</div><div>+ case D_delete: os << "operator new"; return;</div><div>+ case D_deleteArray: os << "operator new[]"; return;</div><div>+ case D_unknown: os << "unknown means"; return;</div></div><div><br></div><div>This function is used to form user visible warnings. Do we ever expect it to print "unknown means"? Can this be based on the Kind stored inside of RefState, where there is no D_unknown?</div></body></html>