From aac8c7ae8b658ad679dc9898be32f0d03b609c7b Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Mon, 18 Nov 2024 16:41:01 +0100 Subject: [PATCH] Use std::string for connection info Avoid truncation problems with the fixed size buffers. --- vncviewer/CConn.cxx | 87 +++++++++++++++++------------------------- vncviewer/CConn.h | 2 +- vncviewer/Viewport.cxx | 3 +- 3 files changed, 37 insertions(+), 55 deletions(-) diff --git a/vncviewer/CConn.cxx b/vncviewer/CConn.cxx index 31f5321d..f8e80429 100644 --- a/vncviewer/CConn.cxx +++ b/vncviewer/CConn.cxx @@ -142,72 +142,53 @@ CConn::~CConn() delete sock; } -const char *CConn::connectionInfo() +std::string CConn::connectionInfo() { - static char infoText[1024] = ""; + std::string infoText; - char scratch[100]; char pfStr[100]; - // Crude way of avoiding constant overflow checks - assert((sizeof(scratch) + 1) * 10 < sizeof(infoText)); + infoText += format(_("Desktop name: %.80s"), server.name()); + infoText += "\n"; - infoText[0] = '\0'; + infoText += format(_("Host: %.80s port: %d"), + serverHost.c_str(), serverPort); + infoText += "\n"; - snprintf(scratch, sizeof(scratch), - _("Desktop name: %.80s"), server.name()); - strcat(infoText, scratch); - strcat(infoText, "\n"); - - snprintf(scratch, sizeof(scratch), - _("Host: %.80s port: %d"), serverHost.c_str(), serverPort); - strcat(infoText, scratch); - strcat(infoText, "\n"); - - snprintf(scratch, sizeof(scratch), - _("Size: %d x %d"), server.width(), server.height()); - strcat(infoText, scratch); - strcat(infoText, "\n"); + infoText += format(_("Size: %d x %d"), + server.width(), server.height()); + infoText += "\n"; // TRANSLATORS: Will be filled in with a string describing the // protocol pixel format in a fairly language neutral way server.pf().print(pfStr, 100); - snprintf(scratch, sizeof(scratch), - _("Pixel format: %s"), pfStr); - strcat(infoText, scratch); - strcat(infoText, "\n"); + infoText += format(_("Pixel format: %s"), pfStr); + infoText += "\n"; // TRANSLATORS: Similar to the earlier "Pixel format" string serverPF.print(pfStr, 100); - snprintf(scratch, sizeof(scratch), - _("(server default %s)"), pfStr); - strcat(infoText, scratch); - strcat(infoText, "\n"); - - snprintf(scratch, sizeof(scratch), - _("Requested encoding: %s"), encodingName(getPreferredEncoding())); - strcat(infoText, scratch); - strcat(infoText, "\n"); - - snprintf(scratch, sizeof(scratch), - _("Last used encoding: %s"), encodingName(lastServerEncoding)); - strcat(infoText, scratch); - strcat(infoText, "\n"); - - snprintf(scratch, sizeof(scratch), - _("Line speed estimate: %d kbit/s"), (int)(bpsEstimate/1000)); - strcat(infoText, scratch); - strcat(infoText, "\n"); - - snprintf(scratch, sizeof(scratch), - _("Protocol version: %d.%d"), server.majorVersion, server.minorVersion); - strcat(infoText, scratch); - strcat(infoText, "\n"); - - snprintf(scratch, sizeof(scratch), - _("Security method: %s"), secTypeName(csecurity->getType())); - strcat(infoText, scratch); - strcat(infoText, "\n"); + infoText += format(_("(server default %s)"), pfStr); + infoText += "\n"; + + infoText += format(_("Requested encoding: %s"), + encodingName(getPreferredEncoding())); + infoText += "\n"; + + infoText += format(_("Last used encoding: %s"), + encodingName(lastServerEncoding)); + infoText += "\n"; + + infoText += format(_("Line speed estimate: %d kbit/s"), + (int)(bpsEstimate / 1000)); + infoText += "\n"; + + infoText += format(_("Protocol version: %d.%d"), + server.majorVersion, server.minorVersion); + infoText += "\n"; + + infoText += format(_("Security method: %s"), + secTypeName(csecurity->getType())); + infoText += "\n"; return infoText; } diff --git a/vncviewer/CConn.h b/vncviewer/CConn.h index 1e71be94..a7b9afda 100644 --- a/vncviewer/CConn.h +++ b/vncviewer/CConn.h @@ -36,7 +36,7 @@ public: CConn(const char* vncServerName, network::Socket* sock); ~CConn(); - const char *connectionInfo(); + std::string connectionInfo(); unsigned getUpdateCount(); unsigned getPixelCount(); diff --git a/vncviewer/Viewport.cxx b/vncviewer/Viewport.cxx index 6869279c..219bb57e 100644 --- a/vncviewer/Viewport.cxx +++ b/vncviewer/Viewport.cxx @@ -1385,7 +1385,8 @@ void Viewport::popupContextMenu() OptionsDialog::showDialog(); break; case ID_INFO: - if (fltk_escape(cc->connectionInfo(), buffer, sizeof(buffer)) < sizeof(buffer)) { + if (fltk_escape(cc->connectionInfo().c_str(), + buffer, sizeof(buffer)) < sizeof(buffer)) { fl_message_title(_("VNC connection info")); fl_message("%s", buffer); } -- 2.39.5