From 539af86e870c62ba4a28b5b4e0cedd252881dfb0 Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Wed, 14 Oct 2020 22:58:29 +0200 Subject: [PATCH] [sonar] Replace some C-style arrays. --- src/kernel/context/Context.hpp | 3 +- src/kernel/context/ContextThread.cpp | 4 +-- src/simdag/sd_global.cpp | 6 ++-- src/simix/popping_generated.cpp | 4 +-- src/simix/popping_private.hpp | 3 +- src/simix/simcalls.py | 4 +-- src/simix/smx_context.cpp | 29 ++++++++++--------- src/simix/smx_global.cpp | 6 ++-- src/xbt/backtrace.cpp | 7 +++-- src/xbt/config.cpp | 11 ++----- src/xbt/log.cpp | 23 +++++---------- src/xbt/log_private.hpp | 4 +++ src/xbt/memory_map.cpp | 3 +- src/xbt/xbt_log_appender_file.cpp | 7 ++--- src/xbt/xbt_log_layout_format.cpp | 2 -- src/xbt/xbt_log_layout_simple.cpp | 1 - src/xbt/xbt_os_file.cpp | 11 +++---- src/xbt/xbt_str.cpp | 11 +++---- .../s4u/basic-link-test/basic-link-test.cpp | 6 ++-- 19 files changed, 69 insertions(+), 76 deletions(-) diff --git a/src/kernel/context/Context.hpp b/src/kernel/context/Context.hpp index 3e77edbe26..f1aa12bc48 100644 --- a/src/kernel/context/Context.hpp +++ b/src/kernel/context/Context.hpp @@ -9,6 +9,7 @@ #include "simgrid/forward.h" #include "src/kernel/activity/ActivityImpl.hpp" +#include #include #include @@ -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 sigsegv_stack; #endif #endif diff --git a/src/kernel/context/ContextThread.cpp b/src/kernel/context/ContextThread.cpp index 42d2be6304..dc6e2ad77e 100644 --- a/src/kernel/context/ContextThread.cpp +++ b/src/kernel/context/ContextThread.cpp @@ -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 diff --git a/src/simdag/sd_global.cpp b/src/simdag/sd_global.cpp index 80a6105582..22d4faf9be 100644 --- a/src/simdag/sd_global.cpp +++ b/src/simdag/sd_global.cpp @@ -124,9 +124,9 @@ std::set* 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(log2(static_cast(state)))].data(); + static std::array state_names{ + {"not scheduled", "schedulable", "scheduled", "runnable", "running", "done", "failed"}}; + return state_names.at(static_cast(log2(static_cast(state)))); } /** diff --git a/src/simix/popping_generated.cpp b/src/simix/popping_generated.cpp index 5cc7d5132d..e83d930e8e 100644 --- a/src/simix/popping_generated.cpp +++ b/src/simix/popping_generated.cpp @@ -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 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 diff --git a/src/simix/popping_private.hpp b/src/simix/popping_private.hpp index e78caf94d9..285d39bb6f 100644 --- a/src/simix/popping_private.hpp +++ b/src/simix/popping_private.hpp @@ -9,12 +9,13 @@ #include "simgrid/forward.h" #include "src/kernel/activity/ActivityImpl.hpp" +#include #include /********************************* 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 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); diff --git a/src/simix/simcalls.py b/src/simix/simcalls.py index ab7f57a6e6..f43657b8e1 100755 --- a/src/simix/simcalls.py +++ b/src/simix/simcalls.py @@ -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 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( diff --git a/src/simix/smx_context.cpp b/src/simix/smx_context.cpp index 1365a6ad00..3fd235f7ce 100644 --- a/src/simix/smx_context.cpp +++ b/src/simix/smx_context.cpp @@ -10,42 +10,43 @@ #include "smpi/smpi.h" #include "xbt/config.hpp" +#include #include XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_context, simix, "Context switching mechanism"); -static std::pair context_factories[] = { +constexpr std::initializer_list> + 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 context_factory_name( - "contexts/factory", - (std::string("Possible values: ")+contexts_list()).c_str(), - context_factories[0].first); +static simgrid::config::Flag + 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; diff --git a/src/simix/smx_global.cpp b/src/simix/smx_global.cpp index 164ddcac32..1349b13e20 100644 --- a/src/simix/smx_global.cpp +++ b/src/simix/smx_global.cpp @@ -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 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) { diff --git a/src/xbt/backtrace.cpp b/src/xbt/backtrace.cpp index 349ed1163b..7df4327206 100644 --- a/src/xbt/backtrace.cpp +++ b/src/xbt/backtrace.cpp @@ -53,9 +53,6 @@ std::unique_ptr> 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()) {} diff --git a/src/xbt/config.cpp b/src/xbt/config.cpp index c698602c41..76f7cbceff 100644 --- a/src/xbt/config.cpp +++ b/src/xbt/config.cpp @@ -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"); diff --git a/src/xbt/log.cpp b/src/xbt/log.cpp index ae51917ec5..e4525c36ab 100644 --- a/src/xbt/log.cpp +++ b/src/xbt/log.cpp @@ -13,6 +13,7 @@ #include "xbt/string.hpp" #include +#include #include #include #include @@ -38,16 +39,8 @@ struct xbt_log_setting_t { static std::vector xbt_log_settings; -const char *xbt_log_priority_names[8] = { - "NONE", - "TRACE", - "DEBUG", - "VERBOSE", - "INFO", - "WARNING", - "ERROR", - "CRITICAL" -}; +const std::array 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(ev->priority) < sizeof(xbt_log_priority_names)/sizeof(xbt_log_priority_names[0]), + xbt_assert(static_cast(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 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; diff --git a/src/xbt/log_private.hpp b/src/xbt/log_private.hpp index e49cb065ea..48b0527c6d 100644 --- a/src/xbt/log_private.hpp +++ b/src/xbt/log_private.hpp @@ -7,6 +7,8 @@ #define LOG_PRIVATE_H #include "xbt/log.h" +#include + 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 xbt_log_priority_names; + /** * @ingroup XBT_log_implem * @param cat the category (not only its name, but the variable) diff --git a/src/xbt/memory_map.cpp b/src/xbt/memory_map.cpp index 57eced3369..44d6fb2f11 100644 --- a/src/xbt/memory_map.cpp +++ b/src/xbt/memory_map.cpp @@ -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 #include #include #include @@ -181,7 +182,7 @@ std::vector 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 lfields; lfields[0] = strtok_r(line, " ", &saveptr); int i; diff --git a/src/xbt/xbt_log_appender_file.cpp b/src/xbt/xbt_log_appender_file.cpp index 8e8831d3a7..33ab714d56 100644 --- a/src/xbt/xbt_log_appender_file.cpp +++ b/src/xbt/xbt_log_appender_file.cpp @@ -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); } } diff --git a/src/xbt/xbt_log_layout_format.cpp b/src/xbt/xbt_log_layout_format.cpp index 6680282b1a..6ba73f7eb8 100644 --- a/src/xbt/xbt_log_layout_format.cpp +++ b/src/xbt/xbt_log_layout_format.cpp @@ -12,8 +12,6 @@ #include #include -extern const char *xbt_log_priority_names[8]; - static constexpr const char* ERRMSG = "Unknown %%%c sequence in layout format (%s).\n" "Known sequences:\n" diff --git a/src/xbt/xbt_log_layout_simple.cpp b/src/xbt/xbt_log_layout_simple.cpp index 7d1c84bfd9..8b3fbe2382 100644 --- a/src/xbt/xbt_log_layout_simple.cpp +++ b/src/xbt/xbt_log_layout_simple.cpp @@ -12,7 +12,6 @@ #include "simgrid/host.h" /* sg_host_self_get_name */ #include -extern const char *xbt_log_priority_names[8]; extern int xbt_log_no_loc; #define check_overflow(len) \ diff --git a/src/xbt/xbt_os_file.cpp b/src/xbt/xbt_os_file.cpp index 4316ee4367..229ac5ee64 100644 --- a/src/xbt/xbt_os_file.cpp +++ b/src/xbt/xbt_os_file.cpp @@ -14,20 +14,21 @@ #endif #if HAVE_UNISTD_H +#include +#include #include #endif -#include #include #include /* 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 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 diff --git a/src/xbt/xbt_str.cpp b/src/xbt/xbt_str.cpp index 6106399acf..dfd3d6b95c 100644 --- a/src/xbt/xbt_str.cpp +++ b/src/xbt/xbt_str.cpp @@ -9,6 +9,7 @@ #include "xbt/misc.h" #include "xbt/str.h" /* headers of these functions */ #include "xbt/string.hpp" +#include /** @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 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; diff --git a/teshsuite/s4u/basic-link-test/basic-link-test.cpp b/teshsuite/s4u/basic-link-test/basic-link-test.cpp index bf26c68ac0..c129a21a3e 100644 --- a/teshsuite/s4u/basic-link-test/basic-link-test.cpp +++ b/teshsuite/s4u/basic-link-test/basic-link-test.cpp @@ -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(l->get_data())), "User data was corrupted."); + l->set_data(&user_data); + xbt_assert(user_data == *static_cast(l->get_data()), "User data was corrupted."); } return 0; -- 2.20.1