@@ -897,6 +897,24 @@ static void setTokenValue(Token* tok, ValueFlow::Value value, const Settings* se
897897 else if (Token::Match (parent, " :: %name%" ) && parent->astOperand2 () == tok) {
898898 setTokenValue (parent, value, settings);
899899 }
900+ // Calling std::size or std::empty on an array
901+ else if (value.isTokValue () && Token::simpleMatch (value.tokvalue , " {" ) && tok->variable () &&
902+ tok->variable ()->isArray () && Token::Match (parent->previous (), " %name% (" ) && astIsRHS (tok)) {
903+ std::vector<const Token*> args = getArguments (value.tokvalue );
904+ if (const Library::Function* f = settings->library .getFunction (parent->previous ())) {
905+ if (f->containerYield == Library::Container::Yield::SIZE) {
906+ ValueFlow::Value v (value);
907+ v.valueType = ValueFlow::Value::ValueType::INT;
908+ v.intvalue = args.size ();
909+ setTokenValue (parent, v, settings);
910+ } else if (f->containerYield == Library::Container::Yield::EMPTY) {
911+ ValueFlow::Value v (value);
912+ v.intvalue = args.empty ();
913+ v.valueType = ValueFlow::Value::ValueType::INT;
914+ setTokenValue (parent, v, settings);
915+ }
916+ }
917+ }
900918}
901919
902920static void setTokenValueCast (Token *parent, const ValueType &valueType, const ValueFlow::Value &value, const Settings *settings)
@@ -7840,9 +7858,6 @@ static std::vector<ValueFlow::Value> isOutOfBoundsImpl(const ValueFlow::Value& s
78407858 return {};
78417859 if (size.bound == ValueFlow::Value::Bound::Lower)
78427860 return {};
7843- ValueFlow::Value inBoundsValue = inferCondition (" <" , indexTok, size.intvalue );
7844- if (inBoundsValue.isKnown () && inBoundsValue.intvalue != 0 )
7845- return {};
78467861 ValueFlow::Value value = inferCondition (" >=" , indexTok, indexValue->intvalue );
78477862 if (!value.isKnown ())
78487863 return {};
@@ -7855,6 +7870,9 @@ static std::vector<ValueFlow::Value> isOutOfBoundsImpl(const ValueFlow::Value& s
78557870
78567871std::vector<ValueFlow::Value> ValueFlow::isOutOfBounds (const Value& size, const Token* indexTok, bool possible)
78577872{
7873+ ValueFlow::Value inBoundsValue = inferCondition (" <" , indexTok, size.intvalue );
7874+ if (inBoundsValue.isKnown () && inBoundsValue.intvalue != 0 )
7875+ return {};
78587876 std::vector<ValueFlow::Value> result = isOutOfBoundsImpl (size, indexTok, false );
78597877 if (!result.empty ())
78607878 return result;
0 commit comments