Skip to content

Commit

Permalink
Make sockets' accept() methods return qintptr
Browse files Browse the repository at this point in the history
Not necessarily relevant outside windows, where the socket descriptor
is SOCKET (= unsigned 64-bit). Also follow their recommendation to not
compare to -1, but rather to INVALID_SOCKET.

Pick-to: 6.4 6.3 6.2
Change-Id: I0cfa4dfd9e147469132e2e72de22b30eab01e15c
Reviewed-by: Qt CI Bot <[email protected]>
Reviewed-by: Thiago Macieira <[email protected]>
  • Loading branch information
Morten242 committed Aug 24, 2022
1 parent ad1980c commit da9d60e
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/network/socket/qabstractsocketengine_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class Q_AUTOTEST_EXPORT QAbstractSocketEngine : public QObject
virtual bool connectToHostByName(const QString &name, quint16 port) = 0;
virtual bool bind(const QHostAddress &address, quint16 port) = 0;
virtual bool listen(int backlog) = 0;
virtual int accept() = 0;
virtual qintptr accept() = 0;
virtual void close() = 0;

virtual qint64 bytesAvailable() const = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/network/socket/qhttpsocketengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ bool QHttpSocketEngine::listen(int backlog)
return false;
}

int QHttpSocketEngine::accept()
qintptr QHttpSocketEngine::accept()
{
qWarning("Operation is not supported");
setError(QAbstractSocket::UnsupportedSocketOperationError, "Unsupported socket operation"_L1);
Expand Down
2 changes: 1 addition & 1 deletion src/network/socket/qhttpsocketengine_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Q_AUTOTEST_EXPORT QHttpSocketEngine : public QAbstractSocketEngine
bool connectToHostByName(const QString &name, quint16 port) override;
bool bind(const QHostAddress &address, quint16 port) override;
bool listen(int backlog) override;
int accept() override;
qintptr accept() override;
void close() override;

qint64 bytesAvailable() const override;
Expand Down
6 changes: 3 additions & 3 deletions src/network/socket/qnativesocketengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ bool QNativeSocketEngine::listen(int backlog)
\sa bind(), listen()
*/
int QNativeSocketEngine::accept()
qintptr QNativeSocketEngine::accept()
{
Q_D(QNativeSocketEngine);
Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::accept(), -1);
Expand Down Expand Up @@ -1255,7 +1255,7 @@ bool QReadNotifier::event(QEvent *e)
class QWriteNotifier : public QSocketNotifier
{
public:
QWriteNotifier(int fd, QNativeSocketEngine *parent)
QWriteNotifier(qintptr fd, QNativeSocketEngine *parent)
: QSocketNotifier(fd, QSocketNotifier::Write, parent) { engine = parent; }

protected:
Expand All @@ -1279,7 +1279,7 @@ bool QWriteNotifier::event(QEvent *e)
class QExceptionNotifier : public QSocketNotifier
{
public:
QExceptionNotifier(int fd, QNativeSocketEngine *parent)
QExceptionNotifier(qintptr fd, QNativeSocketEngine *parent)
: QSocketNotifier(fd, QSocketNotifier::Exception, parent) { engine = parent; }

protected:
Expand Down
4 changes: 2 additions & 2 deletions src/network/socket/qnativesocketengine_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class Q_AUTOTEST_EXPORT QNativeSocketEngine : public QAbstractSocketEngine
bool connectToHostByName(const QString &name, quint16 port) override;
bool bind(const QHostAddress &address, quint16 port) override;
bool listen(int backlog) override;
int accept() override;
qintptr accept() override;
void close() override;

qint64 bytesAvailable() const override;
Expand Down Expand Up @@ -225,7 +225,7 @@ class QNativeSocketEnginePrivate : public QAbstractSocketEnginePrivate
bool nativeConnect(const QHostAddress &address, quint16 port);
bool nativeBind(const QHostAddress &address, quint16 port);
bool nativeListen(int backlog);
int nativeAccept();
qintptr nativeAccept();
#ifndef QT_NO_NETWORKINTERFACE
bool nativeJoinMulticastGroup(const QHostAddress &groupAddress,
const QNetworkInterface &iface);
Expand Down
4 changes: 2 additions & 2 deletions src/network/socket/qnativesocketengine_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ bool QNativeSocketEnginePrivate::nativeListen(int backlog)
return true;
}

int QNativeSocketEnginePrivate::nativeAccept()
qintptr QNativeSocketEnginePrivate::nativeAccept()
{
int acceptedDescriptor = qt_safe_accept(socketDescriptor, nullptr, nullptr);
if (acceptedDescriptor == -1) {
Expand Down Expand Up @@ -605,7 +605,7 @@ int QNativeSocketEnginePrivate::nativeAccept()
}
}

return acceptedDescriptor;
return qintptr(acceptedDescriptor);
}

#ifndef QT_NO_NETWORKINTERFACE
Expand Down
12 changes: 6 additions & 6 deletions src/network/socket/qnativesocketengine_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -773,10 +773,10 @@ bool QNativeSocketEnginePrivate::nativeListen(int backlog)
return true;
}

