Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use std::byte for bye-oriented data manipulation (sonar).
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Tue, 10 May 2022 13:45:07 +0000 (15:45 +0200)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Fri, 16 Dec 2022 15:55:24 +0000 (16:55 +0100)
src/mc/inspect/mc_dwarf.cpp
src/mc/inspect/mc_unw_vmread.cpp

index 7b0c0f5..394eab8 100644 (file)
@@ -869,7 +869,7 @@ static void read_dwarf_info(simgrid::mc::ObjectInformation* info, Dwarf* dwarf)
  *  @param  elf libelf handle for an ELF file
  *  @return build-id for this ELF file (or an empty vector if none is found)
  */
-static std::vector<char> get_build_id(Elf* elf)
+static std::vector<std::byte> get_build_id(Elf* elf)
 {
 #ifdef __linux
   // Summary: the GNU build ID is stored in a ("GNU, NT_GNU_BUILD_ID) note
@@ -898,30 +898,28 @@ static std::vector<char> get_build_id(Elf* elf)
       // A build ID note is identified by the pair ("GNU", NT_GNU_BUILD_ID)
       // (a namespace and a type within this namespace):
       if (nhdr.n_type == NT_GNU_BUILD_ID && nhdr.n_namesz == sizeof("GNU") &&
-          memcmp((char*)data->d_buf + name_pos, "GNU", sizeof("GNU")) == 0) {
+          memcmp(static_cast<std::byte*>(data->d_buf) + name_pos, "GNU", sizeof("GNU")) == 0) {
         XBT_DEBUG("Found GNU/NT_GNU_BUILD_ID note");
-        char* start = (char*)data->d_buf + desc_pos;
-        char* end   = start + nhdr.n_descsz;
-        return std::vector<char>(start, end);
+        std::byte* start = static_cast<std::byte*>(data->d_buf) + desc_pos;
+        std::byte* end   = start + nhdr.n_descsz;
+        return std::vector<std::byte>(start, end);
       }
     }
   }
 #endif
-  return std::vector<char>();
+  return std::vector<std::byte>();
 }
 
 /** Binary data to hexadecimal */
-static inline std::array<char, 2> to_hex(std::uint8_t byte)
+static inline std::array<char, 2> to_hex(std::byte byte)
 {
   constexpr std::array<char, 16> hexdigits{
       {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}};
-  // Horrid double braces!
-  // Apparently, this is needed in C++11 (not in C++14).
-  return {{hexdigits[byte >> 4], hexdigits[byte & 0xF]}};
+  return {hexdigits[std::to_integer<unsigned>(byte >> 4)], hexdigits[std::to_integer<unsigned>(byte & std::byte{0xF})]};
 }
 
 /** Binary data to hexadecimal */
-static std::string to_hex(const char* data, std::size_t count)
+static std::string to_hex(const std::byte* data, std::size_t count)
 {
   std::string res;
   res.resize(2 * count);
@@ -931,7 +929,7 @@ static std::string to_hex(const char* data, std::size_t count)
 }
 
 /** Binary data to hexadecimal */
-static std::string to_hex(std::vector<char> const& data)
+static std::string to_hex(std::vector<std::byte> const& data)
 {
   return to_hex(data.data(), data.size());
 }
@@ -949,7 +947,7 @@ static constexpr auto debug_paths = {
  */
 // Example:
 // /usr/lib/debug/.build-id/0b/dc77f1c29aea2b14ff5acd9a19ab3175ffdeae.debug
-static int find_by_build_id(std::vector<char> id)
+static int find_by_build_id(std::vector<std::byte> id)
 {
   std::string filename;
   std::string hex = to_hex(id);
@@ -1005,7 +1003,7 @@ static void MC_load_dwarf(simgrid::mc::ObjectInformation* info)
 
   // Try with NT_GNU_BUILD_ID: we find the build ID in the ELF file and then
   // use this ID to find the file in some known locations in the filesystem.
-  if (std::vector<char> build_id = get_build_id(elf); not build_id.empty()) {
+  if (std::vector<std::byte> build_id = get_build_id(elf); not build_id.empty()) {
     elf_end(elf);
     close(fd);
 
index 671fccd..78a7355 100644 (file)
@@ -57,7 +57,7 @@ static int access_mem(const unw_addr_space_t as, const unw_word_t addr, unw_word
   // On older kernels, it is necessary to ptrace the target process.
   size_t count = size;
   auto off     = static_cast<off_t>(addr);
-  auto* buf    = reinterpret_cast<char*>(valp);
+  auto* buf    = reinterpret_cast<std::byte*>(valp);
   int fd       = simgrid::mc::open_vm(pid, O_RDONLY);
   if (fd < 0)
     return -UNW_EINVAL;