Skip to content

Bug in cv::FileStorage when the XML contains integer values larger INT_MAX #26829

@sbreuss

Description

@sbreuss

System Information

OpenCV version: 4.11.0
Operating System / Platform: Windows
Compiler: msvc 19.42.34436.0 (Visual Studio 2022)

Detailed description

There seems to be a problem in the cv::FileStorage when it contains integers larger than INT_MAX.

When reading the Name node from an XML file like this one below, we get an error in modules\core\src\persistence.cpp:2233.
The XML reads fine for OpenCV up to version 4.10.0, or with integer values smaller than INT_MAX.

<?xml version="1.0"?>
    <opencv_storage>
      <A>
        <Value>2147483648</Value>
        <Name>"image"</Name>
      </A>
    </opencv_storage>

Output built with OpenCV 4.11.0

a is ok
b is ok
OpenCV(4.11.0) modules\core\src\persistence.cpp:2233: error: (-215:Assertion failed) key2 < fs->str_hash_data.size() in function 'cv::FileNode::operator []'

Output build with OpenCV 4.10.0

a is ok
b is ok
c is ok

Steps to reproduce

#include <iostream>
#include <opencv2/core/persistence.hpp>

int main() {
    auto example_a = R"(<?xml version="1.0"?>
    <opencv_storage>
      <A>
        <Value>2147483648</Value>
      </A>
    </opencv_storage>)";

    auto example_b = R"(<?xml version="1.0"?>
    <opencv_storage>
      <A>
        <Value>2147483647</Value>
        <Name>"image"</Name>
      </A>
    </opencv_storage>)";

    auto example_c = R"(<?xml version="1.0"?>
    <opencv_storage>
      <A>
        <Value>2147483648</Value>
        <Name>"image"</Name>
      </A>
    </opencv_storage>)";

    std::string name;
    cv::FileStorage fs;

    try {
        fs = cv::FileStorage(example_a, cv::FileStorage::READ | cv::FileStorage::MEMORY | cv::FileStorage::FORMAT_XML);
        fs["A"]["Name"] >> name;
        std::cout << "a is ok" << std::endl;
    } catch (std::exception &e) {
        std::cout << e.what() << std::endl;
    }

    try {
        fs = cv::FileStorage(example_b, cv::FileStorage::READ | cv::FileStorage::MEMORY | cv::FileStorage::FORMAT_XML);
        fs["A"]["Name"] >> name;
        std::cout << "b is ok" << std::endl;
    } catch (std::exception &e) {
        std::cout << e.what() << std::endl;
    }

    try {
        fs = cv::FileStorage(example_c, cv::FileStorage::READ | cv::FileStorage::MEMORY | cv::FileStorage::FORMAT_XML);
        fs["A"]["Name"] >> name;
        std::cout << "c is ok" << std::endl;
    } catch (std::exception &e) {
        std::cout << e.what() << std::endl;
    }

    return 0;
}

Issue submission checklist

  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files (videos, images, onnx, etc)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions