[ Coverity ] Fix Coverity Issues
authorjijoong.moon <jijoong.moon@samsung.com>
Mon, 3 Aug 2020 07:15:26 +0000 (16:15 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Mon, 3 Aug 2020 10:53:28 +0000 (19:53 +0900)
This PR includes Coverity issues fixes.

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

Signed-off-by: jijoong.moon <jijoong.moon@samsung.com>
Applications/KNN/jni/bitmap_helpers.cpp
Applications/ReinforcementLearning/DeepQ/jni/main.cpp
Applications/Training/jni/bitmap_helpers.cpp
Applications/mnist/jni/main.cpp
nntrainer/include/tensor_dim.h
nntrainer/src/neuralnet.cpp
test/include/nntrainer_test_util.h
test/nntrainer_test_util.cpp
test/tizen_capi/unittest_tizen_capi.cpp

index 4499610..a3e3d59 100644 (file)
@@ -109,8 +109,10 @@ uint8_t *read_bmp(const std::string &input_bmp_name, int *width, int *height,
   // Decode image, allocating tensor once the image size is known
   uint8_t *output = new uint8_t[abs(*height) * *width * *channels];
   const uint8_t *bmp_pixels = &img_bytes[header_size];
-  return decode_bmp(bmp_pixels, row_size, output, *width, abs(*height),
-                    *channels, top_down);
+  decode_bmp(bmp_pixels, row_size, output, *width, abs(*height), *channels,
+             top_down);
+  delete[] img_bytes;
+  return output;
 }
 
 } // namespace label_image
index 4f6ca69..62ff972 100644 (file)
@@ -453,17 +453,26 @@ int main(int argc, char **argv) {
           next_inbatch.push_back({{next_in}});
         }
 
+        nntrainer::Tensor q_in, nq_in;
+        try {
+          q_in = nntrainer::Tensor(inbatch);
+          nq_in = nntrainer::Tensor(next_inbatch);
+        } catch (...) {
+          std::cerr << "Error during tensor constructino" << std::endl;
+          mainNet.finalize();
+          targetNet.finalize();
+          return 0;
+        }
+
         /**
          * @brief     run forward propagation with mainNet
          */
-        nntrainer::Tensor Q =
-          mainNet.forwarding(nntrainer::Tensor(inbatch), status);
+        nntrainer::Tensor Q = mainNet.forwarding(q_in, status);
 
         /**
          * @brief     run forward propagation with targetNet
          */
-        nntrainer::Tensor NQ =
-          targetNet.forwarding(nntrainer::Tensor(next_inbatch), status);
+        nntrainer::Tensor NQ = targetNet.forwarding(nq_in, status);
         float *nqa = NQ.getData();
 
         /**
index ca7dc09..b8f5648 100644 (file)
@@ -112,7 +112,7 @@ uint8_t *read_bmp(const std::string &input_bmp_name, int *width, int *height,
   decode_bmp(bmp_pixels, row_size, output, *width, abs(*height), *channels,
              top_down);
 
-  delete (img_bytes);
+  delete[] img_bytes;
 
   return output;
 }
index 63f979b..4e21773 100644 (file)
@@ -91,12 +91,12 @@ float stepFunction(float x) {
  * @retval true/false false : end of data
  */
 bool getData(std::ifstream &F, std::vector<float> &outVec,
-             std::vector<float> &outLabel, int id) {
+             std::vector<float> &outLabel, unsigned int id) {
   F.clear();
   F.seekg(0, std::ios_base::end);
   uint64_t file_length = F.tellg();
-  uint64_t position =
-    (uint64_t)((feature_size + total_label_size) * id * sizeof(float));
+  uint64_t position = (uint64_t)((feature_size + total_label_size) *
+                                 (uint64_t)id * sizeof(float));
 
   if (position > file_length) {
     return false;
index 439fb0b..def186f 100644 (file)
@@ -44,8 +44,7 @@ public:
     len = b * feature_len;
   }
 
-  TensorDim(const TensorDim &rhs) :
-    TensorDim(rhs.batch(), rhs.channel(), rhs.height(), rhs.width()){};
+  TensorDim(const TensorDim &rhs) = default;
 
   ~TensorDim(){};
 
index d255086..b89e136 100644 (file)
@@ -309,7 +309,7 @@ int NeuralNetwork::loadFromConfig() {
 
   if (layers.empty()) {
     ml_loge("there is no layer section in the ini file");
-    return ML_ERROR_INVALID_PARAMETER;
+    status = ML_ERROR_INVALID_PARAMETER;
   }
 
   iniparser_freedict(ini);
index 713d736..125cbe1 100644 (file)
@@ -157,7 +157,7 @@ protected:
   nntrainer::NeuralNetwork NN;
 
 private:
-  void erase_ini() { std::remove((char *)(getIniName().c_str())); }
+  void erase_ini() { name.clear(); }
   int failAt;
   std::string name;
   std::vector<IniSection> sections;
index d3a4494..fd57a34 100644 (file)
@@ -87,14 +87,14 @@ static int rangeRandom(int min, int max) {
  * @retval true/false false : end of data
  */
 static bool getData(std::ifstream &F, std::vector<float> &outVec,
-                    std::vector<float> &outLabel, uint64_t id) {
+                    std::vector<float> &outLabel, unsigned int id) {
   F.clear();
   F.seekg(0, std::ios_base::end);
   uint64_t file_length = F.tellg();
   if (id < 0)
     return false;
   uint64_t position =
-    (uint64_t)((feature_size + num_class) * id * sizeof(float));
+    (uint64_t)((feature_size + num_class) * (uint64_t)id * sizeof(float));
 
   if (position > file_length) {
     return false;
index b31b49f..372cbe3 100644 (file)
@@ -760,7 +760,7 @@ TEST(nntrainer_capi_summary, summary_01_p) {
   status = ml_train_model_destroy(handle);
   EXPECT_EQ(status, ML_ERROR_NONE);
 
-  free(sum);
+  delete[] sum;
 }
 
 /**