Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dnn: Fix dangling pointers returned by GetLayerNames #927

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Dockerfile.opencv-openvino
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ LABEL maintainer="hybridgroup"
ENV DEBIAN_FRONTEND=noninteractive
USER root
RUN apt-get update && apt-get install -y --no-install-recommends \
git build-essential cmake pkg-config unzip libgtk2.0-dev \
git build-essential cmake pkg-config unzip libgtk2.0-dev libgtk-3-0 \
wget curl ca-certificates libcurl4-openssl-dev libssl-dev \
libavcodec-dev libavformat-dev libswscale-dev libtbb2 libtbb-dev \
libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev && \
libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev libcanberra-gtk-module \
libcanberra-gtk3-module && \
rm -rf /var/lib/apt/lists/*

ARG OPENCV_VERSION="4.6.0"
Expand Down Expand Up @@ -39,7 +40,6 @@ RUN curl -Lo opencv.zip https://github.com/opencv/opencv/archive/${OPENCV_VERSIO
-D BUILD_opencv_python3=NO \
-D WITH_TBB=ON \
-D WITH_OPENVINO=1 \
-D ENABLE_FAST_MATH=1 \
-D OPENCV_GENERATE_PKGCONFIG=ON .. && \
make -j $(nproc --all) && \
make preinstall && make install && ldconfig && \
Expand Down
31 changes: 27 additions & 4 deletions dnn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,38 @@ void Net_GetUnconnectedOutLayers(Net net, IntVector* res) {

void Net_GetLayerNames(Net net, CStrings* names) {
std::vector< cv::String > cstrs(net->getLayerNames());
const char **strs = new const char*[cstrs.size()];

for (size_t i = 0; i < cstrs.size(); ++i) {
strs[i] = cstrs[i].c_str();
size_t totalStrLen = 0;
for (cv::String str : cstrs) {
totalStrLen += str.size();
}

// Compute 1D buffer size required to store
// 2D array of null-terminated strings
size_t numStrings = cstrs.size();
size_t bufferLen = numStrings * sizeof(char*) + (totalStrLen + numStrings) * sizeof(char);

const char **strs = (const char**)new char[bufferLen];
memset(strs, 0, bufferLen);

char* it = (char*)(strs + cstrs.size());
const char* end = (char*)(strs) + bufferLen;

for (size_t i = 0; i < numStrings; ++i, ++it) {
strs[i] = it;
size_t strlen = cstrs[i].size();

// Avoid buffer overrun
if(end < it + strlen + 1) {
break;
}

memcpy(it, cstrs[i].c_str(), strlen);
it += strlen;
}

names->length = cstrs.size();
names->strs = strs;
return;
}

Mat Net_BlobFromImage(Mat image, double scalefactor, Size size, Scalar mean, bool swapRB,
Expand Down
23 changes: 21 additions & 2 deletions dnn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,27 @@ func checkNet(t *testing.T, net Net) {
t.Errorf("Invalid len layer names in ReadNet test: %d\n", len(lnames))
}

if len(lnames) == 142 && lnames[1] != "conv1/relu_7x7" {
t.Errorf("Invalid layer name in ReadNet test: %s\n", lnames[1])
m := map[int]string{
0: "conv1/7x7_s2",
10: "inception_3a/1x1",
20: "inception_3a/pool",
30: "inception_3b/5x5_reduce",
40: "inception_4a/relu_1x1",
50: "inception_4a/pool_proj",
60: "inception_4b/relu_5x5_reduce",
70: "inception_4c/relu_3x3_reduce",
80: "inception_4c/output",
90: "inception_4d/relu_5x5",
100: "inception_4e/relu_3x3",
110: "inception_5a/1x1",
120: "inception_5a/pool",
130: "inception_5b/5x5_reduce",
140: "loss3/classifier"}

for k, v := range m {
if lnames[k] != v {
t.Errorf("Invalid layer name in ReadNet test: \"%s\" (expected=\"%s\")\n", lnames[k], v)
}
}

prob := net.ForwardLayers([]string{"prob"})
Expand Down