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

Coverity fixes #2411

Merged
merged 2 commits into from
Apr 17, 2024
Merged
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
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