Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] Replace some C-style arrays.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Wed, 14 Oct 2020 20:58:29 +0000 (22:58 +0200)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Thu, 15 Oct 2020 08:25:49 +0000 (10:25 +0200)
19 files changed:
src/kernel/context/Context.hpp
src/kernel/context/ContextThread.cpp
src/simdag/sd_global.cpp
src/simix/popping_generated.cpp
src/simix/popping_private.hpp
src/simix/simcalls.py
src/simix/smx_context.cpp
src/simix/smx_global.cpp
src/xbt/backtrace.cpp
src/xbt/config.cpp
src/xbt/log.cpp
src/xbt/log_private.hpp
src/xbt/memory_map.cpp
src/xbt/xbt_log_appender_file.cpp
src/xbt/xbt_log_layout_format.cpp
src/xbt/xbt_log_layout_simple.cpp
src/xbt/xbt_os_file.cpp
src/xbt/xbt_str.cpp
teshsuite/s4u/basic-link-test/basic-link-test.cpp

index 3e77edb..f1aa12b 100644 (file)
@@ -9,6 +9,7 @@
 #include "simgrid/forward.h"
 #include "src/kernel/activity/ActivityImpl.hpp"
 
+#include <array>
 #include <csignal>
 #include <functional>
 
@@ -105,6 +106,6 @@ XBT_PRIVATE void SIMIX_context_mod_init();
 XBT_PRIVATE void SIMIX_context_mod_exit();
 
 #ifndef WIN32
-XBT_PUBLIC_DATA unsigned char sigsegv_stack[SIGSTKSZ];
+XBT_PUBLIC_DATA std::array<unsigned char, SIGSTKSZ> sigsegv_stack;
 #endif
 #endif
index 42d2be6..dc6e2ad 100644 (file)
@@ -89,8 +89,8 @@ void ThreadContext::wrapper(ThreadContext* context)
 #ifndef WIN32
   /* Install alternate signal stack, for SIGSEGV handler. */
   stack_t stack;
-  stack.ss_sp = sigsegv_stack;
-  stack.ss_size = sizeof sigsegv_stack;
+  stack.ss_sp    = sigsegv_stack.data();
+  stack.ss_size  = sigsegv_stack.size();
   stack.ss_flags = 0;
   sigaltstack(&stack, nullptr);
 #endif
