replace strerror with strerror_r
authorhyeonseok lee <hs89.lee@samsung.com>
Wed, 8 Mar 2023 10:36:34 +0000 (19:36 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Fri, 10 Mar 2023 02:16:09 +0000 (11:16 +0900)
 - Replace strerror with strerror_r to make thread safe.

Signed-off-by: hyeonseok lee <hs89.lee@samsung.com>
Applications/Tizen_native/CustomShortcut/src/data.c
Applications/Tizen_native/CustomShortcut/src/main.c
Applications/utils/datagen/cifar/cifar_dataloader.cpp
nntrainer/compiler/tflite_interpreter.cpp
nntrainer/models/model_loader.cpp
nntrainer/tensor/swap_device.cpp
nntrainer/utils/ini_wrapper.cpp
nntrainer/utils/util_func.h

index 7c06b04..40fb009 100644 (file)
@@ -587,7 +587,10 @@ static void on_inference_end_(ml_tensors_data_h data,
   status =
     ml_tensors_data_get_tensor_data(data, 0, (void **)&raw_data, &data_size);
   if (status != ML_ERROR_NONE) {
-    LOG_E("get tensor data failed: reason %s", strerror(status));
+    const size_t error_buflen = 100;
+    char error_buf[error_buflen];
+    LOG_E("get tensor data failed: reason %s",
+          strerror_r(status, error_buf, error_buflen));
     goto RESUME;
   }
 
index 124f51a..10b68a3 100644 (file)
@@ -75,7 +75,10 @@ static void *train_(void *data) {
   LOG_D("creating thread to run model");
   status = pthread_create(&ad->tid_writer, NULL, data_run_model, (void *)ad);
   if (status < 0) {
-    LOG_E("creating pthread failed %s", strerror(errno));
+    const size_t error_buflen = 100;
+    char error_buf[error_buflen];
+    LOG_E("creating pthread failed %s",
+          strerror_r(errno, error_buf, error_buflen));
     goto RESTORE_CB;
   }
 
@@ -94,7 +97,10 @@ static void *train_(void *data) {
 
   status = pthread_join(ad->tid_writer, NULL);
   if (status < 0) {
-    LOG_E("joining writing thread failed %s", strerror(errno));
+    const size_t error_buflen = 100;
+    char error_buf[error_buflen];
+    LOG_E("joining writing thread failed %s",
+          strerror_r(errno, error_buf, error_buflen));
     pthread_cancel(ad->tid_writer);
   }
 
@@ -227,12 +233,18 @@ void presenter_on_canvas_submit_inference(void *data, Evas_Object *obj,
 
   status = pthread_create(&infer_thread, NULL, infer_, data);
   if (status != 0) {
-    LOG_E("creating pthread failed %s", strerror(errno));
+    const size_t error_buflen = 100;
+    char error_buf[error_buflen];
+    LOG_E("creating pthread failed %s",
+          strerror_r(errno, error_buf, error_buflen));
     return;
   }
   status = pthread_detach(infer_thread);
   if (status < 0) {
-    LOG_E("detaching reading thread failed %s", strerror(errno));
+    const size_t error_buflen = 100;
+    char error_buf[error_buflen];
+    LOG_E("detaching reading thread failed %s",
+          strerror_r(errno, error_buf, error_buflen));
     pthread_cancel(infer_thread);
   }
 }
@@ -257,12 +269,18 @@ void presenter_on_canvas_submit_training(void *data, Evas_Object *obj,
                                    presenter_on_back_button_press);
     status = pthread_create(&train_thread, NULL, train_, (void *)ad);
     if (status < 0) {
-      LOG_E("creating pthread failed %s", strerror(errno));
+      const size_t error_buflen = 100;
+      char error_buf[error_buflen];
+      LOG_E("creating pthread failed %s",
+            strerror_r(errno, error_buf, error_buflen));
       return;
     }
     status = pthread_detach(train_thread);
     if (status < 0) {
-      LOG_E("detaching reading thread failed %s", strerror(errno));
+      const size_t error_buflen = 100;
+      char error_buf[error_buflen];
+      LOG_E("detaching reading thread failed %s",
+            strerror_r(errno, error_buf, error_buflen));
       pthread_cancel(train_thread);
     }
   }
@@ -281,7 +299,7 @@ void presenter_on_canvas_submit_training(void *data, Evas_Object *obj,
 
 /********************* app related methods  **************************/
 static bool app_create(void *data) {
-  /* Hook to take necessary actions before main event loop starts
+  /** Hook to take necessary actions before main event loop starts
      Initialize UI resources and application's data
      If this function returns true, the main loop of application starts
      If this function returns false, the application is terminated */
index 459a533..c96bdfd 100644 (file)
@@ -136,8 +136,11 @@ void Cifar100DataLoader::next(float **input, float **label, bool *last) {
   /// @note below logic assumes a single input and the fine label is used
 
   auto fill_one_sample = [this](float *input_, float *label_, int index) {
+    const size_t error_buflen = 100;
+    char error_buf[error_buflen];
     NNTR_THROW_IF(!file.good(), std::invalid_argument)
-      << "file is not good, reason: " << std::strerror(errno);
+      << "file is not good, reason: "
+      << strerror_r(errno, error_buf, error_buflen);
     file.seekg(index * Cifar100DataLoader::SampleSize, std::ios_base::beg);
 
     uint8_t current_label;
index 07d641a..3140245 100644 (file)
@@ -52,8 +52,11 @@ void builder2file(const flatbuffers::FlatBufferBuilder &builder,
     << FUNC_TAG << "Verifying serialized model failed";
 
   std::ofstream os(out, std::ios_base::binary);
+  const size_t error_buflen = 100;
+  char error_buf[error_buflen];
   NNTR_THROW_IF(!os.good(), std::invalid_argument)
-    << FUNC_TAG << "failed to open, reason: " << strerror(errno);
+    << FUNC_TAG
+    << "failed to open, reason: " << strerror_r(errno, error_buf, error_buflen);
   os.write((char *)builder.GetBufferPointer(), builder.GetSize());
   os.close();
 }
index 0a22303..7f7d9c1 100644 (file)
@@ -472,8 +472,10 @@ int ModelLoader::loadFromConfig(std::string config, NeuralNetwork &model) {
 
   auto config_realpath_char = getRealpath(config.c_str(), nullptr);
   if (config_realpath_char == nullptr) {
+    const size_t error_buflen = 100;
+    char error_buf[error_buflen];
     ml_loge("failed to resolve config path to absolute path, reason: %s",
-            strerror(errno));
+            strerror_r(errno, error_buf, error_buflen));
     return ML_ERROR_INVALID_PARAMETER;
   }
   std::string config_realpath(config_realpath_char);
index 732c0dc..0c215df 100644 (file)
@@ -63,8 +63,11 @@ void *SwapDevice::getBuffer(off_t offset, size_t size, bool alloc_only) {
 
   char *ptr = static_cast<char *>(
     mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, off));
+  const size_t error_buflen = 100;
+  char error_buf[error_buflen];
   NNTR_THROW_IF(ptr == (void *)-1, std::runtime_error)
-    << "SwapDevice: mmap: " << std::string(strerror(errno));
+    << "SwapDevice: mmap: "
+    << std::string(strerror_r(errno, error_buf, error_buflen));
 
   void *buf = static_cast<void *>(ptr + diff);
   mapped[buf] = std::make_pair(ptr, len);
@@ -106,8 +109,11 @@ void SwapDevice::putBuffer(void *ptr, bool dealloc_only) {
 
   auto info = mapped[ptr];
   ret = munmap(std::get<void *>(info), std::get<size_t>(info));
+  const size_t error_buflen = 100;
+  char error_buf[error_buflen];
   NNTR_THROW_IF(ret == -1, std::runtime_error)
-    << "SwapDevice: munmap: " << std::string(strerror(errno));
+    << "SwapDevice: munmap: "
+    << std::string(strerror_r(errno, error_buf, error_buflen));
 
   mapped.erase(ptr);
 
index ab37fc2..7e1fb53 100644 (file)
@@ -128,8 +128,10 @@ void IniWrapper::save_ini(const std::string &ini_name) const {
 
 void IniWrapper::erase_ini() const noexcept {
   if (remove(getIniName().c_str())) {
-    std::cerr << "remove ini " << getIniName()
-              << "failed, reason: " << strerror(errno);
+    const size_t error_buflen = 100;
+    char error_buf[error_buflen];
+    std::cerr << "remove ini " << getIniName() << "failed, reason: "
+              << strerror_r(errno, error_buf, error_buflen);
   }
 }
 
index 9c8d007..f6fde9a 100644 (file)
@@ -188,9 +188,11 @@ template <typename T>
 T checkedOpenStream(const std::string &path, std::ios_base::openmode mode) {
   T model_file(path, mode);
   if (!model_file.good()) {
+    const size_t error_buflen = 100;
+    char error_buf[error_buflen];
     std::stringstream ss;
     ss << "[parseutil] requested file not opened, file path: " << path
-       << " reason: " << std::strerror(errno);
+       << " reason: " << strerror_r(errno, error_buf, error_buflen);
     if (errno == EPERM || errno == EACCES) {
       throw nntrainer::exception::permission_denied(ss.str().c_str());
     } else {