diff --git a/modules/core/src/persistence_yml.cpp b/modules/core/src/persistence_yml.cpp index 009ab7d8b7..d926fd0b2e 100644 --- a/modules/core/src/persistence_yml.cpp +++ b/modules/core/src/persistence_yml.cpp @@ -432,12 +432,13 @@ public: CV_PARSE_ERROR_CPP( "Missing \':\'" ); saveptr = endptr + 1; + if( endptr == ptr ) + CV_PARSE_ERROR_CPP( "An empty key" ); + do c = *--endptr; while( c == ' ' ); ++endptr; - if( endptr == ptr ) - CV_PARSE_ERROR_CPP( "An empty key" ); value_placeholder = fs->addNode(map_node, std::string(ptr, endptr - ptr), FileNode::NONE); ptr = saveptr; diff --git a/modules/core/test/test_io.cpp b/modules/core/test/test_io.cpp index 7e2a9d52d4..a6d44a3316 100644 --- a/modules/core/test/test_io.cpp +++ b/modules/core/test/test_io.cpp @@ -1673,6 +1673,21 @@ TEST(Core_InputOutput, FileStorage_free_file_after_exception) ASSERT_EQ(0, std::remove(fileName.c_str())); } +TEST(Core_InputOutput, FileStorage_YAML_empty_key) +{ + const std::string fileName = cv::tempfile("FileStorage_YAML_empty_key_test.yml"); + const std::string content = "%YAML:1.0\n---\nkey1: value1\n: 10\n"; + + std::fstream testFile; + testFile.open(fileName.c_str(), std::fstream::out); + if(!testFile.is_open()) FAIL(); + testFile << content; + testFile.close(); + + EXPECT_THROW(FileStorage(fileName, FileStorage::READ), cv::Exception); + ASSERT_EQ(0, std::remove(fileName.c_str())); +} + TEST(Core_InputOutput, FileStorage_write_to_sequence) { const std::vector formatExts = { ".yml", ".json", ".xml" };