index 80a6105..22d4faf 100644 (file)
@@ -124,9 +124,9 @@ std::set<SD_task_t>* simulate(double how_long){
  * @return the equivalent as a readable string
  */
 const char *__get_state_name(e_SD_task_state_t state){
-  static std::string state_names[7] =
-    { "not scheduled", "schedulable", "scheduled", "runnable","running", "done", "failed" };
-  return state_names[static_cast<int>(log2(static_cast<double>(state)))].data();
+  static std::array<const char*, 7> state_names{
+      {"not scheduled", "schedulable", "scheduled", "runnable", "running", "done", "failed"}};
+  return state_names.at(static_cast<int>(log2(static_cast<double>(state))));
 }
 
 /**
index 5cc7d51..e83d930 100644 (file)
@@ -24,7 +24,7 @@
 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_popping);
 
 /** @brief Simcalls' names (generated from src/simix/simcalls.in) */
-const char* simcall_names[] = {
+const std::array<const char*, NUM_SIMCALLS> simcall_names{{
     "SIMCALL_NONE",
     "SIMCALL_EXECUTION_WAITANY_FOR",
     "SIMCALL_COMM_SEND",
@@ -45,7 +45,7 @@ const char* simcall_names[] = {
     "SIMCALL_MC_RANDOM",
     "SIMCALL_RUN_KERNEL",
     "SIMCALL_RUN_BLOCKING",
-};
+}};
 
 /** @private
  * @brief (in kernel mode) unpack the simcall and activate the handler
index e78caf9..285d39b 100644 (file)
@@ -9,12 +9,13 @@
 #include "simgrid/forward.h"
 #include "src/kernel/activity/ActivityImpl.hpp"
 
+#include <array>
 #include <boost/intrusive_ptr.hpp>
 
 /********************************* Simcalls *********************************/
 #include "popping_enum.hpp" /* Definition of e_smx_simcall_t, with one value per simcall */
 
-XBT_PUBLIC_DATA const char* simcall_names[]; /* Name of each simcall */
+XBT_PUBLIC_DATA const std::array<const char*, NUM_SIMCALLS> simcall_names; /* Name of each simcall */
 
 typedef bool (*simix_match_func_t)(void*, void*, simgrid::kernel::activity::CommImpl*);
 typedef void (*simix_copy_data_func_t)(simgrid::kernel::activity::CommImpl*, void*, size_t);
index ab7f57a..f43657b 100755 (executable)
@@ -325,12 +325,12 @@ if __name__ == '__main__':
 
     fd.write(
         '/** @brief Simcalls\' names (generated from src/simix/simcalls.in) */\n')
-    fd.write('const char* simcall_names[] = {\n')
+    fd.write('const std::array<const char*, NUM_SIMCALLS> simcall_names{{\n')
 
     fd.write('    "SIMCALL_NONE",\n')
     handle(fd, Simcall.string, simcalls, simcalls_dict)
 
-    fd.write('};\n\n')
+    fd.write('}};\n\n')
 
     fd.write('/** @private\n')
     fd.write(
index 1365a6a..3fd235f 100644 (file)
 #include "smpi/smpi.h"
 #include "xbt/config.hpp"
 
+#include <initializer_list>
 #include <thread>
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_context, simix, "Context switching mechanism");
 
-static std::pair<const char*, simgrid::kernel::context::ContextFactoryInitializer> context_factories[] = {
+constexpr std::initializer_list<std::pair<const char*, simgrid::kernel::context::ContextFactoryInitializer>>
+    context_factories = {
 #if HAVE_RAW_CONTEXTS
-  { "raw", &simgrid::kernel::context::raw_factory },
+        {"raw", &simgrid::kernel::context::raw_factory},
 #endif
 #if HAVE_UCONTEXT_CONTEXTS
-  { "ucontext", &simgrid::kernel::context::sysv_factory },
+        {"ucontext", &simgrid::kernel::context::sysv_factory},
 #endif
 #if HAVE_BOOST_CONTEXTS
-  { "boost", &simgrid::kernel::context::boost_factory },
+        {"boost", &simgrid::kernel::context::boost_factory},
 #endif
-  { "thread", &simgrid::kernel::context::thread_factory },
+        {"thread", &simgrid::kernel::context::thread_factory},
 };
 
-static_assert(sizeof(context_factories) != 0, "No context factories are enabled for this build");
+static_assert(context_factories.size() > 0, "No context factories are enabled for this build");
 
 // Create the list of possible contexts:
 static inline
 std::string contexts_list()
 {
   std::string res;
-  const std::size_t n = sizeof(context_factories) / sizeof(context_factories[0]);
-  for (std::size_t i = 1; i != n; ++i) {
-    res += ", ";
-    res += context_factories[i].first;
+  std::string sep = "";
+  for (auto const& factory : context_factories) {
+    res += sep + factory.first;
+    sep = ", ";
   }
   return res;
 }
 
-static simgrid::config::Flag<std::string> context_factory_name(
-  "contexts/factory",
-  (std::string("Possible values: ")+contexts_list()).c_str(),
-  context_factories[0].first);
+static simgrid::config::Flag<std::string>
+    context_factory_name("contexts/factory", (std::string("Possible values: ") + contexts_list()).c_str(),
+                         context_factories.begin()->first);
 
 unsigned smx_context_stack_size;
 unsigned smx_context_guard_size;
index 164ddca..1349b13 100644 (file)
@@ -85,7 +85,7 @@ static void segvhandler(int signum, siginfo_t* siginfo, void* /*context*/)
   std::raise(signum);
 }
 
-unsigned char sigsegv_stack[SIGSTKSZ]; /* alternate stack for SIGSEGV handler */
+std::array<unsigned char, SIGSTKSZ> sigsegv_stack; /* alternate stack for SIGSEGV handler */
 
 /**
  * Install signal handler for SIGSEGV.  Check that nobody has already installed
@@ -95,8 +95,8 @@ static void install_segvhandler()
 {
   stack_t stack;
   stack_t old_stack;
-  stack.ss_sp = sigsegv_stack;
-  stack.ss_size = sizeof sigsegv_stack;
+  stack.ss_sp    = sigsegv_stack.data();
+  stack.ss_size  = sigsegv_stack.size();
   stack.ss_flags = 0;
 
   if (sigaltstack(&stack, &old_stack) == -1) {
index 349ed11..7df4327 100644 (file)
@@ -53,9 +53,6 @@ std::unique_ptr<char, std::function<void(char*)>> demangle(const char* name)
 class BacktraceImpl {
 #if HAVE_BOOST_STACKTRACE_BACKTRACE || HAVE_BOOST_STACKTRACE_ADDR2LINE
   const boost::stacktrace::stacktrace st = boost::stacktrace::stacktrace();
-#else
-  const char st[1] = ""; // fallback value
-#endif
 public:
   std::string resolve() const
   {
@@ -63,6 +60,10 @@ public:
     ss << st;
     return ss.str();
   }
+#else
+public:
+  std::string resolve() const { return ""; } // fallback value
+#endif
 };
 
 Backtrace::Backtrace() : impl_(std::make_shared<BacktraceImpl>()) {}
index c698602..76f7cbc 100644 (file)
@@ -38,19 +38,12 @@ namespace config {
 
 namespace {
 
-const char* true_values[] = {
-  "yes", "on", "true", "1"
-};
-const char* false_values[] = {
-  "no", "off", "false", "0"
-};
-
 static bool parse_bool(const char* value)
 {
-  for (const char* const& true_value : true_values)
+  for (const char* true_value : {"yes", "on", "true", "1"})
     if (std::strcmp(true_value, value) == 0)
       return true;
-  for (const char* const& false_value : false_values)
+  for (const char* false_value : {"no", "off", "false", "0"})
     if (std::strcmp(false_value, value) == 0)
       return false;
   throw std::range_error("not a boolean");
index ae51917..e4525c3 100644 (file)
@@ -13,6 +13,7 @@
 #include "xbt/string.hpp"
 
 #include <algorithm>
+#include <array>
 #include <mutex>
 #include <string>
 #include <vector>
@@ -38,16 +39,8 @@ struct xbt_log_setting_t {
 
 static std::vector<xbt_log_setting_t> xbt_log_settings;
 
-const char *xbt_log_priority_names[8] = {
-  "NONE",
-  "TRACE",
-  "DEBUG",
-  "VERBOSE",
-  "INFO",
-  "WARNING",
-  "ERROR",
-  "CRITICAL"
-};
+const std::array<const char*, 8> xbt_log_priority_names{
+    {"NONE", "TRACE", "DEBUG", "VERBOSE", "INFO", "WARNING", "ERROR", "CRITICAL"}};
 
 s_xbt_log_category_t _XBT_LOGV(XBT_LOG_ROOT_CAT) = {
     nullptr /*parent */,
@@ -158,7 +151,7 @@ void _xbt_log_event_log(xbt_log_event_t ev, const char *fmt, ...)
   const xbt_log_category_s* cat = ev->cat;
 
   xbt_assert(ev->priority >= 0, "Negative logging priority naturally forbidden");
-  xbt_assert(static_cast<size_t>(ev->priority) < sizeof(xbt_log_priority_names)/sizeof(xbt_log_priority_names[0]),
+  xbt_assert(static_cast<size_t>(ev->priority) < xbt_log_priority_names.size(),
              "Priority %d is greater than the biggest allowed value", ev->priority);
 
   while (true) {
@@ -169,15 +162,15 @@ void _xbt_log_event_log(xbt_log_event_t ev, const char *fmt, ...)
 
       /* First, try with a static buffer */
       int done = 0;
-      char buff[XBT_LOG_STATIC_BUFFER_SIZE];
-      ev->buffer      = buff;
-      ev->buffer_size = sizeof buff;
+      std::array<char, XBT_LOG_STATIC_BUFFER_SIZE> buff;
+      ev->buffer      = buff.data();
+      ev->buffer_size = buff.size();
       va_start(ev->ap, fmt);
       done = cat->layout->do_layout(cat->layout, ev, fmt);
       va_end(ev->ap);
       ev->buffer = nullptr; // Calm down, static analyzers, this pointer to local array won't leak out of the scope.
       if (done) {
-        appender->do_append(appender, buff);
+        appender->do_append(appender, buff.data());
       } else {
         /* The static buffer was too small, use a dynamically expanded one */
         ev->buffer_size = XBT_LOG_DYNAMIC_BUFFER_SIZE;
index e49cb06..48b0527 100644 (file)
@@ -7,6 +7,8 @@
 #define LOG_PRIVATE_H
 
 #include "xbt/log.h"
+#include <array>
+
 struct xbt_log_appender_s {
   void (*do_append)(const s_xbt_log_appender_t* this_appender, const char* event);
   void (*free_)(const s_xbt_log_appender_t* this_);
@@ -19,6 +21,8 @@ struct xbt_log_layout_s {
   void *data;
 };
 
+extern const std::array<const char*, 8> xbt_log_priority_names;
+
 /**
  * @ingroup XBT_log_implem
  * @param cat the category (not only its name, but the variable)
index 57eced3..44d6fb2 100644 (file)
@@ -3,6 +3,7 @@
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
+#include <array>
 #include <cstdio>
 #include <cstdlib>
 #include <cstring>
@@ -181,7 +182,7 @@ std::vector<VmMap> get_memory_map(pid_t pid)
 
     /* Tokenize the line using spaces as delimiters and store each token in lfields array. We expect 5 tokens for 6 fields */
     char* saveptr = nullptr; // for strtok_r()
-    char* lfields[6];
+    std::array<char*, 6> lfields;
     lfields[0] = strtok_r(line, " ", &saveptr);
 
     int i;
index 8e8831d..33ab714 100644 (file)
@@ -71,18 +71,17 @@ static void open_append2_file(xbt_log_append2_file_t data){
     //Split
     if(data->file)
       fclose(data->file);
-    char newname[512];
     char* pre=xbt_strdup(data->filename);
     char* sep=strchr(pre,'%');
     if(!sep)
       sep=pre+strlen(pre);
     char* post=sep+1;
     *sep='\0';
-    snprintf(newname,511,"%s%i%s",pre,data->count,post);
+    std::string newname = pre + std::to_string(data->count) + post;
     data->count++;
-    data->file= fopen(newname, "w");
+    data->file = fopen(newname.c_str(), "w");
     if (data->file == nullptr)
-      xbt_die("Cannot open file: %s: %s", newname, strerror(errno));
+      xbt_die("Cannot open file: %s: %s", newname.c_str(), strerror(errno));
     xbt_free(pre);
   }
 }
index 6680282..6ba73f7 100644 (file)
@@ -12,8 +12,6 @@
 #include <algorithm>
 #include <cstdio>
 
-extern const char *xbt_log_priority_names[8];
-
 static constexpr const char* ERRMSG =
     "Unknown %%%c sequence in layout format (%s).\n"
     "Known sequences:\n"
index 7d1c84b..8b3fbe2 100644 (file)
@@ -12,7 +12,6 @@
 #include "simgrid/host.h"   /* sg_host_self_get_name */
 #include <cstdio>
 
-extern const char *xbt_log_priority_names[8];
 extern int xbt_log_no_loc;
 
 #define check_overflow(len)                                                                                            \
index 4316ee4..229ac5e 100644 (file)
 #endif
 
 #if HAVE_UNISTD_H
+#include <array>
+#include <cerrno>
 #include <unistd.h>
 #endif
 
-#include <cerrno>
 #include <cstring>
 #include <libgen.h> /* POSIX dirname */
 
 simgrid::xbt::Path::Path()
 {
 #if HAVE_UNISTD_H
-  char buffer[2048];
-  const char* ret = getcwd(buffer, 2048);
-  xbt_assert(ret == buffer, "Error during getcwd: %s", strerror(errno));
-  path_ = std::string(buffer);
+  std::array<char, 2048> buffer;
+  const char* cwd = getcwd(buffer.data(), 2048);
+  xbt_assert(cwd != nullptr, "Error during getcwd: %s", strerror(errno));
+  path_ = std::string(cwd);
 #else
   path_ = std::string(".");
 #endif
index 6106399..dfd3d6b 100644 (file)
@@ -9,6 +9,7 @@
 #include "xbt/misc.h"
 #include "xbt/str.h" /* headers of these functions */
 #include "xbt/string.hpp"
+#include <array>
 
 /** @brief Splits a string into a dynar of strings
  *
@@ -28,18 +29,18 @@ xbt_dynar_t xbt_str_split(const char *s, const char *sep)
 {
   xbt_dynar_t res = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
   const char *sep_dflt = " \t\n\r\x0B";
-  char is_sep[256] = { 1, 0 };
+  std::array<bool, 256> is_sep;
 
   /* check what are the separators */
-  memset(is_sep, 0, sizeof(is_sep));
+  is_sep.fill(false);
   if (not sep) {
     while (*sep_dflt)
-      is_sep[(unsigned char) *sep_dflt++] = 1;
+      is_sep[(unsigned char)*sep_dflt++] = true;
   } else {
     while (*sep)
-      is_sep[(unsigned char) *sep++] = 1;
+      is_sep[(unsigned char)*sep++] = true;
   }
-  is_sep[0] = 1; /* End of string is also separator */
+  is_sep[0] = true; /* End of string is also separator */
 
   /* Do the job */
   const char* p = s;
index bf26c68..c129a21 100644 (file)
@@ -11,7 +11,7 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(basic_link_test, s4u, "basic link test");
 
 int main(int argc, char** argv)
 {
-  char user_data[] = "some user_data";
+  std::string user_data = "some user_data";
 
   /* initialization of SD */
   simgrid::s4u::Engine e(&argc, argv);
@@ -28,8 +28,8 @@ int main(int argc, char** argv)
 
   for (const auto& l : links) {
     XBT_INFO("%s: latency = %.5f, bandwidth = %f", l->get_cname(), l->get_latency(), l->get_bandwidth());
-    l->set_data(user_data);
-    xbt_assert(!strcmp(user_data, static_cast<const char*>(l->get_data())), "User data was corrupted.");
+    l->set_data(&user_data);
+    xbt_assert(user_data == *static_cast<const std::string*>(l->get_data()), "User data was corrupted.");
   }
 
   return 0;