From a995cbf9ecafc44a73da17c193da982c2aa2492a Mon Sep 17 00:00:00 2001 From: alexander-g <3867427+alexander-g@users.noreply.github.com> Date: Sat, 30 Jan 2021 19:41:47 +0100 Subject: [PATCH 1/2] disallowing zero sized tensors --- src/Tensor.cpp | 6 +++++- test/TestOpTensorCreate.cpp | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Tensor.cpp b/src/Tensor.cpp index 214ac2eb..63e66705 100644 --- a/src/Tensor.cpp +++ b/src/Tensor.cpp @@ -268,10 +268,14 @@ Tensor::createBuffer() throw std::runtime_error("Kompute Tensor device is null"); } - this->mFreeBuffer = true; vk::BufferUsageFlags usageFlags = this->getBufferUsageFlags(); vk::DeviceSize bufferSize = this->memorySize(); + if(bufferSize<1){ + throw std::runtime_error("Kompute Tensor attempted to create a zero-sized buffer"); + } + + this->mFreeBuffer = true; SPDLOG_DEBUG("Kompute Tensor creating buffer with memory size: {}, and " "usage flags: {}", diff --git a/test/TestOpTensorCreate.cpp b/test/TestOpTensorCreate.cpp index 6f8a39c5..e25c1354 100644 --- a/test/TestOpTensorCreate.cpp +++ b/test/TestOpTensorCreate.cpp @@ -113,3 +113,21 @@ TEST(TestOpTensorCreate, NoErrorIfTensorFreedBefore) EXPECT_FALSE(tensorA->isInit()); EXPECT_FALSE(tensorB->isInit()); } + + +TEST(TestOpTensorCreate, ExceptionOnZeroSizeTensor) +{ + std::vector testVecA; + + std::shared_ptr tensorA{ new kp::Tensor(testVecA) }; + + kp::Manager mgr; + + try{ + mgr.evalOpDefault({ tensorA }); + } catch( const std::runtime_error& err ) { + // check exception + ASSERT_STREQ("Kompute Tensor attempted to create a zero-sized buffer", err.what() ); + } + +} From ae0c539bfc2ca87cf66ce8aea0b3a7d86b691061 Mon Sep 17 00:00:00 2001 From: alexander-g <3867427+alexander-g@users.noreply.github.com> Date: Sun, 31 Jan 2021 07:21:23 +0100 Subject: [PATCH 2/2] weaker test case and constructor comment --- src/include/kompute/Tensor.hpp | 2 +- test/TestOpTensorCreate.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/include/kompute/Tensor.hpp b/src/include/kompute/Tensor.hpp index 0a22eafc..d7b7e8f8 100644 --- a/src/include/kompute/Tensor.hpp +++ b/src/include/kompute/Tensor.hpp @@ -39,7 +39,7 @@ class Tensor * Default constructor with data provided which would be used to create the * respective vulkan buffer and memory. * - * @param data Vector of data that will be used by the tensor + * @param data Non-zero-sized vector of data that will be used by the tensor * @param tensorType Type for the tensor which is of type TensorTypes */ Tensor(const std::vector& data, diff --git a/test/TestOpTensorCreate.cpp b/test/TestOpTensorCreate.cpp index e25c1354..1281e1be 100644 --- a/test/TestOpTensorCreate.cpp +++ b/test/TestOpTensorCreate.cpp @@ -127,7 +127,7 @@ TEST(TestOpTensorCreate, ExceptionOnZeroSizeTensor) mgr.evalOpDefault({ tensorA }); } catch( const std::runtime_error& err ) { // check exception - ASSERT_STREQ("Kompute Tensor attempted to create a zero-sized buffer", err.what() ); + ASSERT_TRUE( std::string(err.what()).find("zero-sized") != std::string::npos ); } }