OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[NPU] Bound recursion depth in compatibility string parser (#38005)
mingmingtasd25b710ed79d2
diff --git a/src/plugins/intel_npu/src/al/include/intel_npu/compat_string_parser.hpp b/src/plugins/intel_npu/src/al/include/intel_npu/compat_string_parser.hppindex 46d54790c07..0e86ece26e0 100644--- a/src/plugins/intel_npu/src/al/include/intel_npu/compat_string_parser.hpp+++ b/src/plugins/intel_npu/src/al/include/intel_npu/compat_string_parser.hpp@@ -75,8 +75,8 @@versions.Version history:+ 2026-05-28 Limit recursion depth.2026-05-06 Initial version.-*/#pragma once@@ -122,10 +122,13 @@ public:private:enum class CaptureMode { ATTR, STRING };+ constexpr static int MAX_NESTING = 20;char peek() const;char nextc();void expect(char c);+ void nest();+ void unnest();void reset(std::string_view input, CaptureMode mode = CaptureMode::ATTR);void startCapture(CaptureMode mode);std::string_view stopCapture(CaptureMode mode);@@ -156,6 +159,7 @@ private:std::string_view::iterator _captureEnd;int _nesting = 0; // Nesting level for current capture modeint _optional = 0; // Tracks top-level optional attributes+ int _depth = 0; // Tracks total nesting depth to prevent stack overflow on malicious inputattr_map_type _attributes;attr_map_type _optional_attributes;@@ -169,8 +173,6 @@ Parser::Parser(std::string_view input, const Iterable& legalAttributeNames) {template <typename Iterable>void Parser::parse(std::string_view input, const Iterable& legalAttributeNames) {reset(input);- _attributes.clear();- _optional_attributes.clear();parseString();if (peek() != 0) {throw errorAt("at the end of the string");@@ -225,14 +227,28 @@ inline void Parser::expect(char c) {nextc();}+inline void Parser::nest() {+ if (++_depth > MAX_NESTING) {+ throw errorAt("Maximum nesting depth exceeded");+ }+}++inline void Parser::unnest() {+ assert(_depth > 0);+ --_depth;+}+inline void Parser::reset(std::string_view input, CaptureMode mode) {_current = input.begin();_end = input.end();_optional = 0;_nesting = 0;+ _depth = 0;_captureMode = mode;_captureStart = _end;_captureEnd = _end;+ _attributes.clear();+ _optional_attributes.clear();}inline void Parser::startCapture(CaptureMode mode) {@@ -269,11 +285,13 @@ inline std::string_view Parser::parseString() {// expr ::= attr | '{' string '}'inline void Parser::parseExpr() {if (peek() == '{') {+ nest();++_optional;nextc(); // '{'parseString();expect('}');--_optional;+ unnest();} else {parseAttr();}@@ -284,12 +302,14 @@ inline void Parser::parseAttr() {auto name = parseName();expect('=');if (peek() == '[') {+ nest();startCapture(CaptureMode::ATTR);parseList();auto list = stopCapture(CaptureMode::ATTR);if (!list.empty()) {setAttribute(name, list);}+ unnest();return;}auto value = parseValue();diff --git a/src/plugins/intel_npu/tests/unit/npu/metadata/metadata_text.hpp b/src/plugins/intel_npu/tests/unit/npu/metadata/metadata_text.hppindex e861ee8c1a6..a614229f72d 100644--- a/src/plugins/intel_npu/tests/unit/npu/metadata/metadata_text.hpp+++ b/src/plugins/intel_npu/tests/unit/npu/metadata/metadata_text.hpp@@ -162,6 +162,19 @@ TEST_F(MetadataHumanReadableTests, allTextFields) {EXPECT_EQ(storedMeta->get_compatibility_descriptor().value(), compatDesc);}+TEST_F(MetadataHumanReadableTests, rejectsExcessiveNesting) {+ const auto make = [](size_t depth) {+ return std::string("meta=2.0;ov=2026.1.0;") + std::string(depth, '{') + "future_field=FUTURE_VAL" ++ std::string(depth, '}');+ };+ // Shallow nesting is accepted.+ OV_ASSERT_NO_THROW((void)::read_as_text(make(8)));+ // Nesting past the depth limit must throw. A moderate depth (above the limit,+ // well below a stack overflow) keeps this deterministic: without the guard the+ // input parses cleanly, so its absence is a clean failure, not a crash.+ ASSERT_ANY_THROW((void)::read_as_text(make(100)));+}+TEST_P(MetadataTextTest, Format) {std::unique_ptr<MetadataBase> meta;if (isValid) {