Skip to content

Commit

Permalink
Use std::string for connection info
Browse files Browse the repository at this point in the history
Avoid truncation problems with the fixed size buffers.
  • Loading branch information
CendioOssman committed Nov 18, 2024
1 parent 1e8017a commit aac8c7a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 55 deletions.
87 changes: 34 additions & 53 deletions vncviewer/CConn.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion vncviewer/CConn.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class CConn : public rfb::CConnection
CConn(const char* vncServerName, network::Socket* sock);
~CConn();

const char *connectionInfo();
std::string connectionInfo();

unsigned getUpdateCount();
unsigned getPixelCount();
Expand Down
3 changes: 2 additions & 1 deletion vncviewer/Viewport.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit aac8c7a

Please sign in to comment.