From: Pierre Ossman Date: Tue, 17 Jan 2023 15:38:59 +0000 (+0100) Subject: Use std::string for string memory management X-Git-Tag: v1.13.90~87^2~9 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=adaedc9629c5f4b124a1872bac6a11844b7f576e;p=tigervnc.git Use std::string for string memory management Avoids a bit of complexity by delegating that handling to a string object. --- diff --git a/common/rfb/Blacklist.cxx b/common/rfb/Blacklist.cxx index f052eafb..12a54c45 100644 --- a/common/rfb/Blacklist.cxx +++ b/common/rfb/Blacklist.cxx @@ -45,11 +45,6 @@ Blacklist::Blacklist() { } Blacklist::~Blacklist() { - // Free the map keys - BlacklistMap::iterator i; - for (i=blm.begin(); i!=blm.end(); i++) { - strFree((char*)(*i).first); - } } bool Blacklist::isBlackmarked(const char* name) { @@ -65,7 +60,7 @@ bool Blacklist::isBlackmarked(const char* name) { bi.marks = 1; bi.blockUntil = 0; bi.blockTimeout = initialTimeout; - blm[strDup(name)] = bi; + blm[name] = bi; i = blm.find(name); } @@ -92,9 +87,5 @@ bool Blacklist::isBlackmarked(const char* name) { } void Blacklist::clearBlackmark(const char* name) { - BlacklistMap::iterator i = blm.find(name); - if (i != blm.end()) { - strFree((char*)(*i).first); - blm.erase(i); - } + blm.erase(name); } diff --git a/common/rfb/Blacklist.h b/common/rfb/Blacklist.h index 45e36a0e..c1699f29 100644 --- a/common/rfb/Blacklist.h +++ b/common/rfb/Blacklist.h @@ -30,9 +30,9 @@ #include #include #include +#include #include -#include namespace rfb { @@ -68,17 +68,12 @@ namespace rfb { void clearBlackmark(const char* name); protected: - struct ltStr { - bool operator()(const char* s1, const char* s2) const { - return strcmp(s1, s2) < 0; - }; - }; struct BlacklistInfo { int marks; time_t blockUntil; unsigned int blockTimeout; }; - typedef std::map BlacklistMap; + typedef std::map BlacklistMap; BlacklistMap blm; }; diff --git a/common/rfb/CConnection.cxx b/common/rfb/CConnection.cxx index 79adec5c..56429c98 100644 --- a/common/rfb/CConnection.cxx +++ b/common/rfb/CConnection.cxx @@ -59,7 +59,7 @@ CConnection::CConnection() firstUpdate(true), pendingUpdate(false), continuousUpdates(false), forceNonincremental(true), framebuffer(NULL), decoder(this), - serverClipboard(NULL), hasLocalClipboard(false) + hasRemoteClipboard(false), hasLocalClipboard(false) { } @@ -405,8 +405,6 @@ void CConnection::close() reader_ = NULL; delete writer_; writer_ = NULL; - strFree(serverClipboard); - serverClipboard = NULL; } void CConnection::setDesktopSize(int w, int h) @@ -545,10 +543,8 @@ void CConnection::serverCutText(const char* str) { hasLocalClipboard = false; - strFree(serverClipboard); - serverClipboard = NULL; - - serverClipboard = strDup(latin1ToUTF8(str).c_str()); + serverClipboard = latin1ToUTF8(str); + hasRemoteClipboard = true; handleClipboardAnnounce(true); } @@ -589,8 +585,7 @@ void CConnection::handleClipboardPeek() void CConnection::handleClipboardNotify(uint32_t flags) { - strFree(serverClipboard); - serverClipboard = NULL; + hasRemoteClipboard = false; if (flags & rfb::clipboardUTF8) { hasLocalClipboard = false; @@ -609,14 +604,11 @@ void CConnection::handleClipboardProvide(uint32_t flags, return; } - strFree(serverClipboard); - serverClipboard = NULL; - - std::string filtered(convertLF((const char*)data[0], lengths[0])); - serverClipboard = strDup(filtered.c_str()); + serverClipboard = convertLF((const char*)data[0], lengths[0]); + hasRemoteClipboard = true; // FIXME: Should probably verify that this data was actually requested - handleClipboardData(serverClipboard); + handleClipboardData(serverClipboard.c_str()); } void CConnection::authSuccess() @@ -646,8 +638,8 @@ void CConnection::handleClipboardData(const char* /*data*/) void CConnection::requestClipboard() { - if (serverClipboard != NULL) { - handleClipboardData(serverClipboard); + if (hasRemoteClipboard) { + handleClipboardData(serverClipboard.c_str()); return; } diff --git a/common/rfb/CConnection.h b/common/rfb/CConnection.h index 0cf47962..71da175e 100644 --- a/common/rfb/CConnection.h +++ b/common/rfb/CConnection.h @@ -24,6 +24,8 @@ #ifndef __RFB_CCONNECTION_H__ #define __RFB_CCONNECTION_H__ +#include + #include #include #include @@ -292,7 +294,8 @@ namespace rfb { ModifiablePixelBuffer* framebuffer; DecodeManager decoder; - char* serverClipboard; + std::string serverClipboard; + bool hasRemoteClipboard; bool hasLocalClipboard; bool unsolicitedClipboardAttempt; }; diff --git a/common/rfb/ClientParams.cxx b/common/rfb/ClientParams.cxx index 572e2d82..ade99018 100644 --- a/common/rfb/ClientParams.cxx +++ b/common/rfb/ClientParams.cxx @@ -34,7 +34,7 @@ ClientParams::ClientParams() : majorVersion(0), minorVersion(0), compressLevel(2), qualityLevel(-1), fineQualityLevel(-1), subsampling(subsampleUndefined), - width_(0), height_(0), name_(0), + width_(0), height_(0), cursorPos_(0, 0), ledState_(ledUnknown) { setName(""); @@ -49,7 +49,6 @@ ClientParams::ClientParams() ClientParams::~ClientParams() { - delete [] name_; delete cursor_; } @@ -80,8 +79,7 @@ void ClientParams::setPF(const PixelFormat& pf) void ClientParams::setName(const char* name) { - delete [] name_; - name_ = strDup(name); + name_ = name; } void ClientParams::setCursor(const Cursor& other) diff --git a/common/rfb/ClientParams.h b/common/rfb/ClientParams.h index 0f4171cc..ea86ea78 100644 --- a/common/rfb/ClientParams.h +++ b/common/rfb/ClientParams.h @@ -24,6 +24,7 @@ #define __RFB_CLIENTPARAMS_H__ #include +#include #include @@ -72,7 +73,7 @@ namespace rfb { const PixelFormat& pf() const { return pf_; } void setPF(const PixelFormat& pf); - const char* name() const { return name_; } + const char* name() const { return name_.c_str(); } void setName(const char* name); const Cursor& cursor() const { return *cursor_; } @@ -113,7 +114,7 @@ namespace rfb { ScreenSet screenLayout_; PixelFormat pf_; - char* name_; + std::string name_; Cursor* cursor_; Point cursorPos_; std::set encodings_; diff --git a/common/rfb/Configuration.cxx b/common/rfb/Configuration.cxx index 0fc2bde6..1c215c7f 100644 --- a/common/rfb/Configuration.cxx +++ b/common/rfb/Configuration.cxx @@ -372,7 +372,7 @@ IntParameter::operator int() const { StringParameter::StringParameter(const char* name_, const char* desc_, const char* v, ConfigurationObject co) - : VoidParameter(name_, desc_, co), value(strDup(v)), def_value(strDup(v)) + : VoidParameter(name_, desc_, co), value(v), def_value(v) { if (!v) { vlog.error("Default value for %s not allowed",name_); @@ -381,8 +381,6 @@ StringParameter::StringParameter(const char* name_, const char* desc_, } StringParameter::~StringParameter() { - strFree(value); - strFree(def_value); } bool StringParameter::setParam(const char* v) { @@ -391,9 +389,8 @@ bool StringParameter::setParam(const char* v) { if (!v) throw rfb::Exception("setParam() not allowed"); vlog.debug("set %s(String) to %s", getName(), v); - CharArray oldValue(value); - value = strDup(v); - return value != 0; + value = v; + return true; } std::string StringParameter::getDefaultStr() const { @@ -402,11 +399,11 @@ std::string StringParameter::getDefaultStr() const { std::string StringParameter::getValueStr() const { LOCK_CONFIG; - return std::string(value); + return value; } StringParameter::operator const char *() const { - return value; + return value.c_str(); } // -=- BinaryParameter diff --git a/common/rfb/Configuration.h b/common/rfb/Configuration.h index eef89d93..d73d8005 100644 --- a/common/rfb/Configuration.h +++ b/common/rfb/Configuration.h @@ -250,8 +250,8 @@ namespace rfb { virtual std::string getValueStr() const; operator const char*() const; protected: - char* value; - char* def_value; + std::string value; + std::string def_value; }; class BinaryParameter : public VoidParameter { diff --git a/common/rfb/KeyRemapper.cxx b/common/rfb/KeyRemapper.cxx index 731eb256..762eb413 100644 --- a/common/rfb/KeyRemapper.cxx +++ b/common/rfb/KeyRemapper.cxx @@ -87,7 +87,7 @@ class KeyMapParameter : public StringParameter { public: KeyMapParameter() : StringParameter("RemapKeys", "Comma-separated list of incoming keysyms to remap. Mappings are expressed as two hex values, prefixed by 0x, and separated by ->", "") { - setParam(value); + KeyRemapper::defInstance.setMapping(""); } bool setParam(const char* v) { KeyRemapper::defInstance.setMapping(v); diff --git a/common/rfb/SConnection.cxx b/common/rfb/SConnection.cxx index 244fb324..a1755cb4 100644 --- a/common/rfb/SConnection.cxx +++ b/common/rfb/SConnection.cxx @@ -60,7 +60,8 @@ SConnection::SConnection() is(0), os(0), reader_(0), writer_(0), ssecurity(0), authFailureTimer(this, &SConnection::handleAuthFailureTimeout), state_(RFBSTATE_UNINITIALISED), preferredEncoding(encodingRaw), - accessRights(0x0000), clientClipboard(NULL), hasLocalClipboard(false), + accessRights(0x0000), hasRemoteClipboard(false), + hasLocalClipboard(false), unsolicitedClipboardAttempt(false) { defaultMajorVersion = 3; @@ -380,10 +381,8 @@ void SConnection::clientCutText(const char* str) { hasLocalClipboard = false; - strFree(clientClipboard); - clientClipboard = NULL; - - clientClipboard = strDup(latin1ToUTF8(str).c_str()); + clientClipboard = latin1ToUTF8(str); + hasRemoteClipboard = true; handleClipboardAnnounce(true); } @@ -409,8 +408,7 @@ void SConnection::handleClipboardPeek() void SConnection::handleClipboardNotify(uint32_t flags) { - strFree(clientClipboard); - clientClipboard = NULL; + hasRemoteClipboard = false; if (flags & rfb::clipboardUTF8) { hasLocalClipboard = false; @@ -429,14 +427,11 @@ void SConnection::handleClipboardProvide(uint32_t flags, return; } - strFree(clientClipboard); - clientClipboard = NULL; - - std::string filtered(convertLF((const char*)data[0], lengths[0])); - clientClipboard = strDup(filtered.c_str()); + clientClipboard = convertLF((const char*)data[0], lengths[0]); + hasRemoteClipboard = true; // FIXME: Should probably verify that this data was actually requested - handleClipboardData(clientClipboard); + handleClipboardData(clientClipboard.c_str()); } void SConnection::supportsQEMUKeyEvent() @@ -554,8 +549,8 @@ void SConnection::handleClipboardData(const char* /*data*/) void SConnection::requestClipboard() { - if (clientClipboard != NULL) { - handleClipboardData(clientClipboard); + if (hasRemoteClipboard) { + handleClipboardData(clientClipboard.c_str()); return; } @@ -624,8 +619,6 @@ void SConnection::cleanup() reader_ = NULL; delete writer_; writer_ = NULL; - strFree(clientClipboard); - clientClipboard = NULL; } void SConnection::writeFakeColourMap(void) diff --git a/common/rfb/SConnection.h b/common/rfb/SConnection.h index 54acdd9c..08574069 100644 --- a/common/rfb/SConnection.h +++ b/common/rfb/SConnection.h @@ -24,6 +24,8 @@ #ifndef __RFB_SCONNECTION_H__ #define __RFB_SCONNECTION_H__ +#include + #include #include @@ -263,7 +265,8 @@ namespace rfb { int32_t preferredEncoding; AccessRights accessRights; - char* clientClipboard; + std::string clientClipboard; + bool hasRemoteClipboard; bool hasLocalClipboard; bool unsolicitedClipboardAttempt; }; diff --git a/common/rfb/ServerParams.cxx b/common/rfb/ServerParams.cxx index 62fbde91..6af446c2 100644 --- a/common/rfb/ServerParams.cxx +++ b/common/rfb/ServerParams.cxx @@ -33,7 +33,7 @@ ServerParams::ServerParams() supportsQEMUKeyEvent(false), supportsSetDesktopSize(false), supportsFence(false), supportsContinuousUpdates(false), - width_(0), height_(0), name_(0), + width_(0), height_(0), ledState_(ledUnknown) { setName(""); @@ -46,7 +46,6 @@ ServerParams::ServerParams() ServerParams::~ServerParams() { - delete [] name_; delete cursor_; } @@ -77,8 +76,7 @@ void ServerParams::setPF(const PixelFormat& pf) void ServerParams::setName(const char* name) { - delete [] name_; - name_ = strDup(name); + name_ = name; } void ServerParams::setCursor(const Cursor& other) diff --git a/common/rfb/ServerParams.h b/common/rfb/ServerParams.h index 4feacf18..791e3e7f 100644 --- a/common/rfb/ServerParams.h +++ b/common/rfb/ServerParams.h @@ -23,6 +23,8 @@ #ifndef __RFB_SERVERPARAMS_H__ #define __RFB_SERVERPARAMS_H__ +#include + #include #include #include @@ -60,7 +62,7 @@ namespace rfb { const PixelFormat& pf() const { return pf_; } void setPF(const PixelFormat& pf); - const char* name() const { return name_; } + const char* name() const { return name_.c_str(); } void setName(const char* name); const Cursor& cursor() const { return *cursor_; } @@ -85,7 +87,7 @@ namespace rfb { ScreenSet screenLayout_; PixelFormat pf_; - char* name_; + std::string name_; Cursor* cursor_; unsigned int ledState_; uint32_t clipFlags; diff --git a/vncviewer/MonitorIndicesParameter.cxx b/vncviewer/MonitorIndicesParameter.cxx index 691a8470..dcc762a3 100644 --- a/vncviewer/MonitorIndicesParameter.cxx +++ b/vncviewer/MonitorIndicesParameter.cxx @@ -53,7 +53,7 @@ std::set MonitorIndicesParameter::getParam() return indices; } - valid = parseIndices(value, &configIndices); + valid = parseIndices(value.c_str(), &configIndices); if (!valid) { return indices; } diff --git a/vncviewer/ServerDialog.cxx b/vncviewer/ServerDialog.cxx index ec1e40e4..144f31a7 100644 --- a/vncviewer/ServerDialog.cxx +++ b/vncviewer/ServerDialog.cxx @@ -38,7 +38,6 @@ #include #include #include -#include #include "fltk/layout.h" #include "ServerDialog.h" @@ -62,8 +61,6 @@ ServerDialog::ServerDialog() Fl_Button *button; Fl_Box *divider; - usedDir = NULL; - x = OUTER_MARGIN; y = OUTER_MARGIN; @@ -120,8 +117,6 @@ ServerDialog::ServerDialog() ServerDialog::~ServerDialog() { - if (usedDir) - free(usedDir); } @@ -168,10 +163,11 @@ void ServerDialog::handleLoad(Fl_Widget* /*widget*/, void* data) { ServerDialog *dialog = (ServerDialog*)data; - if (!dialog->usedDir) - dialog->usedDir = strDup(os::getuserhomedir()); + if (dialog->usedDir.empty()) + dialog->usedDir = os::getuserhomedir(); - Fl_File_Chooser* file_chooser = new Fl_File_Chooser(dialog->usedDir, _("TigerVNC configuration (*.tigervnc)"), + Fl_File_Chooser* file_chooser = new Fl_File_Chooser(dialog->usedDir.c_str(), + _("TigerVNC configuration (*.tigervnc)"), 0, _("Select a TigerVNC configuration file")); file_chooser->preview(0); file_chooser->previewButton->hide(); @@ -207,10 +203,11 @@ void ServerDialog::handleSaveAs(Fl_Widget* /*widget*/, void* data) ServerDialog *dialog = (ServerDialog*)data; const char* servername = dialog->serverName->value(); const char* filename; - if (!dialog->usedDir) - dialog->usedDir = strDup(os::getuserhomedir()); + if (dialog->usedDir.empty()) + dialog->usedDir = os::getuserhomedir(); - Fl_File_Chooser* file_chooser = new Fl_File_Chooser(dialog->usedDir, _("TigerVNC configuration (*.tigervnc)"), + Fl_File_Chooser* file_chooser = new Fl_File_Chooser(dialog->usedDir.c_str(), + _("TigerVNC configuration (*.tigervnc)"), 2, _("Save the TigerVNC configuration to file")); file_chooser->preview(0); @@ -405,6 +402,6 @@ void ServerDialog::saveServerHistory() void ServerDialog::updateUsedDir(const char* filename) { char * name = strdup(filename); - usedDir = strdup(dirname(name)); + usedDir = dirname(name); free(name); } diff --git a/vncviewer/ServerDialog.h b/vncviewer/ServerDialog.h index f9c3d136..a76a58cf 100644 --- a/vncviewer/ServerDialog.h +++ b/vncviewer/ServerDialog.h @@ -50,7 +50,7 @@ private: protected: Fl_Input_Choice *serverName; std::vector serverHistory; - char *usedDir; + std::string usedDir; }; #endif diff --git a/win/winvnc/ListConnInfo.h b/win/winvnc/ListConnInfo.h index f779cf5d..1926b8a7 100644 --- a/win/winvnc/ListConnInfo.h +++ b/win/winvnc/ListConnInfo.h @@ -21,8 +21,7 @@ #define __RFB_LISTCONNINFO_INCLUDED__ #include - -#include +#include namespace winvnc { @@ -53,12 +52,12 @@ namespace winvnc { void addInfo(void* Conn, const char* IP, int Status) { conn.push_back(Conn); - IP_address.push_back(rfb::strDup(IP)); + IP_address.push_back(IP); status.push_back(Status); } void iGetCharInfo(const char* buf[2]) { - buf[0] = *Ii; + buf[0] = Ii->c_str(); switch (*si) { case 0: buf[1] = "Full control"; @@ -107,10 +106,10 @@ namespace winvnc { private: std::list conn; - std::list IP_address; + std::list IP_address; std::list status; std::list::iterator ci; - std::list::iterator Ii; + std::list::iterator Ii; std::list::iterator si; bool disableClients; };