OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[core] Fix out-of-bounds access in reference gather_nd (#38296)
Pawel Raaszdb5e03ddf37b
diff --git a/src/core/reference/include/openvino/reference/gather_nd.hpp b/src/core/reference/include/openvino/reference/gather_nd.hppindex 525a653c049..aa75d6cc7ee 100644--- a/src/core/reference/include/openvino/reference/gather_nd.hpp+++ b/src/core/reference/include/openvino/reference/gather_nd.hpp@@ -6,8 +6,10 @@#include <algorithm>#include <cassert>+#include <cstddef>#include <numeric>+#include "openvino/core/shape_util.hpp"#include "openvino/reference/utils/coordinate_transform.hpp"#include "utils/span.hpp"@@ -93,8 +95,8 @@ void gather_nd(const T* const params,size_t input_slice_offset = input_batch_offset;for (size_t c = 0; c != coordinates_size; ++c) {const auto i_c = slice_coordinates[c];- const auto index = i_c < 0 ? k_1_params[c] + i_c : i_c;- input_slice_offset += index * indices_offsets[c];+ const auto index = ov::util::normalize_shape_index(static_cast<std::ptrdiff_t>(i_c), k_1_params[c]);+ input_slice_offset += static_cast<size_t>(index) * indices_offsets[c];}const auto output_slice_offset = output_batch_offset + slice * slice_size;std::copy(next(params, input_slice_offset),diff --git a/src/plugins/template/tests/functional/op_reference/gather_nd.cpp b/src/plugins/template/tests/functional/op_reference/gather_nd.cppindex 999f71205e8..0b29f1a5a5a 100644--- a/src/plugins/template/tests/functional/op_reference/gather_nd.cpp+++ b/src/plugins/template/tests/functional/op_reference/gather_nd.cpp@@ -7,6 +7,7 @@#include <gtest/gtest.h>#include "base_reference_test.hpp"+#include "common_test_utils/test_assertions.hpp"using namespace reference_tests;using namespace ov;@@ -415,4 +416,19 @@ INSTANTIATE_TEST_SUITE_P(smoke_GatherND_With_Hardcoded_Refs,ReferenceGatherND8Test,testing::ValuesIn(generateCombinedParams_v8()),ReferenceGatherND8Test::getTestCaseName);++class ReferenceGatherND8LayerTestNegative : public ReferenceGatherND8Test {};++TEST_P(ReferenceGatherND8LayerTestNegative, IndicesOutOfRangeThrows) {+ OV_EXPECT_THROW_HAS_SUBSTRING(Exec(), ov::Exception, "Accessing out-of-range dimension");+}++INSTANTIATE_TEST_SUITE_P(+ smoke_GatherNDNegative,+ ReferenceGatherND8LayerTestNegative,+ ::testing::Values(GatherNDParams(reference_tests::Tensor(element::f32, {16, 64}, std::vector<float>(16 * 64, 1.0f)),+ reference_tests::Tensor(element::i64, {1, 1}, std::vector<int64_t>{40}),+ 0,+ reference_tests::Tensor(element::f32, {1, 64}, std::vector<float>(64, 0.0f)))),+ ReferenceGatherND8LayerTestNegative::getTestCaseName);} // namespace