Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Avoid extra memory allocation and fix off-by-one buffer overflow
authorGabriel Corona <gabriel.corona@loria.fr>
Mon, 18 Jul 2016 12:32:13 +0000 (14:32 +0200)
committerGabriel Corona <gabriel.corona@loria.fr>
Mon, 18 Jul 2016 12:32:13 +0000 (14:32 +0200)
src/mc/AddressSpace.hpp

index 5c4f515..1fd9e49 100644 (file)
@@ -160,11 +160,10 @@ public:
   /** Read a string of known size */
   std::string read_string(RemotePtr<char> address, std::size_t len) const
   {
   /** Read a string of known size */
   std::string read_string(RemotePtr<char> address, std::size_t len) const
   {
-    // TODO, use std::vector with .data() in C++17 to avoid useless copies
-    std::vector<char> buffer(len);
-    buffer[len] = '\0';
-    this->read_bytes(buffer.data(), len, address);
-    return std::string(buffer.data(), buffer.size());
+    std::string res;
+    res.resize(len);
+    this->read_bytes(&res[0], len, address);
+    return res;
   }
 
 };
   }
 
 };