Skip to content

Segfault when Net::Impl::forward(OutputArrayOfArrays outputBlobs, const std::vector<String>& outBlobNames) is called with empty outBlobNames #27352

@MRo47

Description

@MRo47

System Information

  • Ubuntu 24.04 x64
  • gcc 13.3.0
  • OpenCV 4.11.0

Detailed description

Calling the forward() method with empty outBlobNames causes a segfault with no helpful message to debug. The method works fine if correct output layer names are provided using net.getUnconnectedOutLayersNames();

[1]    11096 segmentation fault (core dumped)  ./bug

using GDB i could backtrace it to

#0  0x00007f6df3876ec9 in cv::dnn::dnn4_v20241223::Net::Impl::getLatestLayerPin(std::vector<cv::dnn::dnn4_v20241223::detail::LayerPin, std::allocator<cv::dnn::dnn4_v20241223::detail::LayerPin> > const&) const () from /opt/opencv-4.11.0/lib/libopencv_dnn.so.411
No symbol table info available.
#1  0x00007f6df388daf9 in cv::dnn::dnn4_v20241223::Net::Impl::forward(cv::_OutputArray const&, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&) () from /opt/opencv-4.11.0/lib/libopencv_dnn.so.411
No symbol table info available.
#2  0x00007f6df386dfed in cv::dnn::dnn4_v20241223::Net::forward(cv::_OutputArray const&, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&) () from /opt/opencv-4.11.0/lib/libopencv_dnn.so.411
No symbol table info available.

where getLatestLayerPin() has no check for empty, which results in de-referencing an invalid iterator.

LayerPin Net::Impl::getLatestLayerPin(const std::vector<LayerPin>& pins) const
{
    return *std::max_element(pins.begin(), pins.end());
}

Steps to reproduce

Compile and run the following sample

#include <opencv2/dnn.hpp>
#include <opencv2/imgcodecs.hpp>
#include <iostream>

int main()
{
    try
    {
        // load some model
        cv::dnn::Net net = cv::dnn::readNetFromONNX("yolo_nas_s.onnx");
        cv::Size network_input_size(640, 640);

        cv::Mat image(network_input_size, CV_8UC3);
        cv::randu(image, cv::Scalar(0, 0, 0), cv::Scalar(255, 255, 255));

        // backend agnostic
        net.setPreferableBackend(cv::dnn::DNN_BACKEND_OPENCV);
        net.setPreferableTarget(cv::dnn::DNN_TARGET_CPU);

        cv::Mat blob = cv::dnn::blobFromImage(
            image, 1, network_input_size, cv::Scalar(0, 0, 0), true, false, CV_32F);

        net.setInput(blob);

        std::vector<cv::Mat> outs;
        std::vector<cv::String> outNames; // Empty vector

        // ideally if i do the following, it works
        // outNames = net.getUnconnectedOutLayersNames();

        net.forward(outs, outNames); // <-- Segfaults here
    }
    catch (const cv::Exception &e)
    {
        // should throw this atleast to know what caused the error
        std::cerr << "OpenCV Exception: " << e.what() << std::endl;
        return 1;
    }
    catch (const std::exception &e)
    {
        std::cerr << "Standard Exception: " << e.what() << std::endl;
        return 1;
    }
    return 0;
}

NOTE: Uploading the onnx model is failing here, likely due to file size, ive tried zip version too

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

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions