Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] Explicit cast when loosing precision (src/xbt/, and parts of surf/).
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Thu, 9 Jul 2020 21:05:45 +0000 (23:05 +0200)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Thu, 9 Jul 2020 21:09:47 +0000 (23:09 +0200)
That's enough for tonight....

src/surf/surf_interface.cpp
src/surf/xml/surfxml_parseplatf.cpp
src/surf/xml/surfxml_sax_cb.cpp
src/xbt/config.cpp
src/xbt/random.cpp
src/xbt/xbt_log_layout_format.cpp
src/xbt/xbt_main.cpp
src/xbt/xbt_virtu.cpp

index 624ee54..9c8f230 100644 (file)
@@ -197,7 +197,7 @@ int find_model_description(const std::vector<surf_model_description_t>& table, c
   auto pos = std::find_if(table.begin(), table.end(),
                           [&name](const surf_model_description_t& item) { return item.name == name; });
   if (pos != table.end())
-    return std::distance(table.begin(), pos);
+    return static_cast<int>(std::distance(table.begin(), pos));
 
   if (table.empty())
     xbt_die("No model is valid! This is a bug.");
index 1a0e0ad..638d2f5 100644 (file)
@@ -61,7 +61,7 @@ void sg_platf_trace_connect(simgrid::kernel::routing::TraceConnectCreationArgs*
 void parse_platform_file(const std::string& file)
 {
   const char* cfile = file.c_str();
-  int len           = strlen(cfile);
+  size_t len        = strlen(cfile);
   bool is_lua       = len > 3 && file[len - 3] == 'l' && file[len - 2] == 'u' && file[len - 1] == 'a';
 
   sg_platf_init();
index 43d1dbe..f8fcade 100644 (file)
@@ -182,8 +182,9 @@ void ETag_surfxml_storage___type()
   storage_type.content = A_surfxml_storage___type_content;
   storage_type.id      = A_surfxml_storage___type_id;
   storage_type.model   = A_surfxml_storage___type_model;
-  storage_type.size    = surf_parse_get_size(surf_parsed_filename, surf_parse_lineno, A_surfxml_storage___type_size,
-                                          "size of storage type", storage_type.id.c_str());
+  storage_type.size =
+      static_cast<sg_size_t>(surf_parse_get_size(surf_parsed_filename, surf_parse_lineno, A_surfxml_storage___type_size,
+                                                 "size of storage type", storage_type.id.c_str()));
   sg_platf_new_storage_type(&storage_type);
 }
 
index 5a9cb8d..f317be5 100644 (file)
@@ -99,7 +99,12 @@ public:
   static constexpr const char* type_name = "int";
   static inline int parse(const char* value)
   {
-    return parse_long(value);
+    long val = parse_long(value);
+    if (val < INT_MIN)
+      throw std::range_error("underflow");
+    if (val > INT_MAX)
+      throw std::range_error("overflow");
+    return static_cast<int>(val);
   }
 };
 template <> class ConfigType<double> {
index 9a0ef47..7deb41e 100644 (file)
@@ -73,7 +73,7 @@ int XbtRandom::uniform_int(int min, int max)
   do {
     value = mt19937_gen();
   } while (value >= decltype(mt19937_gen)::max() - decltype(mt19937_gen)::max() % range);
-  return value % range + min;
+  return static_cast<int>(value % range + min);
 }
 
 double XbtRandom::uniform_real(double min, double max)
@@ -84,7 +84,7 @@ double XbtRandom::uniform_real(double min, double max)
   do {
     numerator = mt19937_gen() - decltype(mt19937_gen)::min();
   } while (numerator == divisor);
-  return min + (max - min) * numerator / divisor;
+  return min + (max - min) * static_cast<double>(numerator) / divisor;
 }
 
 double XbtRandom::exponential(double lambda)
index 9a0e7fe..00e58fd 100644 (file)
@@ -99,7 +99,7 @@ static int xbt_log_layout_format_doit(const s_xbt_log_layout_t* l, xbt_log_event
             check_overflow(1);
             break;
           case '.': /* precision specifier */
-            precision = strtol(q + 1, &q, 10);
+            precision = static_cast<int>(strtol(q + 1, &q, 10));
             continue; /* conversion specifier still not found, continue reading */
           case '0':
           case '1':
@@ -111,7 +111,7 @@ static int xbt_log_layout_format_doit(const s_xbt_log_layout_t* l, xbt_log_event
           case '7':
           case '8':
           case '9': /* length modifier */
-            length = strtol(q, &q, 10);
+            length = static_cast<int>(strtol(q, &q, 10));
             continue; /* conversion specifier still not found, continue reading */
           case 'c':   /* category name; LOG4J compliant
                          should accept a precision postfix to show the hierarchy */
index 324c2bf..317741d 100644 (file)
@@ -87,12 +87,12 @@ static void xbt_preinit()
   GetSystemInfo(&si);
   xbt_pagesize = si.dwPageSize;
 #elif HAVE_SYSCONF
-  xbt_pagesize = sysconf(_SC_PAGESIZE);
+  xbt_pagesize = static_cast<int>(sysconf(_SC_PAGESIZE));
 #else
 # error Cannot get page size.
 #endif
 
-  xbt_pagebits = log2(xbt_pagesize);
+  xbt_pagebits = static_cast<int>(log2(xbt_pagesize));
 
 #ifdef _TWO_DIGIT_EXPONENT
   /* Even printf behaves differently on Windows... */
index 5902f3a..d2ef77c 100644 (file)
@@ -12,7 +12,7 @@
 int xbt_getpid()
 {
   const simgrid::kernel::actor::ActorImpl* self = SIMIX_process_self();
-  return self == nullptr ? 0 : self->get_pid();
+  return self == nullptr ? 0 : static_cast<int>(self->get_pid());
 }
 
 const char* xbt_procname(void)