]> source.dussan.org Git - tigervnc.git/commitdiff
Use std::vector for temporary char arrays
authorPierre Ossman <ossman@cendio.se>
Sun, 15 Jan 2023 13:01:28 +0000 (14:01 +0100)
committerPierre Ossman <ossman@cendio.se>
Sat, 4 Feb 2023 13:03:13 +0000 (14:03 +0100)
It's more standard and familiar than our custom CharArray type, and it
still gives us automatic freeing of the buffer.

We could probably have used std::unique_ptr instead, but we are
currently targeting older compilers where C++11 isn't standard yet.

common/rfb/CConnection.cxx
common/rfb/CMsgReader.cxx
common/rfb/SMsgReader.cxx
win/rfb_win32/Registry.cxx
win/vncconfig/Connections.h

index da0368212909453e37acc97110d1d755683b5a5e..8e545b2fe71df4ae44d4c1f8789863c235cce8ef 100644 (file)
@@ -360,12 +360,12 @@ bool CConnection::processSecurityReasonMsg()
     return false;
   is->clearRestorePoint();
 
-  CharArray reason(len + 1);
-  is->readBytes(reason.buf, len);
-  reason.buf[len] = '\0';
+  std::vector<char> reason(len + 1);
+  is->readBytes(reason.data(), len);
+  reason[len] = '\0';
 
   state_ = RFBSTATE_INVALID;
-  throw AuthFailureException(reason.buf);
+  throw AuthFailureException(reason.data());
 }
 
 bool CConnection::processInitMsg()
index 42647119f21bd81c8d00d91f8eb464158df94dd9..0052d35e0ec653cb79b590e16b6b99f35e1171f0 100644 (file)
@@ -33,7 +33,6 @@
 #include <rfb/clipboardTypes.h>
 #include <rfb/Exception.h>
 #include <rfb/LogWriter.h>
-#include <rfb/util.h>
 #include <rfb/CMsgHandler.h>
 #include <rfb/CMsgReader.h>
 
@@ -73,10 +72,10 @@ bool CMsgReader::readServerInit()
   if (!is->hasDataOrRestore(len))
     return false;
   is->clearRestorePoint();
-  CharArray name(len + 1);
-  is->readBytes(name.buf, len);
-  name.buf[len] = '\0';
-  handler->serverInit(width, height, pf, name.buf);
+  std::vector<char> name(len + 1);
+  is->readBytes(name.data(), len);
+  name[len] = '\0';
+  handler->serverInit(width, height, pf, name.data());
 
   return true;
 }
@@ -275,9 +274,9 @@ bool CMsgReader::readServerCutText()
     vlog.error("cut text too long (%d bytes) - ignoring",len);
     return true;
   }
-  CharArray ca(len);
-  is->readBytes(ca.buf, len);
-  std::string filtered(convertLF(ca.buf, len));
+  std::vector<char> ca(len);
+  is->readBytes(ca.data(), len);
+  std::string filtered(convertLF(ca.data(), len));
   handler->serverCutText(filtered.c_str());
 
   return true;
@@ -762,14 +761,14 @@ bool CMsgReader::readSetDesktopName(int x, int y, int w, int h)
     return false;
   is->clearRestorePoint();
 
-  CharArray name(len + 1);
-  is->readBytes(name.buf, len);
-  name.buf[len] = '\0';
+  std::vector<char> name(len + 1);
+  is->readBytes(name.data(), len);
+  name[len] = '\0';
 
   if (x || y || w || h) {
     vlog.error("Ignoring DesktopName rect with non-zero position/size");
   } else {
-    handler->setName(name.buf);
+    handler->setName(name.data());
   }
 
   return true;
index 3fddf3028b691d3ab3ff1754712b0751b64f78b7..2884170470d5685a9debb71188a88a55d079302c 100644 (file)
@@ -32,7 +32,6 @@
 #include <rfb/qemuTypes.h>
 #include <rfb/clipboardTypes.h>
 #include <rfb/Exception.h>
-#include <rfb/util.h>
 #include <rfb/SMsgHandler.h>
 #include <rfb/SMsgReader.h>
 #include <rfb/Configuration.h>
@@ -314,9 +313,9 @@ bool SMsgReader::readClientCutText()
     return true;
   }
 
-  CharArray ca(len);
-  is->readBytes(ca.buf, len);
-  std::string filtered(convertLF(ca.buf, len));
+  std::vector<char> ca(len);
+  is->readBytes(ca.data(), len);
+  std::string filtered(convertLF(ca.data(), len));
   handler->clientCutText(filtered.c_str());
 
   return true;
