From 00112bbe104efc086fce141169e9bbe042d56a23 Mon Sep 17 00:00:00 2001 From: Philipp Hasper Date: Thu, 7 Jul 2016 09:55:30 +0200 Subject: [PATCH] persistence: fixing crash with space-only values --- modules/core/src/persistence.cpp | 2 +- modules/core/test/test_io.cpp | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/modules/core/src/persistence.cpp b/modules/core/src/persistence.cpp index 2fc4f9f..4d99a4a 100644 --- a/modules/core/src/persistence.cpp +++ b/modules/core/src/persistence.cpp @@ -1852,7 +1852,7 @@ icvYMLWriteString( CvFileStorage* fs, const char* key, if( quote || len == 0 || str[0] != str[len-1] || (str[0] != '\"' && str[0] != '\'') ) { - int need_quote = quote || len == 0; + int need_quote = quote || len == 0 || str[0] == ' '; data = buf; *data++ = '\"'; for( i = 0; i < len; i++ ) diff --git a/modules/core/test/test_io.cpp b/modules/core/test/test_io.cpp index 1367776..f2c53dc 100644 --- a/modules/core/test/test_io.cpp +++ b/modules/core/test/test_io.cpp @@ -578,6 +578,22 @@ TEST(Core_InputOutput, FileStorageKey) ASSERT_STREQ(f.releaseAndGetString().c_str(), expected.c_str()); } +TEST(Core_InputOutput, FileStorageSpaces) +{ + cv::FileStorage f("dummy.yml", cv::FileStorage::WRITE | cv::FileStorage::MEMORY); + const int valueCount = 5; + std::string values[5] = { "", " ", " ", " a", " some string" }; + for (size_t i = 0; i < valueCount; i++) { + EXPECT_NO_THROW(f << cv::format("key%d", i) << values[i]); + } + cv::FileStorage f2(f.releaseAndGetString(), cv::FileStorage::READ | cv::FileStorage::MEMORY); + std::string valuesRead[valueCount]; + for (size_t i = 0; i < valueCount; i++) { + EXPECT_NO_THROW(f2[cv::format("key%d", i)] >> valuesRead[i]); + ASSERT_STREQ(values[i].c_str(), valuesRead[i].c_str()); + } +} + TEST(Core_InputOutput, filestorage_yml_compatibility) { // TODO: -- 2.7.4