Skip to content

Commit 08a24ba

Browse files
Merge pull request #26846 from MaximSmolskiy:fix_bug_with_int64_support_for_FileStorage
Fix bug with int64 support for FileStorage #26846 ### Pull Request Readiness Checklist Fix #26829, opencv/opencv-python#1078 In current implementation of `int64` support raw size of recorded integer is variable (`4` or `8` bytes depending on value). But then we iterate over nodes we need to know it exact value https://github.com/opencv/opencv/blob/dfad11aae7ef3b3a0643379266bc363b1a9c3d40/modules/core/src/persistence.cpp#L2596-L2609 Bug is that `rawSize` method still return `4` for any integer. I haven't figured out a way how to get variable raw size for integer in this method. I made raw size for integer is constant and equal to `8`. Yes, after this patch memory consumption for integers will increase, but I don't know a better way to do it yet. At least this fixes bug and implementation becomes more correct See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake
1 parent 0049cde commit 08a24ba

File tree

2 files changed

+61
-12
lines changed

2 files changed

+61
-12
lines changed

modules/core/src/persistence.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2441,7 +2441,7 @@ size_t FileNode::rawSize() const
24412441
p += 4;
24422442
size_t sz0 = (size_t)(p - p0);
24432443
if( tp == INT )
2444-
return sz0 + 4;
2444+
return sz0 + 8;
24452445
if( tp == REAL )
24462446
return sz0 + 8;
24472447
if( tp == NONE )
@@ -2475,13 +2475,7 @@ void FileNode::setValue( int type, const void* value, int len )
24752475
sz += 4;
24762476

24772477
if( type == INT )
2478-
{
2479-
int64_t ival = *(const int64_t*)value;
2480-
if (ival > INT_MAX || ival < INT_MIN)
2481-
sz += 8;
2482-
else
2483-
sz += 4;
2484-
}
2478+
sz += 8;
24852479
else if( type == REAL )
24862480
sz += 8;
24872481
else if( type == STRING )
@@ -2502,10 +2496,7 @@ void FileNode::setValue( int type, const void* value, int len )
25022496
if( type == INT )
25032497
{
25042498
int64_t ival = *(const int64_t*)value;
2505-
if (sz > 8)
2506-
writeInt(p, ival);
2507-
else
2508-
writeInt(p, static_cast<int>(ival));
2499+
writeInt(p, ival);
25092500
}
25102501
else if( type == REAL )
25112502
{

modules/core/test/test_io.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2003,6 +2003,64 @@ TEST(Core_InputOutput, FileStorage_invalid_attribute_value_regression_25946)
20032003
ASSERT_EQ(0, std::remove(fileName.c_str()));
20042004
}
20052005

2006+
// see https://github.com/opencv/opencv/issues/26829
2007+
TEST(Core_InputOutput, FileStorage_int64_26829)
2008+
{
2009+
String content =
2010+
"%YAML:1.0\n"
2011+
"String1: string1\n"
2012+
"IntMin: -2147483648\n"
2013+
"String2: string2\n"
2014+
"Int64Min: -9223372036854775808\n"
2015+
"String3: string3\n"
2016+
"IntMax: 2147483647\n"
2017+
"String4: string4\n"
2018+
"Int64Max: 9223372036854775807\n"
2019+
"String5: string5\n";
2020+
2021+
FileStorage fs(content, FileStorage::READ | FileStorage::MEMORY);
2022+
2023+
{
2024+
std::string str;
2025+
2026+
fs["String1"] >> str;
2027+
EXPECT_EQ(str, "string1");
2028+
2029+
fs["String2"] >> str;
2030+
EXPECT_EQ(str, "string2");
2031+
2032+
fs["String3"] >> str;
2033+
EXPECT_EQ(str, "string3");
2034+
2035+
fs["String4"] >> str;
2036+
EXPECT_EQ(str, "string4");
2037+
2038+
fs["String5"] >> str;
2039+
EXPECT_EQ(str, "string5");
2040+
}
2041+
2042+
{
2043+
int value;
2044+
2045+
fs["IntMin"] >> value;
2046+
EXPECT_EQ(value, INT_MIN);
2047+
2048+
fs["IntMax"] >> value;
2049+
EXPECT_EQ(value, INT_MAX);
2050+
}
2051+
2052+
2053+
{
2054+
int64_t value;
2055+
2056+
fs["Int64Min"] >> value;
2057+
EXPECT_EQ(value, INT64_MIN);
2058+
2059+
fs["Int64Max"] >> value;
2060+
EXPECT_EQ(value, INT64_MAX);
2061+
}
2062+
}
2063+
20062064
template <typename T>
20072065
T fsWriteRead(const T& expectedValue, const char* ext)
20082066
{

0 commit comments

Comments
 (0)