index aa497a29c0f8092dc828913140dcfbd222908eac..b1e8b07d8f84482d20c9c3371c3c6e0b2fa42732 100644 (file)
@@ -214,40 +214,40 @@ std::string RegKey::getRepresentation(const char* valname) const {
   LONG result = RegQueryValueEx(key, valname, 0, &type, 0, &length);
   if (result != ERROR_SUCCESS)
     throw rdr::SystemException("get registry value length", result);
-  CharArray data(length);
-  result = RegQueryValueEx(key, valname, 0, &type, (BYTE*)data.buf, &length);
+  std::vector<uint8_t> data(length);
+  result = RegQueryValueEx(key, valname, 0, &type, (BYTE*)data.data(), &length);
   if (result != ERROR_SUCCESS)
     throw rdr::SystemException("get registry value", result);
 
   switch (type) {
   case REG_BINARY:
     {
-      return binToHex((const uint8_t*)data.buf, length);
+      return binToHex(data.data(), length);
     }
   case REG_SZ:
     if (length) {
-      return std::string(data.buf, length);
+      return std::string((char*)data.data(), length);
     } else {
       return "";
     }
   case REG_DWORD:
     {
       char tmp[16];
-      sprintf(tmp, "%lu", *((DWORD*)data.buf));
+      sprintf(tmp, "%lu", *((DWORD*)data.data()));
       return tmp;
     }
   case REG_EXPAND_SZ:
     {
     if (length) {
-      std::string str(data.buf, length);
+      std::string str((char*)data.data(), length);
       DWORD required = ExpandEnvironmentStrings(str.c_str(), 0, 0);
       if (required==0)
         throw rdr::SystemException("ExpandEnvironmentStrings", GetLastError());
-      CharArray result(required);
-      length = ExpandEnvironmentStrings(str.c_str(), result.buf, required);
+      std::vector<char> result(required);
+      length = ExpandEnvironmentStrings(str.c_str(), result.data(), required);
       if (required<length)
         throw rdr::Exception("unable to expand environment strings");
-      return result.buf;
+      return result.data();
     } else {
       return "";
     }
index 492525f38a74bb2f8d14f2ee2670f9f7f6050c3d..1f974454298eb16468ffd2e842d5ef94e3196be3 100644 (file)
@@ -167,13 +167,13 @@ namespace rfb {
           {
             HWND listBox = GetDlgItem(handle, IDC_HOSTS);
             int item = SendMessage(listBox, LB_GETCURSEL, 0, 0);
-            CharArray pattern(SendMessage(listBox, LB_GETTEXTLEN, item, 0)+1);
-            SendMessage(listBox, LB_GETTEXT, item, (LPARAM)pattern.buf);
+            std::vector<char> pattern(SendMessage(listBox, LB_GETTEXTLEN, item, 0)+1);
+            SendMessage(listBox, LB_GETTEXT, item, (LPARAM)pattern.data());
 
-            if (hostDialog.showDialog(pattern.buf)) {
+            if (hostDialog.showDialog(pattern.data())) {
               const char* newPat = hostDialog.getPattern();
               if (newPat) {
-                item = SendMessage(listBox, LB_FINDSTRINGEXACT, item, (LPARAM)pattern.buf);
+                item = SendMessage(listBox, LB_FINDSTRINGEXACT, item, (LPARAM)pattern.data());
                 if (item != LB_ERR) {
                   SendMessage(listBox, LB_DELETESTRING, item, 0); 
                   SendMessage(listBox, LB_INSERTSTRING, item, (LPARAM)newPat);
@@ -189,10 +189,10 @@ namespace rfb {
           {
             HWND listBox = GetDlgItem(handle, IDC_HOSTS);
             int item = SendMessage(listBox, LB_GETCURSEL, 0, 0);
-            CharArray pattern(SendMessage(listBox, LB_GETTEXTLEN, item, 0)+1);
-            SendMessage(listBox, LB_GETTEXT, item, (LPARAM)pattern.buf);
+            std::vector<char> pattern(SendMessage(listBox, LB_GETTEXTLEN, item, 0)+1);
+            SendMessage(listBox, LB_GETTEXT, item, (LPARAM)pattern.data());
             SendMessage(listBox, LB_DELETESTRING, item, 0);
-            SendMessage(listBox, LB_INSERTSTRING, item-1, (LPARAM)pattern.buf);
+            SendMessage(listBox, LB_INSERTSTRING, item-1, (LPARAM)pattern.data());
             SendMessage(listBox, LB_SETCURSEL, item-1, 0);
             onCommand(IDC_HOSTS, EN_CHANGE);
           }
@@ -202,10 +202,10 @@ namespace rfb {
           {
             HWND listBox = GetDlgItem(handle, IDC_HOSTS);
             int item = SendMessage(listBox, LB_GETCURSEL, 0, 0);
-            CharArray pattern(SendMessage(listBox, LB_GETTEXTLEN, item, 0)+1);
-            SendMessage(listBox, LB_GETTEXT, item, (LPARAM)pattern.buf);
+            std::vector<char> pattern(SendMessage(listBox, LB_GETTEXTLEN, item, 0)+1);
+            SendMessage(listBox, LB_GETTEXT, item, (LPARAM)pattern.data());
             SendMessage(listBox, LB_DELETESTRING, item, 0);
-            SendMessage(listBox, LB_INSERTSTRING, item+1, (LPARAM)pattern.buf);
+            SendMessage(listBox, LB_INSERTSTRING, item+1, (LPARAM)pattern.data());
             SendMessage(listBox, LB_SETCURSEL, item+1, 0);
             onCommand(IDC_HOSTS, EN_CHANGE);
           }