From 9ba63d1460db41c219b638212b42476164fcfdff Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 24 Jul 2018 13:08:25 +0200 Subject: Update code style, fix compiler warnings - Use nullptr instead of NULL for better warnings in case of mistakes - Get rid of VLAs which are not in C++11 actually - Fix implicit signed <-> unsigned mismatches by adding checks and casts --- src/client/clientapp/clientapp.cpp | 8 ++++---- src/client/clientapp/clientapp.h | 2 +- src/client/connectwindow/connectwindow.cpp | 8 ++++---- src/client/connectwindow/connectwindow.h | 2 +- src/client/net/serverconnection.cpp | 28 ++++++++++++++-------------- src/client/net/serverconnection.h | 2 +- src/client/net/serverdiscovery.cpp | 4 ++-- src/client/toolbar/toolbar.cpp | 10 +++++----- src/client/util/platform/blankscreen.cpp | 12 ++++++------ src/client/vnc/vncserver.cpp | 22 +++++++++++----------- src/client/vnc/vncthread.cpp | 23 ++++++++++++++--------- src/client/vnc/vncwindow.cpp | 20 ++++++++++---------- 12 files changed, 73 insertions(+), 68 deletions(-) (limited to 'src/client') diff --git a/src/client/clientapp/clientapp.cpp b/src/client/clientapp/clientapp.cpp index 2f65bd1..962ec42 100644 --- a/src/client/clientapp/clientapp.cpp +++ b/src/client/clientapp/clientapp.cpp @@ -5,7 +5,7 @@ #include "../net/serverconnection.h" ClientApp::ClientApp(int& argc, char** argv) - : QApplication(argc, argv), _connectionMode(ConnectionMode::None), _examMode(false), _connection(NULL), _isManagerPc(false) + : QApplication(argc, argv), _connectionMode(ConnectionMode::None), _examMode(false), _connection(nullptr), _isManagerPc(false) { /* some values */ setOrganizationName("openslx"); @@ -31,7 +31,7 @@ ClientApp::ClientApp(int& argc, char** argv) readIsManagerPc(); - _connectWindow = new ConnectWindow(NULL); + _connectWindow = new ConnectWindow(nullptr); connect(_connectWindow, SIGNAL(connected(ServerConnection*)), this, SLOT(connected(ServerConnection*))); if (_connectionMode == ConnectionMode::Auto) { _toolbar = new Toolbar(true); // auto connect client without session ID. @@ -114,10 +114,10 @@ void ClientApp::connected(ServerConnection* connection) void ClientApp::disconnected(ServerConnection* connection) { - if (connection != NULL) + if (connection != nullptr) connection->blockSignals(true); if (_connection == connection) { - _connection = NULL; + _connection = nullptr; } } diff --git a/src/client/clientapp/clientapp.h b/src/client/clientapp/clientapp.h index 0f7cb5b..91dccf3 100644 --- a/src/client/clientapp/clientapp.h +++ b/src/client/clientapp/clientapp.h @@ -58,7 +58,7 @@ public: bool isConfiguredAsManager() { return _isManagerPc; } - bool isConnectedToLocalManager() { return _connection != NULL && _connection->isLocalConnection(); } + bool isConnectedToLocalManager() { return _connection != nullptr && _connection->isLocalConnection(); } private slots: diff --git a/src/client/connectwindow/connectwindow.cpp b/src/client/connectwindow/connectwindow.cpp index b1f583a..c77a5e6 100644 --- a/src/client/connectwindow/connectwindow.cpp +++ b/src/client/connectwindow/connectwindow.cpp @@ -27,7 +27,7 @@ ConnectWindow::ConnectWindow(QWidget *parent) : QWidget(parent) _timerHide = 0; _state = Idle; _hashSslErrorCount = 0; - _pendingConnection = NULL; + _pendingConnection = nullptr; // Initialize the GUI _ui->setupUi(this); @@ -320,7 +320,7 @@ void ConnectWindow::onConnectionStateChange(ConnectWindow::ConnectionState state QObject::disconnect(_pendingConnection, SIGNAL(stateChange(ConnectWindow::ConnectionState)), this, SLOT(onConnectionStateChange(ConnectWindow::ConnectionState))); QObject::disconnect(_pendingConnection, SIGNAL(destroyed(QObject*)), this, SLOT(onConnectionClosed(QObject*))); emit connected(_pendingConnection); - _pendingConnection = NULL; + _pendingConnection = nullptr; this->updateUserInterface(); @@ -341,12 +341,12 @@ void ConnectWindow::onComboBox_keyPressed(QKeyEvent* e) } /***************************************************************************//** - * If connection is closed set _pendingConnection = NULL. + * If connection is closed set _pendingConnection = nullptr. * @param connection */ void ConnectWindow::onConnectionClosed(QObject* /* connection */ ) { - _pendingConnection = NULL; + _pendingConnection = nullptr; } /***************************************************************************//** diff --git a/src/client/connectwindow/connectwindow.h b/src/client/connectwindow/connectwindow.h index 51b1bf2..af45db6 100644 --- a/src/client/connectwindow/connectwindow.h +++ b/src/client/connectwindow/connectwindow.h @@ -54,7 +54,7 @@ public: Connected }; - explicit ConnectWindow(QWidget *parent = NULL); + explicit ConnectWindow(QWidget *parent = nullptr); virtual ~ConnectWindow(); void connectToSession(const QByteArray sessionName, QString mgrIP); diff --git a/src/client/net/serverconnection.cpp b/src/client/net/serverconnection.cpp index 0cbd4c2..6350418 100644 --- a/src/client/net/serverconnection.cpp +++ b/src/client/net/serverconnection.cpp @@ -19,7 +19,7 @@ #define CHALLENGE_LEN 20 ServerConnection::ServerConnection(const QString& host, const quint16 port, const QByteArray& sessionName, const QByteArray& certHash, bool autoConnect) : - QObject(NULL), _timerDelete(0), _jpegQuality(80), _authed(0), _autoConnect(autoConnect), _isLocalConnection(-1), _sessionName(sessionName), _certHash(certHash) + QObject(nullptr), _timerDelete(0), _jpegQuality(80), _authed(0), _autoConnect(autoConnect), _isLocalConnection(-1), _sessionName(sessionName), _certHash(certHash) { _socket = new QSslSocket(); _blank = new BlankScreen(); @@ -46,13 +46,13 @@ ServerConnection::ServerConnection(const QString& host, const quint16 port, cons ServerConnection::~ServerConnection() { this->disconnectFromServer(); - if (_socket != NULL) { + if (_socket != nullptr) { qCritical("**** SOCKET DELETE IN DESTRUCTOR"); _socket->deleteLater(); } qDebug("*** Server connection destroyed."); _blank->deleteLater(); - _blank = NULL; + _blank = nullptr; } /** @@ -60,7 +60,7 @@ ServerConnection::~ServerConnection() */ void ServerConnection::sendMessage(NetworkMessage& message) { - if (_socket == NULL || _socket->state() != QAbstractSocket::ConnectedState) + if (_socket == nullptr || _socket->state() != QAbstractSocket::ConnectedState) return; message.writeMessage(_socket); if (!message.writeComplete()) { @@ -89,7 +89,7 @@ void ServerConnection::disconnectFromServer() emit disconnected(this); _timerDelete = startTimer(500); qDebug("Closing connection to server"); - if (_socket != NULL) { + if (_socket != nullptr) { _socket->blockSignals(true); _socket->abort(); } @@ -144,11 +144,11 @@ void ServerConnection::handleMsg() } emit stateChange(ConnectWindow::LoggingIn); const char *user = getpwuid(getuid())->pw_name; - if (user == NULL || *user == '\0') + if (user == nullptr || *user == '\0') user = getenv("USER"); - if (user == NULL || *user == '\0') + if (user == nullptr || *user == '\0') user = getenv("USERNAME"); - if (user == NULL || *user == '\0') + if (user == nullptr || *user == '\0') user = "Hans Affe"; _toServer.reset(); _toServer.setField(_ID, _LOGIN); @@ -269,7 +269,7 @@ void ServerConnection::handleMsg() void ServerConnection::checkLocalConnection() { - if (_socket == NULL) { + if (_socket == nullptr) { return; } if (_socket->peerAddress() == _socket->localAddress()) { @@ -297,10 +297,10 @@ void ServerConnection::timerEvent(QTimerEvent *event) _timerId = 0; this->disconnectFromServer(); } else if (event->timerId() == _timerDelete) { - if (_socket == NULL || _socket->state() == QAbstractSocket::UnconnectedState) { - if (_socket != NULL) + if (_socket == nullptr || _socket->state() == QAbstractSocket::UnconnectedState) { + if (_socket != nullptr) _socket->deleteLater(); - _socket = NULL; + _socket = nullptr; killTimer(_timerDelete); this->deleteLater(); return; @@ -375,7 +375,7 @@ void ServerConnection::sslErrors(const QList & errors) void ServerConnection::sock_dataArrival() { - if (_socket == NULL || _socket->state() != QAbstractSocket::ConnectedState) { + if (_socket == nullptr || _socket->state() != QAbstractSocket::ConnectedState) { qDebug("dataArrival called in bad state"); return; } @@ -390,7 +390,7 @@ void ServerConnection::sock_dataArrival() return; if (_fromServer.readComplete()) { // message is complete this->handleMsg(); - if (_socket == NULL) + if (_socket == nullptr) return; _fromServer.reset(); } diff --git a/src/client/net/serverconnection.h b/src/client/net/serverconnection.h index 9db9b02..f5f6264 100644 --- a/src/client/net/serverconnection.h +++ b/src/client/net/serverconnection.h @@ -38,7 +38,7 @@ public: ~ServerConnection(); inline bool isConnected() const { - return _socket != NULL && _socket->state() == QAbstractSocket::ConnectedState; + return _socket != nullptr && _socket->state() == QAbstractSocket::ConnectedState; } const inline QString getPeerAdress() const diff --git a/src/client/net/serverdiscovery.cpp b/src/client/net/serverdiscovery.cpp index 33ddbf5..799fcbe 100644 --- a/src/client/net/serverdiscovery.cpp +++ b/src/client/net/serverdiscovery.cpp @@ -145,12 +145,12 @@ void ServerDiscovery::onUdpReadyRead() while (_discoverySocket.hasPendingDatagrams()) { // Discard any packets if discovery is stopped if (!this->isActive()) { - _discoverySocket.readDatagram(NULL, 0); + _discoverySocket.readDatagram(nullptr, 0); continue; } const qint64 size = _discoverySocket.readDatagram(data, UDPBUFSIZ, &addr, &port); - if (size <= 0) //|| clientApp->connection() != NULL) // TODO CHECK + if (size <= 0) //|| clientApp->connection() != nullptr) // TODO CHECK continue; _packet.reset(); diff --git a/src/client/toolbar/toolbar.cpp b/src/client/toolbar/toolbar.cpp index 955731a..a8e50e9 100644 --- a/src/client/toolbar/toolbar.cpp +++ b/src/client/toolbar/toolbar.cpp @@ -99,7 +99,7 @@ void Toolbar::init() setAttribute(Qt::WA_DeleteOnClose, false); /* Create the VNC Window */ - _vnc = new VncWindow(NULL); + _vnc = new VncWindow(nullptr); /* Create the connect window */ clientApp->connectWindow()->setAvailableRooms(myRooms()); @@ -353,7 +353,7 @@ void Toolbar::onVncServerIsRunning(int port) */ void Toolbar::onDisconnected(ServerConnection* connection) { - if (connection != NULL) { + if (connection != nullptr) { disconnect(connection, SIGNAL(disconnected(ServerConnection*)), this, SLOT(onDisconnected(ServerConnection*))); } _ui->lblStatus->setStyleSheet("color:red"); @@ -398,7 +398,7 @@ void Toolbar::onConnected(ServerConnection* connection) */ void Toolbar::onDoDisconnect() { - if (clientApp->connection() != NULL) + if (clientApp->connection() != nullptr) clientApp->connection()->disconnectFromServer(); } @@ -486,12 +486,12 @@ void Toolbar::showInformationDialog() void Toolbar::onBtnAttention() { - const bool on = clientApp->connection() != NULL && _ui->btnAttention->isChecked(); + const bool on = clientApp->connection() != nullptr && _ui->btnAttention->isChecked(); if (on != _ui->btnAttention->isChecked()) { _ui->btnAttention->setChecked(on); return; } - if (clientApp->connection() != NULL) { + if (clientApp->connection() != nullptr) { clientApp->connection()->sendAttention(on); } } diff --git a/src/client/util/platform/blankscreen.cpp b/src/client/util/platform/blankscreen.cpp index ee644ca..bf34614 100644 --- a/src/client/util/platform/blankscreen.cpp +++ b/src/client/util/platform/blankscreen.cpp @@ -16,11 +16,11 @@ struct BlankScreen_Sysdep { Display *dpy; }; -BlankScreen::BlankScreen() : QDialog(NULL) +BlankScreen::BlankScreen() : QDialog(nullptr) { _sysdep = new BlankScreen_Sysdep; - _sysdep->dpy = XOpenDisplay(NULL); - if (_sysdep->dpy == NULL) + _sysdep->dpy = XOpenDisplay(nullptr); + if (_sysdep->dpy == nullptr) return; setWindowFlags(Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint | Qt::FramelessWindowHint); @@ -35,7 +35,7 @@ BlankScreen::BlankScreen() : QDialog(NULL) BlankScreen::~BlankScreen() { unlock(); - if (_sysdep->dpy != NULL) { + if (_sysdep->dpy != nullptr) { XCloseDisplay(_sysdep->dpy); } delete _sysdep; @@ -45,7 +45,7 @@ bool BlankScreen::lock(const QString& message) { if (_locked) return true; - if (_sysdep->dpy == NULL) + if (_sysdep->dpy == nullptr) return false; _message = message; @@ -73,7 +73,7 @@ bool BlankScreen::unlock() this->hide(); if (!_locked) return true; - if (_sysdep->dpy == NULL) + if (_sysdep->dpy == nullptr) return false; // ungrabbing of keyboard and mouse diff --git a/src/client/vnc/vncserver.cpp b/src/client/vnc/vncserver.cpp index 165ba6d..a016f08 100644 --- a/src/client/vnc/vncserver.cpp +++ b/src/client/vnc/vncserver.cpp @@ -12,7 +12,7 @@ #include "vncserver.h" #include "../util/util.h" -VncServer* VncServer::me = NULL; +VncServer* VncServer::me = nullptr; /***************************************************************************//** * @brief VncServer::instance @@ -20,7 +20,7 @@ VncServer* VncServer::me = NULL; */ VncServer* VncServer::instance() { - if (me == NULL) + if (me == nullptr) me = new VncServer(); return me; } @@ -32,10 +32,10 @@ VncServer* VncServer::instance() */ static QString makePassword(int len = 10) { - char pass[len]; + QString ret(len, Qt::Uninitialized); for (int i = 0; i < len; ++i) - pass[i] = (char)(43 + qrand() % 80); - return QString::fromUtf8(pass, len); + ret[i] = QChar(43 + qrand() % 80); + return ret; } /***************************************************************************//** @@ -48,7 +48,7 @@ struct Sleeper : public QThread { /***************************************************************************//** * @brief VncServer::VncServer */ -VncServer::VncServer() : _process(NULL), _port(0), _timerId(0) {} +VncServer::VncServer() : _process(nullptr), _port(0), _timerId(0) {} /***************************************************************************//** * @brief VncServer::~VncServer @@ -73,7 +73,7 @@ QSharedPointer VncServer::createPwFile(const QDir& dir) void VncServer::start() { // Keep things clean - if (_process != NULL) { + if (_process != nullptr) { disconnect(_process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(onError(QProcess::ProcessError))); disconnect(_process, SIGNAL(finished(int)), this, SLOT(onFinished(int))); } @@ -138,13 +138,13 @@ void VncServer::stop() killTimer(_timerId); _timerId = 0; } - if (_process == NULL) + if (_process == nullptr) return; qDebug("Stopping old VNC server."); disconnect(_process, SIGNAL(readyReadStandardOutput()), this, SLOT(onStdOut())); disconnect(_process, SIGNAL(readyReadStandardError()), this, SLOT(onStdErr())); QProcess *process = _process; - _process = NULL; + _process = nullptr; _port = 0; process->terminate(); for (int i = 0; i < 10 && process->state() != QProcess::NotRunning; ++i) @@ -185,7 +185,7 @@ void VncServer::timerEvent(QTimerEvent * /* event */ ) */ void VncServer::onStdOut() { - if (_process == NULL) { + if (_process == nullptr) { qDebug("VncServer::onStdOut() called in bad state."); return; } @@ -211,7 +211,7 @@ void VncServer::onStdOut() */ void VncServer::onStdErr() { - if (_process == NULL) { + if (_process == nullptr) { qDebug("VncServer::onStdErr() called in bad state."); return; } diff --git a/src/client/vnc/vncthread.cpp b/src/client/vnc/vncthread.cpp index 1dbe005..e399e9b 100644 --- a/src/client/vnc/vncthread.cpp +++ b/src/client/vnc/vncthread.cpp @@ -36,7 +36,7 @@ VncThread::VncThread(QString host, int port, QString passwd, int quality) : _port = port; _passwd = passwd; _quality = quality; - _client = NULL; + _client = nullptr; _connected = false; moveToThread(this); } @@ -45,7 +45,7 @@ VncThread::~VncThread() { qDebug("VNC worker destructor called."); Q_ASSERT(_run == false); - if (_client != NULL) { + if (_client != nullptr) { if (_client->sock != -1) ::close(_client->sock); _client->sock = -1; @@ -82,27 +82,27 @@ void VncThread::run() _client->canHandleNewFBSize = true; free(_client->serverHost); // in rfbGetClient, serverHost is assigned strdup(""), so free that first. _client->serverHost = strdup(_host.toUtf8().constData()); - _client->desktopName = NULL; + _client->desktopName = nullptr; _client->serverPort = _port; _client->GetPassword = &passwdHandler; _client->GotFrameBufferUpdate = &updateImage; - _client->frameBuffer = NULL; + _client->frameBuffer = nullptr; // save this instance in vnc-struct for callbacks rfbClientSetClientData(_client, 0, this); // start client - if (rfbInitClient(_client, NULL, NULL)) { + if (rfbInitClient(_client, nullptr, nullptr)) { break; // Success! } // Connection failed - _client = NULL; // InitClient frees the client on failure, so make sure we don't keep an invalid pointer around + _client = nullptr; // InitClient frees the client on failure, so make sure we don't keep an invalid pointer around if (!_run) break; // error, let's try again this->msleep(10 + qrand() % 50); } - if (_client != NULL) { + if (_client != nullptr) { qDebug("[%s] Connection successful!", metaObject()->className()); int one = 1; setsockopt(_client->sock, SOL_TCP, TCP_NODELAY, &one, sizeof(one)); @@ -136,7 +136,7 @@ void VncThread::run() */ const QString VncThread::getDesktopName() const { - if (_client == NULL || _client->desktopName == NULL) + if (_client == nullptr || _client->desktopName == nullptr) return QString(); return QString(_client->desktopName); } @@ -189,6 +189,11 @@ rfbBool VncThread::frameBufferHandler(rfbClient *client) const int width = client->width, height = client->height, depth = 32; const int size = width * height * (depth / 8); qDebug("[%s] Remote desktop: %ix%ix%i", t->metaObject()->className(), width, height, depth); + if (width < 0 || height < 0 || size < 0) { + qWarning() << "IGNORING INVALID FRAMEBUFFER SIZE"; + client->frameBuffer = nullptr; + return false; + } t->_img = QSharedPointer(new QImage(width, height, QImage::Format_RGB32)); if (size > t->_img->byteCount()) { @@ -196,7 +201,7 @@ rfbBool VncThread::frameBufferHandler(rfbClient *client) ::exit(1); } client->frameBuffer = t->_img->bits(); - memset(client->frameBuffer, '\0', size); + memset(client->frameBuffer, '\0', (size_t)size); client->format.trueColour = 1; client->format.bitsPerPixel = depth; client->format.redShift = 16; diff --git a/src/client/vnc/vncwindow.cpp b/src/client/vnc/vncwindow.cpp index 79352ae..a9d85f4 100644 --- a/src/client/vnc/vncwindow.cpp +++ b/src/client/vnc/vncwindow.cpp @@ -37,7 +37,7 @@ static int gcd(int a, int b) VncWindow::VncWindow(QWidget *parent) : QWidget(parent), _srcStepX(1), _srcStepY(1), _dstStepX(1), _dstStepY(1), - _vncWorker(NULL), _viewOnly(true), _multiScreen(false), _clientId(0), _redrawTimer(0), _tcpTimeoutTimer(0) + _vncWorker(nullptr), _viewOnly(true), _multiScreen(false), _clientId(0), _redrawTimer(0), _tcpTimeoutTimer(0) { QTimer *upper = new QTimer(this); connect(upper, SIGNAL(timeout()), this, SLOT(timer_moveToTop())); @@ -55,13 +55,13 @@ VncWindow::~VncWindow() /** * Terminates the vnc worker thread and stops all related timers. * The thread will be signalled to stop, but we don't wait for it - * to actually terminate. All signals of the thread are blocked, and we NULL + * to actually terminate. All signals of the thread are blocked, and we nullptr * our reference to it. It will finish running in a detached state and finally * delete itself upon completion. */ void VncWindow::terminateVncThread() { - if (_vncWorker == NULL) + if (_vncWorker == nullptr) return; disconnect(_vncWorker, SIGNAL(projectionStopped()), this, SLOT(onProjectionStopped())); @@ -69,7 +69,7 @@ void VncWindow::terminateVncThread() disconnect(_vncWorker, SIGNAL(imageUpdated(const int, const int, const int, const int)), this, SLOT(onUpdateImage(const int, const int, const int, const int))); _vncWorker->stop(); - _vncWorker = NULL; + _vncWorker = nullptr; if (_redrawTimer != 0) { killTimer(_redrawTimer); _redrawTimer = 0; @@ -96,7 +96,7 @@ void VncWindow::deleteVncThread() */ void VncWindow::draw(const int x, const int y, const int w, const int h) { - if (_vncWorker == NULL) + if (_vncWorker == nullptr) return; QSharedPointer buffer = _vncWorker->getFrameBuffer(); if (buffer.isNull()) @@ -147,10 +147,10 @@ void VncWindow::draw(const int x, const int y, const int w, const int h) bool VncWindow::calcScaling(const QImage *remote) { if (this->size() == _desiredSize && - (remote == NULL || remote->size() == _remoteSize)) + (remote == nullptr || remote->size() == _remoteSize)) return false; const QSize mySize = this->size(); - const QSize remoteSize = remote == NULL ? _remoteSize : remote->size(); + const QSize remoteSize = remote == nullptr ? _remoteSize : remote->size(); if (mySize.isEmpty()) return false; if (remoteSize.isEmpty()) @@ -270,7 +270,7 @@ void VncWindow::closeEvent(QCloseEvent * /* e */ ) */ void VncWindow::onUpdateImage(const int x, const int y, const int w, const int h) { - if (_vncWorker == NULL) + if (_vncWorker == nullptr) return; if (!this->calcScaling(_vncWorker->getFrameBuffer().data()) && !_remoteSize.isEmpty()) { if (_srcStepX > 1 || _srcStepY > 1) { @@ -345,7 +345,7 @@ void VncWindow::timerEvent(QTimerEvent *event) } else if (event->timerId() == _tcpTimeoutTimer) { killTimer(_tcpTimeoutTimer); _tcpTimeoutTimer = 0; - if (_vncWorker != NULL && !_vncWorker->isConnected()) { + if (_vncWorker != nullptr && !_vncWorker->isConnected()) { this->close(); } } else @@ -362,7 +362,7 @@ void VncWindow::timerEvent(QTimerEvent *event) */ void VncWindow::paintEvent(QPaintEvent *event) { - if (_vncWorker == NULL || !_vncWorker->isConnected()) { + if (_vncWorker == nullptr || !_vncWorker->isConnected()) { QPainter painter(this); if (!_remoteThumb.isNull() && _remoteThumb.height() > 0) { painter.drawPixmap(0, 0, this->width(), this->height(), _remoteThumb); -- cgit v1.2.3-55-g7522