@@ -1864,35 +1864,28 @@ void CheckBufferOverrun::arrayIndexThenCheck()
18641864 }
18651865
18661866 if (Token::Match (tok, " %name% [ %var% ]" )) {
1867- tok = tok->tokAt ( 2 );
1867+ tok = tok->next ( );
18681868
1869- const unsigned int indexID = tok->varId ();
1870- const std::string& indexName (tok->str ( ));
1869+ const unsigned int indexID = tok->next ()-> varId ();
1870+ const std::string& indexName (tok->strAt ( 1 ));
18711871
1872- // skip array index..
1873- tok = tok->tokAt (2 );
1874- while (tok && tok->str () == " [" )
1875- tok = tok->link ()->next ();
1876-
1877- // syntax error
1878- if (!tok)
1879- return ;
1880-
1881- // skip comparison
1882- if (tok->tokType () == Token::eComparisonOp)
1883- tok = tok->tokAt (2 );
1872+ // Iterate AST upwards
1873+ const Token* tok2 = tok;
1874+ const Token* tok3 = tok2;
1875+ while (tok2->astParent () && tok2->tokType () != Token::eLogicalOp) {
1876+ tok3 = tok2;
1877+ tok2 = tok2->astParent ();
1878+ }
18841879
1885- if (!tok)
1886- break ;
1887- // skip close parentheses
1888- if (tok->str () == " )" )
1889- tok = tok->next ();
1880+ // Ensure that we ended at a logical operator and that we came from its left side
1881+ if (tok2->tokType () != Token::eLogicalOp || tok2->astOperand1 () != tok3)
1882+ continue ;
18901883
18911884 // check if array index is ok
18921885 // statement can be closed in parentheses, so "(| " is using
1893- if (Token::Match (tok , " && (| %varid% <|<=" , indexID))
1886+ if (Token::Match (tok2 , " && (| %varid% <|<=" , indexID))
18941887 arrayIndexThenCheckError (tok, indexName);
1895- else if (Token::Match (tok , " && (| %any% >|>= %varid% !!+" , indexID))
1888+ else if (Token::Match (tok2 , " && (| %any% >|>= %varid% !!+" , indexID))
18961889 arrayIndexThenCheckError (tok, indexName);
18971890 }
18981891 }
0 commit comments