[coverity] fix coverity issue
authorDonghyeon Jeong <dhyeon.jeong@samsung.com>
Wed, 14 Aug 2024 11:49:11 +0000 (20:49 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Mon, 19 Aug 2024 07:28:55 +0000 (16:28 +0900)
This PR resolves the coverity issues of resource leak, unreachable code, and missing break.

**Changes proposed in this PR:**
- use static arrays instead of dynamic allocation to avoid resource leaks.
- remove unreachable code and add missing break statement.

**Self-evaluation:**
1. Build test: [X]Passed [ ]Failed [ ]Skipped
2. Run test:   [X]Passed [ ]Failed [ ]Skipped

Signed-off-by: Donghyeon Jeong <dhyeon.jeong@samsung.com>
nntrainer/tensor/char_tensor.cpp
nntrainer/tensor/tensor_base.cpp
test/unittest/unittest_nntrainer_tensor.cpp
test/unittest/unittest_nntrainer_tensor_nhwc.cpp

index 825ec6d2edb9863f8d77d3bb67cf4dc7a2bc035c..ec70943b84ef6dd6d6615c1ac03a968affcdf36f 100644 (file)
@@ -245,6 +245,7 @@ void CharTensor::copyData(const Tensor &from) {
   switch (from.getDataType()) {
   case ml::train::TensorDim::DataType::QINT8:
     copy(from.getData());
+    break;
   default:
     throw std::invalid_argument("Error: Unsupported data type");
     break;
index 8471dcb6e2bc0a802e7acac7f4cf0f1d52b32edc..0711504f8bcbe4e8659f7c192392d71b7f327a32 100644 (file)
@@ -370,21 +370,18 @@ Tensor TensorBase::multiply_strided(Tensor const &m, Tensor &output,
   throw std::invalid_argument("Tensor::multiply_strided() is currently not "
                               "supported in tensor data type " +
                               getStringDataType());
-  return output;
 }
 
 int TensorBase::multiply_i(float const &value) {
   throw std::invalid_argument(
     "Tensor::multiply_i() is currently not supported in tensor data type " +
     getStringDataType());
-  return ML_ERROR_NOT_SUPPORTED;
 }
 
 Tensor &TensorBase::multiply(float const &value, Tensor &output) const {
   throw std::invalid_argument(
     "Tensor::multiply() is currently not supported in tensor data type " +
     getStringDataType());
-  return output;
 }
 
 Tensor &TensorBase::multiply(Tensor const &m, Tensor &output,
@@ -392,21 +389,18 @@ Tensor &TensorBase::multiply(Tensor const &m, Tensor &output,
   throw std::invalid_argument(
     "Tensor::multiply() is currently not supported in tensor data type " +
     getStringDataType());
-  return output;
 }
 
 Tensor &TensorBase::divide(float const &value, Tensor &output) const {
   throw std::invalid_argument(
     "Tensor::divide() is currently not supported in tensor data type " +
     getStringDataType());
-  return output;
 }
 
 Tensor &TensorBase::divide(Tensor const &m, Tensor &output) const {
   throw std::invalid_argument(
     "Tensor::divide() is currently not supported in tensor data type " +
     getStringDataType());
-  return output;
 }
 
 Tensor &TensorBase::add_strided(Tensor const &input, Tensor &output,
@@ -414,14 +408,12 @@ Tensor &TensorBase::add_strided(Tensor const &input, Tensor &output,
   throw std::invalid_argument(
     "Tensor::add_strided() is currently not supported in tensor data type " +
     getStringDataType());
-  return output;
 }
 
 int TensorBase::add_i(Tensor const &m, Tensor &output, float const alpha) {
   throw std::invalid_argument(
     "Tensor::add_i() is currently not supported in tensor data type " +
     getStringDataType());
-  return ML_ERROR_NOT_SUPPORTED;
 }
 
 int TensorBase::add_i_partial(unsigned int len, unsigned int addr_idx,
@@ -430,14 +422,12 @@ int TensorBase::add_i_partial(unsigned int len, unsigned int addr_idx,
   throw std::invalid_argument(
     "Tensor::add_i_partial() is currently not supported in tensor data type " +
     getStringDataType());
-  return ML_ERROR_NOT_SUPPORTED;
 }
 
 Tensor &TensorBase::add(float const &value, Tensor &output) const {
   throw std::invalid_argument(
     "Tensor::add() is currently not supported in tensor data type " +
     getStringDataType());
-  return output;
 }
 
 Tensor &TensorBase::add(Tensor const &m, Tensor &output,
@@ -445,14 +435,12 @@ Tensor &TensorBase::add(Tensor const &m, Tensor &output,
   throw std::invalid_argument(
     "Tensor::add() is currently not supported in tensor data type " +
     getStringDataType());
-  return output;
 }
 
 Tensor &TensorBase::subtract(float const &value, Tensor &output) const {
   throw std::invalid_argument(
     "Tensor::subtract() is currently not supported in tensor data type " +
     getStringDataType());
-  return output;
 }
 
 void TensorBase::sum_by_batch(Tensor &output) const {
@@ -466,28 +454,24 @@ Tensor &TensorBase::sum(unsigned int axis, Tensor &output, float alpha,
   throw std::invalid_argument(
     "Tensor::sum() is currently not supported in tensor data type " +
     getStringDataType());
-  return output;
 }
 
 float TensorBase::l2norm() const {
   throw std::invalid_argument(
     "Tensor::l2norm() is currently not supported in tensor data type " +
     getStringDataType());
-  return ML_ERROR_NOT_SUPPORTED;
 }
 
 Tensor &TensorBase::pow(float exponent, Tensor &output) const {
   throw std::invalid_argument(
     "Tensor::pow() is currently not supported in tensor data type " +
     getStringDataType());
-  return output;
 }
 
 Tensor &TensorBase::erf(Tensor &output) const {
   throw std::invalid_argument(
     "Tensor::erf() is currently not supported in tensor data type " +
     getStringDataType());
-  return output;
 }
 
 void TensorBase::sin(Tensor &out, float alpha) {
@@ -513,7 +497,6 @@ Tensor &TensorBase::dot(Tensor const &input, Tensor &output, bool trans,
   throw std::invalid_argument(
     "Tensor::dot() is currently not supported in tensor data type " +
     getStringDataType());
-  return output;
 }
 
 void TensorBase::dropout_mask(float dropout) {
@@ -538,8 +521,6 @@ std::vector<Tensor> TensorBase::split(std::vector<size_t> sizes, int axis) {
   throw std::invalid_argument(
     "Tensor::split() is currently not supported in tensor data type " +
     getStringDataType());
-  std::vector<Tensor> ret;
-  return ret;
 }
 
 Tensor TensorBase::concat(const std::vector<Tensor> &tensors, int axis,
@@ -547,7 +528,6 @@ Tensor TensorBase::concat(const std::vector<Tensor> &tensors, int axis,
   throw std::invalid_argument(
     "Tensor::concat() is currently not supported in tensor data type " +
     getStringDataType());
-  return tensors[0];
 }
 
 Tensor &TensorBase::apply(std::function<float(float)> f, Tensor &output) const {
@@ -555,7 +535,6 @@ Tensor &TensorBase::apply(std::function<float(float)> f, Tensor &output) const {
     "Tensor::apply(std::function<float(float)> f, Tensor &output) is "
     "not supported in tensor data type " +
     getStringDataType());
-  return output;
 }
 
 #ifdef ENABLE_FP16
@@ -564,7 +543,6 @@ Tensor &TensorBase::apply(std::function<_FP16(_FP16)> f, Tensor &output) const {
     "Tensor::apply(std::function<_FP16(_FP16)> f, Tensor &output) is "
     "not supported in tensor data type " +
     getStringDataType());
-  return output;
 }
 #endif
 
@@ -572,7 +550,6 @@ Tensor &TensorBase::transpose(const std::string &direction, Tensor &out) const {
   throw std::invalid_argument(
     "Tensor::transpose() is currently not supported in tensor data type " +
     getStringDataType());
-  return out;
 }
 
 } // namespace nntrainer
index e07dc8bafc0af3d363bc81b57b7025cda5c9af07..3a16187468512c29145ab4252b4b28078899a1d7 100644 (file)
@@ -4337,6 +4337,7 @@ TEST(nntrainer_Tensor, multiply_strided_06_p) {
   int channel = 1;
   int height = 3;
   int width = 10;
+  const int size = 90;
 
   nntrainer::Tensor input(batch, channel, height, width);
   GEN_TEST_INPUT(input, i * (batch * height) + j * (width) + k + 1);
@@ -4347,9 +4348,9 @@ TEST(nntrainer_Tensor, multiply_strided_06_p) {
   float *indata = input.getData();
   ASSERT_NE(nullptr, indata);
 
-  float *outdata_beta = new float[(input.size())];
-  float *indata_mul = new float[(input.size())];
-  float *outdata = new float[(input.size())];
+  float outdata_beta[size];
+  float indata_mul[size];
+  float outdata[size];
 
   std::transform(
     indata, indata + batch * height * width * channel, outdata_beta,
@@ -4372,10 +4373,6 @@ TEST(nntrainer_Tensor, multiply_strided_06_p) {
     }
   }
 
-  delete[] outdata_beta;
-  delete[] indata_mul;
-  delete[] outdata;
-
   EXPECT_EQ(status, ML_ERROR_NONE);
 }
 
index 167acec29cd0af0e9752f2cd10c123e5a16e23ea..6bb0cb8a12b51095f6e3dfd7c57f94da16428b8d 100644 (file)
@@ -3522,6 +3522,7 @@ TEST(nntrainer_Tensor, multiply_strided_06_nhwc_p) {
   int channel = 1;
   int height = 3;
   int width = 10;
+  const int size = 90;
 
   nntrainer::Tensor input(batch, channel, height, width, NHWC_, FP32_);
   GEN_TEST_INPUT_NHWC(input, i * (batch * height * width) +
@@ -3534,9 +3535,9 @@ TEST(nntrainer_Tensor, multiply_strided_06_nhwc_p) {
   float *indata = input.getData();
   ASSERT_NE(nullptr, indata);
 
-  float *outdata_beta = new float[(input.size())];
-  float *indata_mul = new float[(input.size())];
-  float *outdata = new float[(input.size())];
+  float outdata_beta[size];
+  float indata_mul[size];
+  float outdata[size];
 
   std::transform(
     indata, indata + batch * height * width * channel, outdata_beta,
@@ -3559,10 +3560,6 @@ TEST(nntrainer_Tensor, multiply_strided_06_nhwc_p) {
     }
   }
 
-  delete[] outdata_beta;
-  delete[] indata_mul;
-  delete[] outdata;
-
   EXPECT_EQ(status, ML_ERROR_NONE);
 }