@@ -442,33 +442,36 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken,
442442 continue ;
443443
444444 // Check assignments in the if-statement. Skip multiple assignments since we don't track those
445- if (Token::Match (innerTok, " %var% =" ) && innerTok->astParent () == innerTok->next () &&
446- !(innerTok->next ()->astParent () && innerTok->next ()->astParent ()->isAssignmentOp ())) {
445+ const Token* const eqTok = innerTok->astParent ();
446+ if (Token::Match (innerTok, " .| %var% =" ) && Token::simpleMatch (eqTok, " =" ) &&
447+ !(eqTok->astParent () && eqTok->astParent ()->isAssignmentOp ())) {
447448 // allocation?
448449 // right ast part (after `=` operator)
449- const Token* tokRightAstOperand = innerTok-> next () ->astOperand2 ();
450+ const Token* tokRightAstOperand = eqTok ->astOperand2 ();
450451 while (tokRightAstOperand && tokRightAstOperand->isCast ())
451452 tokRightAstOperand = tokRightAstOperand->astOperand2 () ? tokRightAstOperand->astOperand2 () : tokRightAstOperand->astOperand1 ();
453+ const Token* const allocTok = Token::simpleMatch (eqTok->astOperand2 (), " (" ) ? eqTok->astOperand2 ()->astOperand1 () : eqTok->astOperand2 ();
454+ const Token* const ptrTok = innerTok->astOperand2 () ? innerTok->astOperand2 () : innerTok;
452455 if (tokRightAstOperand && Token::Match (tokRightAstOperand->previous (), " %type% (" )) {
453456 const Library::AllocFunc* f = mSettings ->library .getAllocFuncInfo (tokRightAstOperand->previous ());
454457 if (f && f->arg == -1 ) {
455- VarInfo::AllocInfo& varAlloc = alloctype[innerTok ->varId ()];
458+ VarInfo::AllocInfo& varAlloc = alloctype[ptrTok ->varId ()];
456459 varAlloc.type = f->groupId ;
457460 varAlloc.status = VarInfo::ALLOC;
458461 varAlloc.allocTok = tokRightAstOperand->previous ();
459462 } else {
460463 // Fixme: warn about leak
461- alloctype.erase (innerTok ->varId ());
464+ alloctype.erase (ptrTok ->varId ());
462465 }
463466
464- changeAllocStatusIfRealloc (alloctype, innerTok-> tokAt ( 2 ) , varTok);
465- } else if (mTokenizer ->isCPP () && Token::Match (innerTok-> tokAt ( 2 ) , " new !!(" )) {
466- const Token* tok2 = innerTok-> tokAt ( 2 ) ->astOperand1 ();
467+ changeAllocStatusIfRealloc (alloctype, allocTok , varTok);
468+ } else if (mTokenizer ->isCPP () && Token::Match (allocTok , " new !!(" )) {
469+ const Token* tok2 = allocTok ->astOperand1 ();
467470 const bool arrayNew = (tok2 && (tok2->str () == " [" || (tok2->str () == " (" && tok2->astOperand1 () && tok2->astOperand1 ()->str () == " [" )));
468- VarInfo::AllocInfo& varAlloc = alloctype[innerTok ->varId ()];
471+ VarInfo::AllocInfo& varAlloc = alloctype[ptrTok ->varId ()];
469472 varAlloc.type = arrayNew ? NEW_ARRAY : NEW;
470473 varAlloc.status = VarInfo::ALLOC;
471- varAlloc.allocTok = innerTok-> tokAt ( 2 ) ;
474+ varAlloc.allocTok = allocTok ;
472475 }
473476 }
474477
0 commit comments