mirror of
https://github.com/eclipse/paho.mqtt.cpp.git
synced 2025-05-11 12:32:18 +08:00
Fixed a new double-free error with user-defined persistence.
This commit is contained in:
parent
a2e6645533
commit
b7b497d983
@ -81,8 +81,10 @@ int iclient_persistence::persistence_get(void* handle, char* key,
|
|||||||
try {
|
try {
|
||||||
if (handle && key && buffer && buflen) {
|
if (handle && key && buffer && buflen) {
|
||||||
auto sv = static_cast<iclient_persistence*>(handle)->get(key);
|
auto sv = static_cast<iclient_persistence*>(handle)->get(key);
|
||||||
*buffer = const_cast<char*>(sv.data());
|
size_t n = sv.length();
|
||||||
*buflen = (int) sv.length();
|
*buffer = static_cast<char*>(MQTTAsync_malloc(n));
|
||||||
|
memcpy(*buffer, sv.data(), n);
|
||||||
|
*buflen = int(n);
|
||||||
return MQTTASYNC_SUCCESS;
|
return MQTTASYNC_SUCCESS;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
// The sample demonstrates:
|
// The sample demonstrates:
|
||||||
// - Connecting to an MQTT server/broker
|
// - Connecting to an MQTT server/broker
|
||||||
// - Publishing messages
|
// - Publishing messages
|
||||||
|
// - Default file persistence
|
||||||
// - Last will and testament
|
// - Last will and testament
|
||||||
// - Using asynchronous tokens
|
// - Using asynchronous tokens
|
||||||
// - Implementing callbacks and action listeners
|
// - 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_SERVER_ADDRESS { "tcp://localhost:1883" };
|
||||||
const std::string DFLT_CLIENT_ID { "async_publish" };
|
const std::string DFLT_CLIENT_ID { "async_publish" };
|
||||||
|
const std::string PERSIST_DIR { "./persist" };
|
||||||
|
|
||||||
const string TOPIC { "hello" };
|
const string TOPIC { "hello" };
|
||||||
|
|
||||||
@ -127,7 +129,7 @@ int main(int argc, char* argv[])
|
|||||||
clientID = (argc > 2) ? string(argv[2]) : DFLT_CLIENT_ID;
|
clientID = (argc > 2) ? string(argv[2]) : DFLT_CLIENT_ID;
|
||||||
|
|
||||||
cout << "Initializing for server '" << address << "'..." << endl;
|
cout << "Initializing for server '" << address << "'..." << endl;
|
||||||
mqtt::async_client client(address, clientID);
|
mqtt::async_client client(address, clientID, PERSIST_DIR);
|
||||||
|
|
||||||
callback cb;
|
callback cb;
|
||||||
client.set_callback(cb);
|
client.set_callback(cb);
|
||||||
|
@ -100,7 +100,7 @@ public:
|
|||||||
<< key << "']" << std::endl;
|
<< key << "']" << std::endl;
|
||||||
std::string str;
|
std::string str;
|
||||||
for (const auto& b : bufs)
|
for (const auto& b : bufs)
|
||||||
str += b.str();
|
str.append(b.data(), b.size()); // += b.str();
|
||||||
store_[key] = std::move(str);
|
store_[key] = std::move(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user