1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-21 19:33:03 +04:00

Merge pull request #26852 from MaximSmolskiy:fix_bug_with_int64_support_for_FileStorage_5x

Fix bug with int64 support for FileStorage
This commit is contained in:
Alexander Smorkalov
2025-02-05 10:15:36 +03:00
committed by GitHub
2 changed files with 62 additions and 18 deletions
+4 -18
View File
@@ -2468,7 +2468,7 @@ size_t FileNode::rawSize() const
p += 4;
size_t sz0 = (size_t)(p - p0);
if( tp == INT )
return sz0 + 4;
return sz0 + 8;
if( tp == REAL )
return sz0 + 8;
if( tp == NONE )
@@ -2502,13 +2502,7 @@ void FileNode::setValue( int type, const void* value, int len )
sz += 4;
if( type == INT )
{
int64_t ival = *(const int64_t*)value;
if (ival > INT_MAX || ival < INT_MIN)
sz += 8;
else
sz += 4;
}
sz += 8;
else if( type == REAL )
sz += 8;
else if( type == STRING )
@@ -2529,10 +2523,7 @@ void FileNode::setValue( int type, const void* value, int len )
if( type == INT )
{
int64_t ival = *(const int64_t*)value;
if (sz > 8)
writeInt(p, ival);
else
writeInt(p, static_cast<int>(ival));
writeInt(p, ival);
}
else if( type == REAL )
{
@@ -2683,7 +2674,7 @@ FileNodeIterator& FileNodeIterator::readRaw( const String& fmt, void* _data0, si
offset = alignSize( offset, elem_size );
uchar* data = data0 + offset;
for( int i = 0; i < count; i++ )
for( int i = 0; i < count; i++, ++(*this) )
{
FileNode node = *(*this);
if( node.isInt() )
@@ -2807,11 +2798,6 @@ FileNodeIterator& FileNodeIterator::readRaw( const String& fmt, void* _data0, si
}
else
CV_Error( Error::StsError, "readRawData can only be used to read plain sequences of numbers" );
++(*this);
if (elem_type == CV_64S)
{
ofs += 4;
}
}
offset = (int)(data - data0);
}
+58
View File
@@ -2022,6 +2022,64 @@ TEST(Core_InputOutput, FileStorage_invalid_attribute_value_regression_25946)
ASSERT_EQ(0, std::remove(fileName.c_str()));
}
// see https://github.com/opencv/opencv/issues/26829
TEST(Core_InputOutput, FileStorage_int64_26829)
{
String content =
"%YAML:1.0\n"
"String1: string1\n"
"IntMin: -2147483648\n"
"String2: string2\n"
"Int64Min: -9223372036854775808\n"
"String3: string3\n"
"IntMax: 2147483647\n"
"String4: string4\n"
"Int64Max: 9223372036854775807\n"
"String5: string5\n";
FileStorage fs(content, FileStorage::READ | FileStorage::MEMORY);
{
std::string str;
fs["String1"] >> str;
EXPECT_EQ(str, "string1");
fs["String2"] >> str;
EXPECT_EQ(str, "string2");
fs["String3"] >> str;
EXPECT_EQ(str, "string3");
fs["String4"] >> str;
EXPECT_EQ(str, "string4");
fs["String5"] >> str;
EXPECT_EQ(str, "string5");
}
{
int value;
fs["IntMin"] >> value;
EXPECT_EQ(value, INT_MIN);
fs["IntMax"] >> value;
EXPECT_EQ(value, INT_MAX);
}
{
int64_t value;
fs["Int64Min"] >> value;
EXPECT_EQ(value, INT64_MIN);
fs["Int64Max"] >> value;
EXPECT_EQ(value, INT64_MAX);
}
}
template <typename T>
T fsWriteRead(const T& expectedValue, const char* ext)
{