summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar guyingzhao2025-02-24 23:01:20 +0800
committerGravatar Nikias Bassen2025-03-01 23:17:04 +0100
commit02be84957d44ce68dcf81bada0d3250d4d81395a (patch)
tree9a74cbe22aced4c2b410ce883fb2834144399085
parent44099d4b79c8d6a7d599d652ebef62db8dae6696 (diff)
downloadlibplist-02be84957d44ce68dcf81bada0d3250d4d81395a.tar.gz
libplist-02be84957d44ce68dcf81bada0d3250d4d81395a.tar.bz2
C++: Fix String::GetValue memory leaking and suport assignment of const char*
-rw-r--r--include/plist/String.h1
-rw-r--r--src/String.cpp11
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 :
35 String(plist_t node, Node* parent = NULL); 35 String(plist_t node, Node* parent = NULL);
36 String(const String& s); 36 String(const String& s);
37 String& operator=(const String& s); 37 String& operator=(const String& s);
38 String& operator=(const char* s);
38 String(const std::string& s); 39 String(const std::string& s);
39 virtual ~String(); 40 virtual ~String();
40 41
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)
45 return *this; 45 return *this;
46} 46}
47 47
48String& String::operator=(const char* s)
49{
50 plist_free(_node);
51 _node = plist_new_string(s);
52 return *this;
53}
54
48String::String(const std::string& s) : Node(PLIST_STRING) 55String::String(const std::string& s) : Node(PLIST_STRING)
49{ 56{
50 plist_set_string_val(_node, s.c_str()); 57 plist_set_string_val(_node, s.c_str());
@@ -66,10 +73,8 @@ void String::SetValue(const std::string& s)
66 73
67std::string String::GetValue() const 74std::string String::GetValue() const
68{ 75{
69 char* s = NULL; 76 const char* s = plist_get_string_ptr(_node, NULL);
70 plist_get_string_val(_node, &s);
71 std::string ret = s ? s : ""; 77 std::string ret = s ? s : "";
72 delete s;
73 return ret; 78 return ret;
74} 79}
75 80