int QNativeSocketEnginePrivate::nativeAccept()
qintptr QNativeSocketEnginePrivate::nativeAccept()
{
int acceptedDescriptor = WSAAccept(socketDescriptor, 0,0,0,0);
if (acceptedDescriptor == -1) {
SOCKET acceptedDescriptor = WSAAccept(socketDescriptor, 0,0,0,0);
if (acceptedDescriptor == INVALID_SOCKET) {
int err = WSAGetLastError();
switch (err) {
case WSAEACCES:
Expand Down Expand Up @@ -810,7 +810,7 @@ int QNativeSocketEnginePrivate::nativeAccept()
setError(QAbstractSocket::UnknownSocketError, UnknownSocketErrorString);
break;
}
} else if (acceptedDescriptor != -1 && QAbstractEventDispatcher::instance()) {
} else if (acceptedDescriptor != INVALID_SOCKET && QAbstractEventDispatcher::instance()) {
// Because of WSAAsyncSelect() WSAAccept returns a non blocking socket
// with the same attributes as the listening socket including the current
// WSAAsyncSelect(). To be able to change the socket to blocking mode the
Expand All @@ -820,9 +820,9 @@ int QNativeSocketEnginePrivate::nativeAccept()
n.setEnabled(false);
}
#if defined (QNATIVESOCKETENGINE_DEBUG)
qDebug("QNativeSocketEnginePrivate::nativeAccept() == %i", acceptedDescriptor);
qDebug("QNativeSocketEnginePrivate::nativeAccept() == %lld", qint64(acceptedDescriptor));
#endif
return acceptedDescriptor;
return qintptr(acceptedDescriptor);
}

static bool multicastMembershipHelper(QNativeSocketEnginePrivate *d,
Expand Down
2 changes: 1 addition & 1 deletion src/network/socket/qsocks5socketengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1379,7 +1379,7 @@ bool QSocks5SocketEngine::listen(int backlog)
return false;
}

int QSocks5SocketEngine::accept()
qintptr QSocks5SocketEngine::accept()
{
Q_D(QSocks5SocketEngine);
// check we are listing ---
Expand Down
2 changes: 1 addition & 1 deletion src/network/socket/qsocks5socketengine_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Q_AUTOTEST_EXPORT QSocks5SocketEngine : public QAbstractSocketEngine
bool connectToHostByName(const QString &name, quint16 port) override;
bool bind(const QHostAddress &address, quint16 port) override;
bool listen(int backlog) override;
int accept() override;
qintptr accept() override;
void close() override;

qint64 bytesAvailable() const override;
Expand Down
2 changes: 1 addition & 1 deletion src/network/socket/qtcpserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ void QTcpServerPrivate::readNotification()
return;
}

int descriptor = socketEngine->accept();
qintptr descriptor = socketEngine->accept();
if (descriptor == -1) {
if (socketEngine->error() != QAbstractSocket::TemporaryError) {
q->pauseAccepting();
Expand Down

0 comments on commit da9d60e

Please sign in to comment.