diff options
author | Pierre Ossman <ossman@cendio.se> | 2023-01-15 16:55:34 +0100 |
---|---|---|
committer | Pierre Ossman <ossman@cendio.se> | 2023-02-04 14:03:13 +0100 |
commit | 2047dae22fb862ff43309ebb4fe2b9e7d6ce6153 (patch) | |
tree | 975687d97e31e677f827e0f586a349868e6a1e65 | |
parent | ea6afa9b791e3782455a829f97515fa721edd522 (diff) | |
download | tigervnc-2047dae22fb862ff43309ebb4fe2b9e7d6ce6153.tar.gz tigervnc-2047dae22fb862ff43309ebb4fe2b9e7d6ce6153.zip |
Return std::vector instead of dynamic allocation
This makes memory management more clear and robust when using these
helper functions.
-rw-r--r-- | common/rfb/Configuration.cxx | 17 | ||||
-rw-r--r-- | common/rfb/Configuration.h | 6 | ||||
-rw-r--r-- | common/rfb/SSecurityVncAuth.cxx | 7 | ||||
-rw-r--r-- | common/rfb/util.cxx | 10 | ||||
-rw-r--r-- | common/rfb/util.h | 4 | ||||
-rw-r--r-- | win/rfb_win32/Registry.cxx | 20 | ||||
-rw-r--r-- | win/rfb_win32/Registry.h | 4 | ||||
-rw-r--r-- | win/vncconfig/Legacy.cxx | 7 |
8 files changed, 35 insertions, 40 deletions
diff --git a/common/rfb/Configuration.cxx b/common/rfb/Configuration.cxx index 629214e4..1d4c7a45 100644 --- a/common/rfb/Configuration.cxx +++ b/common/rfb/Configuration.cxx @@ -435,11 +435,10 @@ BinaryParameter::~BinaryParameter() { bool BinaryParameter::setParam(const char* v) { if (immutable) return true; - uint8_t *newValue = hexToBin(v, strlen(v)); - if (newValue == NULL) + std::vector<uint8_t> newValue = hexToBin(v, strlen(v)); + if (newValue.empty() && strlen(v) > 0) return false; - setParam(newValue, strlen(v)/2); - delete [] newValue; + setParam(newValue.data(), newValue.size()); return true; } @@ -467,11 +466,9 @@ char* BinaryParameter::getValueStr() const { return binToHex(value, length); } -void BinaryParameter::getData(uint8_t** data_, size_t* length_) const { +std::vector<uint8_t> BinaryParameter::getData() const { LOCK_CONFIG; - if (length_) *length_ = length; - if (data_) { - *data_ = new uint8_t[length]; - memcpy(*data_, value, length); - } + std::vector<uint8_t> out(length); + memcpy(out.data(), value, length); + return out; } diff --git a/common/rfb/Configuration.h b/common/rfb/Configuration.h index 9e86dcc9..318b6b8d 100644 --- a/common/rfb/Configuration.h +++ b/common/rfb/Configuration.h @@ -44,6 +44,8 @@ #ifndef __RFB_CONFIGURATION_H__ #define __RFB_CONFIGURATION_H__ +#include <vector> + #include <rfb/util.h> namespace os { class Mutex; } @@ -266,9 +268,7 @@ namespace rfb { virtual char* getDefaultStr() const; virtual char* getValueStr() const; - // getData() will return length zero if there is no data - // NB: data may be set to zero, OR set to a zero-length buffer - void getData(uint8_t** data, size_t* length) const; + std::vector<uint8_t> getData() const; protected: uint8_t* value; diff --git a/common/rfb/SSecurityVncAuth.cxx b/common/rfb/SSecurityVncAuth.cxx index b70f0668..d035f97f 100644 --- a/common/rfb/SSecurityVncAuth.cxx +++ b/common/rfb/SSecurityVncAuth.cxx @@ -123,7 +123,12 @@ VncAuthPasswdParameter::VncAuthPasswdParameter(const char* name, void VncAuthPasswdParameter::getVncAuthPasswd(PlainPasswd *password, PlainPasswd *readOnlyPassword) { ObfuscatedPasswd obfuscated, obfuscatedReadOnly; - getData((uint8_t**)&obfuscated.buf, &obfuscated.length); + std::vector<uint8_t> data = getData(); + obfuscated.length = data.size(); + if (!data.empty()) { + obfuscated.buf = new char[data.size()]; + memcpy(obfuscated.buf, data.data(), data.size()); + } if (obfuscated.length == 0) { if (passwdFile) { diff --git a/common/rfb/util.cxx b/common/rfb/util.cxx index 32be6fdd..cfc694e3 100644 --- a/common/rfb/util.cxx +++ b/common/rfb/util.cxx @@ -178,12 +178,10 @@ namespace rfb { return true; } - uint8_t* hexToBin(const char* in, size_t inlen) { - uint8_t* out = new uint8_t[inlen/2]; - if (!hexToBin(in, inlen, out, inlen/2)) { - delete [] out; - return NULL; - } + std::vector<uint8_t> hexToBin(const char* in, size_t inlen) { + std::vector<uint8_t> out(inlen/2); + if (!hexToBin(in, inlen, out.data(), inlen/2)) + return std::vector<uint8_t>(); return out; } diff --git a/common/rfb/util.h b/common/rfb/util.h index bac30f3f..7f65fa87 100644 --- a/common/rfb/util.h +++ b/common/rfb/util.h @@ -28,6 +28,8 @@ #include <string.h> #include <stdint.h> +#include <vector> + struct timeval; namespace rfb { @@ -79,7 +81,7 @@ namespace rfb { void binToHex(const uint8_t* in, size_t inlen, char* out, size_t outlen); char* binToHex(const uint8_t* in, size_t inlen); bool hexToBin(const char* in, size_t inlen, uint8_t* out, size_t outlen); - uint8_t* hexToBin(const char* in, size_t inlen); + std::vector<uint8_t> hexToBin(const char* in, size_t inlen); // Makes sure line endings are in a certain format diff --git a/win/rfb_win32/Registry.cxx b/win/rfb_win32/Registry.cxx index cb4e6688..78879c3b 100644 --- a/win/rfb_win32/Registry.cxx +++ b/win/rfb_win32/Registry.cxx @@ -173,23 +173,17 @@ TCHAR* RegKey::getString(const TCHAR* valname, const TCHAR* def) const { } } -void RegKey::getBinary(const TCHAR* valname, void** data, size_t* length) const { +std::vector<uint8_t> RegKey::getBinary(const TCHAR* valname) const { TCharArray hex(getRepresentation(valname)); - *data = hexToBin(CStr(hex.buf), strlen(CStr(hex.buf))); - *length = strlen(CStr(hex.buf))/2; - if (*data == NULL) - throw rdr::Exception("getBinary failed"); + return hexToBin(CStr(hex.buf), strlen(CStr(hex.buf))); } -void RegKey::getBinary(const TCHAR* valname, void** data, size_t* length, void* def, size_t deflen) const { +std::vector<uint8_t> RegKey::getBinary(const TCHAR* valname, const uint8_t* def, size_t deflen) const { try { - getBinary(valname, data, length); + return getBinary(valname); } catch(rdr::Exception&) { - if (deflen) { - *data = new char[deflen]; - memcpy(*data, def, deflen); - } else - *data = 0; - *length = deflen; + std::vector<uint8_t> out(deflen); + memcpy(out.data(), def, deflen); + return out; } } diff --git a/win/rfb_win32/Registry.h b/win/rfb_win32/Registry.h index 2bb16911..3b87983b 100644 --- a/win/rfb_win32/Registry.h +++ b/win/rfb_win32/Registry.h @@ -78,8 +78,8 @@ namespace rfb { TCHAR* getString(const TCHAR* valname) const; TCHAR* getString(const TCHAR* valname, const TCHAR* def) const; - void getBinary(const TCHAR* valname, void** data, size_t* length) const; - void getBinary(const TCHAR* valname, void** data, size_t* length, void* def, size_t deflength) const; + std::vector<uint8_t> getBinary(const TCHAR* valname) const; + std::vector<uint8_t> getBinary(const TCHAR* valname, const uint8_t* def, size_t deflength) const; int getInt(const TCHAR* valname) const; int getInt(const TCHAR* valname, int def) const; diff --git a/win/vncconfig/Legacy.cxx b/win/vncconfig/Legacy.cxx index deba0ff3..3edfd4f7 100644 --- a/win/vncconfig/Legacy.cxx +++ b/win/vncconfig/Legacy.cxx @@ -19,7 +19,6 @@ #include <vncconfig/Legacy.h> #include <rfb/LogWriter.h> -#include <rfb/Password.h> #include <rfb_win32/CurrentUser.h> using namespace rfb; @@ -212,9 +211,9 @@ void LegacyPage::LoadPrefs() } regKey.setInt(_T("QueryTimeout"), key.getInt(_T("QueryTimeout"), 10)); - ObfuscatedPasswd passwd; - key.getBinary(_T("Password"), (void**)&passwd.buf, &passwd.length, 0, 0); - regKey.setBinary(_T("Password"), passwd.buf, passwd.length); + std::vector<uint8_t> passwd; + passwd = key.getBinary(_T("Password")); + regKey.setBinary(_T("Password"), passwd.data(), passwd.size()); bool enableInputs = key.getBool(_T("InputsEnabled"), true); regKey.setBool(_T("AcceptKeyEvents"), enableInputs); |