From 9d0b3b300cd219ebec9287b3f97c3c0e220cd18d Mon Sep 17 00:00:00 2001 From: Ibrahn Sahir Date: Tue, 12 Mar 2019 12:57:22 +0000 Subject: fixed an access after free in OS_X11::set_context. Added constructor and assignment operator for CharString from const char* to simplify memory management when working with utf8/ascii strings for APIs taking char*. Reworked OS_X11::set_context to use CharString and avoid some manual memory management. --- core/ustring.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'core/ustring.cpp') diff --git a/core/ustring.cpp b/core/ustring.cpp index d60bd16921..78feddb229 100644 --- a/core/ustring.cpp +++ b/core/ustring.cpp @@ -123,6 +123,31 @@ const char *CharString::get_data() const { return ""; } +CharString &CharString::operator=(const char *p_cstr) { + + copy_from(p_cstr); + return *this; +} + +void CharString::copy_from(const char *p_cstr) { + + if (!p_cstr) { + resize(0); + return; + } + + size_t len = strlen(p_cstr); + + if (len == 0) { + resize(0); + return; + } + + resize(len + 1); // include terminating null char + + strcpy(ptrw(), p_cstr); +} + void String::copy_from(const char *p_cstr) { if (!p_cstr) { -- cgit v1.2.3