diff --git a/modules/core/src/persistence_json.cpp b/modules/core/src/persistence_json.cpp index 852c79e265..caa1c71919 100644 --- a/modules/core/src/persistence_json.cpp +++ b/modules/core/src/persistence_json.cpp @@ -684,7 +684,7 @@ public: ptr++; CV_PERSISTENCE_CHECK_END_OF_BUFFER_BUG_CPP(); } - if (*ptr == '.' || *ptr == 'e') + if (*ptr == '.' || *ptr == 'e' || *ptr == 'E') { double fval = fs->strtod( beg, &ptr ); CV_PERSISTENCE_CHECK_END_OF_BUFFER_BUG_CPP(); diff --git a/modules/core/src/persistence_xml.cpp b/modules/core/src/persistence_xml.cpp index b2fa326544..007ef621ec 100644 --- a/modules/core/src/persistence_xml.cpp +++ b/modules/core/src/persistence_xml.cpp @@ -558,7 +558,7 @@ public: endptr = ptr + (c == '-' || c == '+'); while( cv_isdigit(*endptr) ) endptr++; - if( *endptr == '.' || *endptr == 'e' ) + if( *endptr == '.' || *endptr == 'e' || *endptr == 'E' ) { double fval = fs->strtod( ptr, &endptr ); elem->setValue(FileNode::REAL, &fval); diff --git a/modules/core/src/persistence_yml.cpp b/modules/core/src/persistence_yml.cpp index f62c3f7a37..a79195e6aa 100644 --- a/modules/core/src/persistence_yml.cpp +++ b/modules/core/src/persistence_yml.cpp @@ -568,7 +568,7 @@ public: endptr = ptr + (c == '-' || c == '+'); while( cv_isdigit(*endptr) ) endptr++; - if( *endptr == '.' || *endptr == 'e' ) + if( *endptr == '.' || *endptr == 'e' || *endptr == 'E' ) { force_real: double fval = fs->strtod( ptr, &endptr ); diff --git a/modules/core/test/test_io.cpp b/modules/core/test/test_io.cpp index 807d2b1cff..b6bddf7c24 100644 --- a/modules/core/test/test_io.cpp +++ b/modules/core/test/test_io.cpp @@ -2635,4 +2635,31 @@ TEST(Core_InputOutput, YAML_Compatibility) remove(filename.c_str()); } +TEST(Core_InputOutput, filestorage_read_uppercase_exponent) +{ + // JSON, YAML and XML all allow an upper-case exponent marker. A number whose + // mantissa has no '.' was only read as a real for a lower-case 'e', so "1E5" + // was read as the integer 1 followed by stray "E5". + const char* contents[] = { + "{\n \"a\": 1E5,\n \"b\": -3E-2,\n \"c\": 1e5\n}\n", + "%YAML:1.0\n---\na: 1E5\nb: -3E-2\nc: 1e5\n", + "\n\n1E5\n-3E-2\n1e5\n\n", + }; + for (const char* content : contents) + { + SCOPED_TRACE(content); + double a = 0, b = 0, c = 0; + ASSERT_NO_THROW({ + FileStorage fs(content, FileStorage::READ | FileStorage::MEMORY); + EXPECT_TRUE(fs["a"].isReal()); + fs["a"] >> a; + fs["b"] >> b; + fs["c"] >> c; + }); + EXPECT_EQ(1e5, a); + EXPECT_EQ(-3e-2, b); + EXPECT_EQ(1e5, c); + } +} + }} // namespace