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/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 6f8a39c5..1281e1be 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_TRUE( std::string(err.what()).find("zero-sized") != std::string::npos ); + } + +}