diff --git a/modules/core/src/persistence.cpp b/modules/core/src/persistence.cpp index 826168954b..47c035d284 100644 --- a/modules/core/src/persistence.cpp +++ b/modules/core/src/persistence.cpp @@ -1837,6 +1837,10 @@ ushort FileStorage::Impl::Base64Decoder::getUInt16() { return val; } +unsigned FileStorage::Impl::Base64Decoder::getUInt32() { + return (unsigned)getInt32(); +} + int FileStorage::Impl::Base64Decoder::getInt32() { size_t sz = decoded.size(); if (ofs + 4 > sz && !readMore(4)) @@ -1914,7 +1918,7 @@ char *FileStorage::Impl::parseBase64(char *ptr, int indent, FileNode &collection ival = (short) base64decoder.getUInt16(); break; case CV_32U: - ival = base64decoder.getInt32(); + ival = base64decoder.getUInt32(); break; case CV_32S: ival = base64decoder.getInt32(); @@ -2743,7 +2747,7 @@ FileNodeIterator& FileNodeIterator::readRaw( const String& fmt, void* _data0, si FileNode node = *(*this); if( node.isInt() ) { - int64_t ival = static_cast(elem_size == 8 ? (int64_t)node : (int)node); + int64_t ival = (int64_t)node; switch( elem_type ) { case CV_8U: diff --git a/modules/core/src/persistence_impl.hpp b/modules/core/src/persistence_impl.hpp index b1c922e082..aae7ba351e 100644 --- a/modules/core/src/persistence_impl.hpp +++ b/modules/core/src/persistence_impl.hpp @@ -151,6 +151,8 @@ public: ushort getUInt16(); + unsigned getUInt32(); + int getInt32(); int64_t getInt64(); diff --git a/modules/core/test/test_io.cpp b/modules/core/test/test_io.cpp index b6bddf7c24..c156a4febc 100644 --- a/modules/core/test/test_io.cpp +++ b/modules/core/test/test_io.cpp @@ -2662,4 +2662,28 @@ TEST(Core_InputOutput, filestorage_read_uppercase_exponent) } } +// CV_32U values above INT_MAX must survive saving and loading, in every format +TEST(Core_InputOutput, filestorage_32u_large_values) +{ + const unsigned values[] = { 0u, 1u, 2147483647u, 2147483648u, 4294967295u }; + const Mat src(1, 5, CV_32U, (void*)values); + + for (const char* ext : { ".yml", ".xml", ".json" }) + { + for (int base64 : { 0, (int)FileStorage::WRITE_BASE64 }) + { + FileStorage writer(ext, FileStorage::WRITE | FileStorage::MEMORY | base64); + writer << "m" << src; + const std::string text = writer.releaseAndGetString(); + + FileStorage reader(text, FileStorage::READ | FileStorage::MEMORY); + Mat dst; + reader["m"] >> dst; + + EXPECT_EQ(CV_32U, dst.type()) << ext << " base64=" << base64; + EXPECT_EQ(0, cv::norm(src, dst, NORM_INF)) << ext << " base64=" << base64; + } + } +} + }} // namespace