From ed98c0a12c4c3d55b64073af2896c5d292915a36 Mon Sep 17 00:00:00 2001 From: JustinFung <835776509@qq.com> Date: Mon, 28 Jul 2025 13:43:53 +0800 Subject: [PATCH 1/4] Add input shapes validation --- src/net.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/net.cpp b/src/net.cpp index c257a36d1b41..029ba90891a3 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -2218,6 +2218,10 @@ int Extractor::input(int blob_index, const Mat& in) if (blob_index < 0 || blob_index >= (int)d->blob_mats.size()) return -1; + Mat shape = d->net->blobs()[blob_index].shape; + if (shape.total() && (shape.w != in.w) && (shape.h != in.h) && (shape.d != in.d) && (shape.c != in.c)) + return -1; + d->blob_mats[blob_index] = in; return 0; From cb67727457908a9a4d5fd3a860348dbcc406b5c7 Mon Sep 17 00:00:00 2001 From: JustinFung <835776509@qq.com> Date: Sat, 13 Sep 2025 09:13:03 +0800 Subject: [PATCH 2/4] Requested change --- src/net.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/net.cpp b/src/net.cpp index 07bf99c8941d..a5cc55b90986 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -2206,12 +2206,20 @@ int Extractor::input(int blob_index, const Mat& in) if (blob_index < 0 || blob_index >= (int)d->blob_mats.size()) return -1; - Mat shape = d->net->blobs()[blob_index].shape; - if (shape.total() && (shape.w != in.w) && (shape.h != in.h) && (shape.d != in.d) && (shape.c != in.c)) - return -1; + const Mat& shape = d->net->blobs()[blob_index].shape; + if (shape.total()) + { + if ((in.dims == 3 || in.dims == 4) && (shape.w != in.w || shape.h != in.h || shape.d != in.d || shape.c != in.c * in.elempack)) + return -1; - d->blob_mats[blob_index] = in; + if (in.dims == 2 && (shape.w != in.w || shape.h != in.h * in.elempack)) + return -1; + + if (in.dims == 1 && (shape.w != in.w * in.elempack)) + return -1; + } + d->blob_mats[blob_index] = in; return 0; } From 9069f00f134498d9445eddf73a7fbb26091f56e7 Mon Sep 17 00:00:00 2001 From: Deepdive543443 <83911295+Deepdive543443@users.noreply.github.com> Date: Sat, 13 Sep 2025 01:14:47 +0000 Subject: [PATCH 3/4] apply code-format changes --- src/net.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/net.cpp b/src/net.cpp index a5cc55b90986..1009f9255d40 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -2216,7 +2216,7 @@ int Extractor::input(int blob_index, const Mat& in) return -1; if (in.dims == 1 && (shape.w != in.w * in.elempack)) - return -1; + return -1; } d->blob_mats[blob_index] = in; From 614eaca125d5193e53a98117d891d7b7bb4eea95 Mon Sep 17 00:00:00 2001 From: JustinFung <835776509@qq.com> Date: Sat, 13 Sep 2025 15:39:11 +0800 Subject: [PATCH 4/4] Req --- src/net.cpp | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/net.cpp b/src/net.cpp index 1009f9255d40..58863db74812 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -2209,17 +2209,13 @@ int Extractor::input(int blob_index, const Mat& in) const Mat& shape = d->net->blobs()[blob_index].shape; if (shape.total()) { - if ((in.dims == 3 || in.dims == 4) && (shape.w != in.w || shape.h != in.h || shape.d != in.d || shape.c != in.c * in.elempack)) - return -1; - - if (in.dims == 2 && (shape.w != in.w || shape.h != in.h * in.elempack)) - return -1; - - if (in.dims == 1 && (shape.w != in.w * in.elempack)) + const Mat& in_shape = in.shape(); + if (shape.dims != in_shape.dims || shape.w != in_shape.w || shape.h != in_shape.h || shape.d != in_shape.d || shape.c != in_shape.c) return -1; } d->blob_mats[blob_index] = in; + return 0; }