Remove the struct NetlinkSocket

Started with fixing a "Class with destructor should also declare a
copy or move constructor and assignment operator." warning found by an
Axivion scan.

However, the NetlinkSocket struct is used only once and the behavior
can be replicated with a qScopeGuard.

Remove the struct NetlinkSocket and use a qScopeGuard for resource
management.

Drive-by: remove a Q_UNLIKELY that's not needed (qErrnoWarning() is
already marked as Q_DECL_COLD_FUNCTION).

Task-number: QTBUG-125026
Pick-to: 6.8 6.7 6.5
Change-Id: I6b770e284fdda3ff97b8cabac205e9b5abe88b14
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Mate Barany 2024-06-27 15:43:27 +02:00 committed by Marc Mutz
parent 5d73d64f3e
commit 954bf6e18e

View File

@ -9,6 +9,7 @@
#include <qendian.h>
#include <qobjectdefs.h>
#include <qscopeguard.h>
#include <qvarlengtharray.h>
// according to rtnetlink(7)
@ -85,28 +86,6 @@ static QNetworkInterface::InterfaceType probeIfType(int socket, struct ifreq *re
namespace {
struct NetlinkSocket
{
int sock;
Q_NODISCARD_CTOR explicit NetlinkSocket(int bufferSize)
{
sock = qt_safe_socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
if (Q_UNLIKELY(sock == -1))
qErrnoWarning("Could not create AF_NETLINK socket");
// set buffer length
socklen_t len = sizeof(bufferSize);
setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &bufferSize, len);
}
~NetlinkSocket()
{
if (sock != -1)
qt_safe_close(sock);
}
operator int() const { return sock; }
};
template <typename Lambda> struct ProcessNetlinkRequest
{
@ -406,9 +385,17 @@ QList<QNetworkInterfacePrivate *> QNetworkInterfaceManager::scan()
{
// open netlink socket
QList<QNetworkInterfacePrivate *> result;
NetlinkSocket sock(BufferSize);
if (Q_UNLIKELY(sock == -1))
int sock = qt_safe_socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
if (sock == -1) {
qErrnoWarning("Could not create AF_NETLINK socket");
return result;
}
const auto sg = qScopeGuard([&] { qt_safe_close(sock); });
// set buffer length
const int bufferSize = BufferSize;
setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize));
QByteArray buffer(BufferSize, Qt::Uninitialized);
char *buf = buffer.data();