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/vnc/vncserver.cpp | 22 +++++++++++----------- src/client/vnc/vncthread.cpp | 23 ++++++++++++++--------- src/client/vnc/vncwindow.cpp | 20 ++++++++++---------- 3 files changed, 35 insertions(+), 30 deletions(-) (limited to 'src/client/vnc') 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