From 02be84957d44ce68dcf81bada0d3250d4d81395a Mon Sep 17 00:00:00 2001 From: guyingzhao Date: Mon, 24 Feb 2025 23:01:20 +0800 Subject: C++: Fix String::GetValue memory leaking and suport assignment of const char* --- include/plist/String.h | 1 + src/String.cpp | 11 ++++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/include/plist/String.h b/include/plist/String.h index 9aba16b..4392482 100644 --- a/include/plist/String.h +++ b/include/plist/String.h @@ -35,6 +35,7 @@ public : String(plist_t node, Node* parent = NULL); String(const String& s); String& operator=(const String& s); + String& operator=(const char* s); String(const std::string& s); virtual ~String(); diff --git a/src/String.cpp b/src/String.cpp index 2ddc28b..326aa7f 100644 --- a/src/String.cpp +++ b/src/String.cpp @@ -45,6 +45,13 @@ String& String::operator=(const PList::String& s) return *this; } +String& String::operator=(const char* s) +{ + plist_free(_node); + _node = plist_new_string(s); + return *this; +} + String::String(const std::string& s) : Node(PLIST_STRING) { plist_set_string_val(_node, s.c_str()); @@ -66,10 +73,8 @@ void String::SetValue(const std::string& s) std::string String::GetValue() const { - char* s = NULL; - plist_get_string_val(_node, &s); + const char* s = plist_get_string_ptr(_node, NULL); std::string ret = s ? s : ""; - delete s; return ret; } -- cgit v1.1-32-gdbae