From d1e35c0b27ccb08313e60e6afc4517da0d801984 Mon Sep 17 00:00:00 2001 From: gmt2001 Date: Sun, 4 Aug 2013 01:54:03 -0400 Subject: [PATCH] refs #22 Fix leading space and trailing LF in real name Use proper const char* to const char* copy in NetworkVatlib::initiateConnection() --- samples/cli_client/client.cpp | 2 +- src/blackcore/network_vatlib.cpp | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/samples/cli_client/client.cpp b/samples/cli_client/client.cpp index 062608ec0..c885d0bdb 100644 --- a/samples/cli_client/client.cpp +++ b/samples/cli_client/client.cpp @@ -154,7 +154,7 @@ void Client::setCallsignCmd(QTextStream& args) void Client::setRealNameCmd(QTextStream& args) { - emit setRealName(args.readAll()); + emit setRealName(args.readLine().mid(1)); //readLine returns the entire line up to, but excluding, the LF character. mid(int) is the QString equivilent of a substring(start), used here to remove a space } void Client::initiateConnectionCmd(QTextStream&) diff --git a/src/blackcore/network_vatlib.cpp b/src/blackcore/network_vatlib.cpp index dab444836..6327a3cd6 100644 --- a/src/blackcore/network_vatlib.cpp +++ b/src/blackcore/network_vatlib.cpp @@ -166,12 +166,23 @@ namespace BlackCore { try { + //Proper way to copy a const char* to a const char*. Copy it to a char* then assign the char* to the const char* + const size_t len1 = strlen(m_callsign.toStdString().c_str()); //Get the length + char* tmp1 = new char[len1 + 1]; //Allocate the char* + strncpy(tmp1, m_callsign.toStdString().c_str(), len1); //Copy the const char* to the char* + tmp1[len1] = '\0'; //Insurance of null termination + + const size_t len2 = strlen(m_realname.toStdString().c_str()); + char* tmp2 = new char[len2 + 1]; + strncpy(tmp2, m_realname.toStdString().c_str(), len2); + tmp2[len2] = '\0'; + Cvatlib_Network::PilotConnectionInfo info; - info.callsign = m_callsign.toStdString().c_str(); - info.name = m_realname.toStdString().c_str(); + info.callsign = tmp1; //Assign char* to const char* + info.name = tmp2; info.rating = Cvatlib_Network::pilotRating_Unknown; //TODO info.sim = Cvatlib_Network::simType_XPlane; //TODO - + m_net->SetPilotLoginInfo(m_serverHost.toStdString().c_str(), m_serverPort, m_username.toStdString().c_str(), m_password.toStdString().c_str(), info); m_net->ConnectAndLogon();