Skip to content

Commit

Permalink
Coverity fixes (#2411)
Browse files Browse the repository at this point in the history
*Fix uninitialized value issue in pytensor
*Fix minor leak in Mediapipe fork

Changes in Mediapipe fork:
openvinotoolkit/mediapipe#72

ID:CVS-136076
  • Loading branch information
atobiszei committed Apr 17, 2024
1 parent a300728 commit 0c2fe0e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ http_archive(
git_repository(
name = "mediapipe",
remote = "https://github.com/openvinotoolkit/mediapipe",
commit = "0d5cbdbbee501b796217c7520ac0f2fd94920657", # Working log_level and validate (#69)
commit = "193d4089f2511ba11c918c1861f9f79b3da24c23", # Fix leak (#72)
)

# DEV mediapipe 1 source - adjust local repository path for build
Expand Down
8 changes: 8 additions & 0 deletions src/predict_request_validation_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,15 @@ Status validate(
template <typename T>
static bool computeExpectedBufferSizeReturnFalseIfOverflow(const std::vector<T>& shape, const size_t& itemsize, size_t& expectedBufferSize) {
expectedBufferSize = 1;
if (itemsize == 0) {
expectedBufferSize = 0;
return true;
}
for (const T& dim : shape) {
if (dim == 0) {
expectedBufferSize = 0;
return true;
}
if (expectedBufferSize > std::numeric_limits<size_t>::max() / dim)
return false;
expectedBufferSize *= dim;
Expand Down
4 changes: 3 additions & 1 deletion src/python/ovms_py_tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@ OvmsPyTensor::OvmsPyTensor(const std::string& name, const std::vector<py::ssize_
if (allocate) {
ownedDataPtr = std::make_unique<char[]>(size);
ptr = this->ownedDataPtr.get();
} else {
ptr = nullptr;
}
}

OvmsPyTensor::OvmsPyTensor(const std::string& name, void* data, const std::vector<py::ssize_t>& shape, const std::string& datatype, py::ssize_t size, bool copy) :
OvmsPyTensor(name, shape, datatype, size, copy) {
OvmsPyTensor(name, shape, datatype, size, /*allocate=*/copy) {
if (copy) {
memcpy(this->ownedDataPtr.get(), data, size);
} else {
Expand Down
21 changes: 21 additions & 0 deletions src/test/predict_validation_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2111,4 +2111,25 @@ TYPED_TEST(PredictValidationStringNativeTest, string_not_allowed_with_demultiple
EXPECT_EQ(status, ovms::StatusCode::INVALID_NO_OF_SHAPE_DIMENSIONS);
}

#define VERIFY_COMPUTE_BUFFER_SIZE(SHAPE, ELEMENT_SIZE, WILL_NOT_OVERFLOW, EXPECTED_BYTES) \
{ \
size_t elementSize = ELEMENT_SIZE; \
std::vector<int> rawShape SHAPE; \
size_t expectedBytes = 12412412; \
bool result = ovms::request_validation_utils::computeExpectedBufferSizeReturnFalseIfOverflow<int>(rawShape, elementSize, expectedBytes); \
EXPECT_EQ(WILL_NOT_OVERFLOW, result); \
if (WILL_NOT_OVERFLOW) { \
EXPECT_EQ(EXPECTED_BYTES, expectedBytes); \
} \
}

TEST(PredictRequestUtilsTest, ComputeExpectedBufferSize) {
VERIFY_COMPUTE_BUFFER_SIZE(({1, 3, 4}), 1, true, 12);
VERIFY_COMPUTE_BUFFER_SIZE(({1, 3, 4, 0}), 1, true, 0);
VERIFY_COMPUTE_BUFFER_SIZE(({1, 3, 4, 1}), 0, true, 0);
SPDLOG_INFO("int numeric limit:{}", std::numeric_limits<int>::max());
SPDLOG_INFO("size_t numeric limit:{}", std::numeric_limits<size_t>::max());
VERIFY_COMPUTE_BUFFER_SIZE(({1, 9, std::numeric_limits<int>::max(), std::numeric_limits<size_t>::max() / std::numeric_limits<int>::max() / 8}), 1, false, 0);
}

#pragma GCC diagnostic pop

0 comments on commit 0c2fe0e

Please sign in to comment.