1
0
mirror of https://github.com/eclipse/paho.mqtt.cpp.git synced 2025-05-09 19:31:22 +08:00

Fixed a new double-free error with user-defined persistence.

This commit is contained in:
fpagliughi 2020-12-07 22:48:49 -05:00
parent a2e6645533
commit b7b497d983
3 changed files with 8 additions and 4 deletions

View File

@ -81,8 +81,10 @@ int iclient_persistence::persistence_get(void* handle, char* key,
try {
if (handle && key && buffer && buflen) {
auto sv = static_cast<iclient_persistence*>(handle)->get(key);
*buffer = const_cast<char*>(sv.data());
*buflen = (int) sv.length();
size_t n = sv.length();
*buffer = static_cast<char*>(MQTTAsync_malloc(n));
memcpy(*buffer, sv.data(), n);
*buflen = int(n);
return MQTTASYNC_SUCCESS;
}
}

View File

@ -8,6 +8,7 @@
// The sample demonstrates:
// - Connecting to an MQTT server/broker
// - Publishing messages
// - Default file persistence
// - Last will and testament
// - Using asynchronous tokens
// - Implementing callbacks and action listeners
@ -42,6 +43,7 @@ using namespace std;
const std::string DFLT_SERVER_ADDRESS { "tcp://localhost:1883" };
const std::string DFLT_CLIENT_ID { "async_publish" };
const std::string PERSIST_DIR { "./persist" };
const string TOPIC { "hello" };
@ -127,7 +129,7 @@ int main(int argc, char* argv[])
clientID = (argc > 2) ? string(argv[2]) : DFLT_CLIENT_ID;
cout << "Initializing for server '" << address << "'..." << endl;
mqtt::async_client client(address, clientID);
mqtt::async_client client(address, clientID, PERSIST_DIR);
callback cb;
client.set_callback(cb);

View File

@ -100,7 +100,7 @@ public:
<< key << "']" << std::endl;
std::string str;
for (const auto& b : bufs)
str += b.str();
str.append(b.data(), b.size()); // += b.str();
store_[key] = std::move(str);
}