From d386bb96b0a32977c00a0a32f29c07137eb8c24b Mon Sep 17 00:00:00 2001 From: mszy Date: Wed, 22 Sep 2021 04:50:33 +0200 Subject: [PATCH] Removing memory-leak, fixes #83. (#85) Co-authored-by: morgan.szygenda --- src/v3/Action.cpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/v3/Action.cpp b/src/v3/Action.cpp index 1bcfed3..463bd31 100644 --- a/src/v3/Action.cpp +++ b/src/v3/Action.cpp @@ -40,16 +40,14 @@ const std::chrono::high_resolution_clock::time_point etcdv3::Action::startTimepo } std::string etcdv3::detail::string_plus_one(std::string const &value) { - // referred from the Go implementation in etcd. - char *s = static_cast(calloc(value.size() + 1, sizeof(char))); - std::memcpy(s, value.c_str(), value.size()); - for (int i = value.size() - 1; i >= 0; --i) { - if (static_cast(s[i]) < 0xff) { + // Referred from the Go implementation in etcd. + for (int32_t i = value.size() - 1; i >= 0; --i) { + if (static_cast(value[i]) < 0xff) { + std::string s = value.substr(0, i + 1); s[i] = s[i] + 1; - std::string ret = std::string(s, i + 1); - free(s); - return ret; + return s; } } + return {etcdv3::NUL}; }