Skip to content

Commit

Permalink
REFAC: Fix tons of warnings and non-portable code
Browse files Browse the repository at this point in the history
In various places we relied on compiler extensions such as
variable-length arrays on the stack or using non-standard escape
sequences. In order to make our code as portable as possible, these
parts of the code have been refactored to only use standard C++.

Furthermore, the new warning settings triggered a bunch of new warnings
all over the place that have been addressed by this commit.
  • Loading branch information
Krzmbrzl committed Jan 7, 2024
1 parent ded91ed commit b5a67c0
Show file tree
Hide file tree
Showing 167 changed files with 1,522 additions and 1,406 deletions.
10 changes: 5 additions & 5 deletions src/Channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
# include "Database.h"
# include "ServerHandler.h"

QHash< int, Channel * > Channel::c_qhChannels;
QHash< unsigned int, Channel * > Channel::c_qhChannels;
QReadWriteLock Channel::c_qrwlChannels;
#endif

Channel::Channel(int id, const QString &name, QObject *p) : QObject(p) {
Channel::Channel(unsigned int id, const QString &name, QObject *p) : QObject(p) {
iId = id;
iPosition = 0;
qsName = name;
Expand Down Expand Up @@ -59,12 +59,12 @@ Channel::~Channel() {
}

#ifdef MUMBLE
Channel *Channel::get(int id) {
Channel *Channel::get(unsigned int id) {
QReadLocker lock(&c_qrwlChannels);
return c_qhChannels.value(id);
}

Channel *Channel::add(int id, const QString &name) {
Channel *Channel::add(unsigned int id, const QString &name) {
QWriteLocker lock(&c_qrwlChannels);

if (c_qhChannels.contains(id))
Expand Down Expand Up @@ -272,7 +272,7 @@ void Channel::removeUser(User *p) {

Channel::operator QString() const {
return QString::fromLatin1("%1[%2:%3%4]")
.arg(qsName, QString::number(iId), QString::number(cParent ? cParent->iId : -1),
.arg(qsName, QString::number(iId), QString::number(cParent ? static_cast< int >(cParent->iId) : -1),
bTemporary ? QLatin1String("*") : QLatin1String(""));
}

Expand Down
10 changes: 5 additions & 5 deletions src/Channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Channel : public QObject {
public:
static constexpr int ROOT_ID = 0;

int iId;
unsigned int iId;
int iPosition;
bool bTemporary;
Channel *cParent;
Expand Down Expand Up @@ -67,7 +67,7 @@ class Channel : public QObject {
/// setting.
unsigned int uiMaxUsers;

Channel(int id, const QString &name, QObject *p = nullptr);
Channel(unsigned int id, const QString &name, QObject *p = nullptr);
~Channel();

#ifdef MUMBLE
Expand All @@ -79,11 +79,11 @@ class Channel : public QObject {
void clearFilterMode();
bool isFiltered() const;

static QHash< int, Channel * > c_qhChannels;
static QHash< unsigned int, Channel * > c_qhChannels;
static QReadWriteLock c_qrwlChannels;

static Channel *get(int);
static Channel *add(int, const QString &);
static Channel *get(unsigned int);
static Channel *add(unsigned int, const QString &);
static void remove(Channel *);

void addClientUser(ClientUser *p);
Expand Down
26 changes: 13 additions & 13 deletions src/ChannelListenerManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

std::size_t qHash(const ChannelListener &listener) {
return std::hash< ChannelListener >()(listener);
};
}

bool operator==(const ChannelListener &lhs, const ChannelListener &rhs) {
return lhs.channelID == rhs.channelID && lhs.userSession == rhs.userSession;
Expand All @@ -23,21 +23,21 @@ ChannelListenerManager::ChannelListenerManager()
m_listenerVolumeAdjustments() {
}

void ChannelListenerManager::addListener(unsigned int userSession, int channelID) {
void ChannelListenerManager::addListener(unsigned int userSession, unsigned int channelID) {
QWriteLocker lock(&m_listenerLock);

m_listeningUsers[userSession] << channelID;
m_listenedChannels[channelID] << userSession;
}

void ChannelListenerManager::removeListener(unsigned int userSession, int channelID) {
void ChannelListenerManager::removeListener(unsigned int userSession, unsigned int channelID) {
QWriteLocker lock(&m_listenerLock);

m_listeningUsers[userSession].remove(channelID);
m_listenedChannels[channelID].remove(userSession);
}

bool ChannelListenerManager::isListening(unsigned int userSession, int channelID) const {
bool ChannelListenerManager::isListening(unsigned int userSession, unsigned int channelID) const {
QReadLocker lock(&m_listenerLock);

return m_listenedChannels[channelID].contains(userSession);
Expand All @@ -49,25 +49,25 @@ bool ChannelListenerManager::isListeningToAny(unsigned int userSession) const {
return !m_listeningUsers[userSession].isEmpty();
}

bool ChannelListenerManager::isListenedByAny(int channelID) const {
bool ChannelListenerManager::isListenedByAny(unsigned int channelID) const {
QReadLocker lock(&m_listenerLock);

return !m_listenedChannels[channelID].isEmpty();
}

const QSet< unsigned int > ChannelListenerManager::getListenersForChannel(int channelID) const {
const QSet< unsigned int > ChannelListenerManager::getListenersForChannel(unsigned int channelID) const {
QReadLocker lock(&m_listenerLock);

return m_listenedChannels[channelID];
}

const QSet< int > ChannelListenerManager::getListenedChannelsForUser(unsigned int userSession) const {
const QSet< unsigned int > ChannelListenerManager::getListenedChannelsForUser(unsigned int userSession) const {
QReadLocker lock(&m_listenerLock);

return m_listeningUsers[userSession];
}

int ChannelListenerManager::getListenerCountForChannel(int channelID) const {
int ChannelListenerManager::getListenerCountForChannel(unsigned int channelID) const {
QReadLocker lock(&m_listenerLock);

return m_listenedChannels[channelID].size();
Expand All @@ -79,7 +79,7 @@ int ChannelListenerManager::getListenedChannelCountForUser(unsigned int userSess
return m_listeningUsers[userSession].size();
}

void ChannelListenerManager::setListenerVolumeAdjustment(unsigned int userSession, int channelID,
void ChannelListenerManager::setListenerVolumeAdjustment(unsigned int userSession, unsigned int channelID,
const VolumeAdjustment &volumeAdjustment) {
float oldValue = 1.0f;
{
Expand All @@ -103,7 +103,7 @@ void ChannelListenerManager::setListenerVolumeAdjustment(unsigned int userSessio
}

const VolumeAdjustment &ChannelListenerManager::getListenerVolumeAdjustment(unsigned int userSession,
int channelID) const {
unsigned int channelID) const {
static VolumeAdjustment fallbackObj = VolumeAdjustment::fromFactor(1.0f);

QReadLocker lock(&m_volumeLock);
Expand All @@ -121,14 +121,14 @@ const VolumeAdjustment &ChannelListenerManager::getListenerVolumeAdjustment(unsi
}
}

std::unordered_map< int, VolumeAdjustment >
std::unordered_map< unsigned int, VolumeAdjustment >
ChannelListenerManager::getAllListenerVolumeAdjustments(unsigned int userSession) const {
QReadLocker lock1(&m_volumeLock);
QReadLocker lock2(&m_listenerLock);

std::unordered_map< int, VolumeAdjustment > adjustments;
std::unordered_map< unsigned int, VolumeAdjustment > adjustments;

for (int channelID : m_listeningUsers.value(userSession)) {
for (unsigned int channelID : m_listeningUsers.value(userSession)) {
ChannelListener listener = {};
listener.channelID = channelID;
listener.userSession = userSession;
Expand Down
32 changes: 17 additions & 15 deletions src/ChannelListenerManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ struct ChannelListener {
/// The session ID of the owning user
unsigned int userSession;
/// The ID of the channel this listener is placed in
int channelID;
unsigned int channelID;
};

// Make ChannelListener hashable and comparable
template<> struct std::hash< ChannelListener > {
std::size_t operator()(const ChannelListener &val) const {
return std::hash< unsigned int >()(val.userSession) ^ (std::hash< int >()(val.channelID) << 2);
return std::hash< unsigned int >()(val.userSession) ^ (std::hash< unsigned int >()(val.channelID) << 2);
}
};
std::size_t qHash(const ChannelListener &listener);
Expand All @@ -47,9 +47,9 @@ class ChannelListenerManager : public QObject {
/// A lock for guarding m_listeningUsers as well as m_listenedChannels
mutable QReadWriteLock m_listenerLock;
/// A map between a user's session and a list of IDs of all channels the user is listening to
QHash< unsigned int, QSet< int > > m_listeningUsers;
QHash< unsigned int, QSet< unsigned int > > m_listeningUsers;
/// A map between a channel's ID and a list of all user-sessions of users listening to that channel
QHash< int, QSet< unsigned int > > m_listenedChannels;
QHash< unsigned int, QSet< unsigned int > > m_listenedChannels;
/// A lock for guarding m_listenerVolumeAdjustments
mutable QReadWriteLock m_volumeLock;
/// A map between channel IDs and local volume adjustments to be made for ChannelListeners
Expand All @@ -64,38 +64,38 @@ class ChannelListenerManager : public QObject {
///
/// @param userSession The session ID of the user
/// @param channelID The ID of the channel
void addListener(unsigned int userSession, int channelID);
void addListener(unsigned int userSession, unsigned int channelID);

/// Removes a listener from the channel.
///
/// @param userSession The session ID of the user
/// @param channelID The ID of the channel
void removeListener(unsigned int userSession, int channelID);
void removeListener(unsigned int userSession, unsigned int channelID);

/// @param userSession The session ID of the user
/// @param channelID The ID of the channel
/// @returns Whether the given user is listening to the given channel
bool isListening(unsigned int userSession, int channelID) const;
bool isListening(unsigned int userSession, unsigned int channelID) const;

/// @param userSession The session ID of the user
/// @returns Whether this user is listening to any channel via the ChannelListener feature
bool isListeningToAny(unsigned int userSession) const;

/// @param channelID The ID of the channel
/// @returns Whether any user listens to this channel via the ChannelListener feature
bool isListenedByAny(int channelID) const;
bool isListenedByAny(unsigned int channelID) const;

/// @param channelID The ID of the channel
/// @returns A set of user sessions of users listening to the given channel
const QSet< unsigned int > getListenersForChannel(int channelID) const;
const QSet< unsigned int > getListenersForChannel(unsigned int channelID) const;

/// @param userSession The session ID of the user
/// @returns A set of channel IDs of channels the given user is listening to
const QSet< int > getListenedChannelsForUser(unsigned int userSession) const;
const QSet< unsigned int > getListenedChannelsForUser(unsigned int userSession) const;

/// @param channelID The ID of the channel
/// @returns The amount of users that are listening to the given channel
int getListenerCountForChannel(int channelID) const;
int getListenerCountForChannel(unsigned int channelID) const;

/// @param userSession The session ID of the user
/// @returns The amount of channels the given user is listening to
Expand All @@ -106,21 +106,23 @@ class ChannelListenerManager : public QObject {
/// @param userSession The session ID of the user
/// @param channelID The ID of the channel
/// @param volumeAdjustment The volume adjustment to apply
void setListenerVolumeAdjustment(unsigned int userSession, int channelID, const VolumeAdjustment &volumeAdjustment);
void setListenerVolumeAdjustment(unsigned int userSession, unsigned int channelID,
const VolumeAdjustment &volumeAdjustment);

/// @param userSession The session ID of the user
/// @param channelID The ID of the channel
/// @returns The volume adjustment for the listener of the given user in the given channel.
const VolumeAdjustment &getListenerVolumeAdjustment(unsigned int userSession, int channelID) const;
const VolumeAdjustment &getListenerVolumeAdjustment(unsigned int userSession, unsigned int channelID) const;

/// @param userSession The session ID of the user whose listener's volume adjustments to obtain
/// @returns A map between channel IDs and the currently set volume adjustment
std::unordered_map< int, VolumeAdjustment > getAllListenerVolumeAdjustments(unsigned int userSession) const;
std::unordered_map< unsigned int, VolumeAdjustment >
getAllListenerVolumeAdjustments(unsigned int userSession) const;

/// Clears all ChannelListeners and volume adjustments
void clear();
signals:
void localVolumeAdjustmentsChanged(int channelID, float newAdjustment, float oldAdjustment);
void localVolumeAdjustmentsChanged(unsigned int channelID, float newAdjustment, float oldAdjustment);
};

#endif // MUMBLE_CHANNELLISTENERMANAGER_H_
12 changes: 6 additions & 6 deletions src/Connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ void Connection::socketRead() {

qtsSocket->read(reinterpret_cast< char * >(a_ucBuffer), 6);
m_type = static_cast< Mumble::Protocol::TCPMessageType >(qFromBigEndian< quint16 >(&a_ucBuffer[0]));
iPacketLength = qFromBigEndian< quint32 >(&a_ucBuffer[2]);
iPacketLength = qFromBigEndian< int >(&a_ucBuffer[2]);
iAvailable -= 6;
}

Expand Down Expand Up @@ -161,19 +161,19 @@ void Connection::socketDisconnected() {
void Connection::messageToNetwork(const ::google::protobuf::Message &msg, Mumble::Protocol::TCPMessageType msgType,
QByteArray &cache) {
#if GOOGLE_PROTOBUF_VERSION >= 3004000
int len = msg.ByteSizeLong();
std::size_t len = msg.ByteSizeLong();
#else
// ByteSize() has been deprecated as of protobuf v3.4
int len = msg.ByteSize();
std::size_t len = msg.ByteSize();
#endif
if (len > 0x7fffff)
return;
cache.resize(len + 6);
cache.resize(static_cast< int >(len + 6));
unsigned char *uc = reinterpret_cast< unsigned char * >(cache.data());
qToBigEndian< quint16 >(static_cast< quint16 >(msgType), &uc[0]);
qToBigEndian< quint32 >(len, &uc[2]);
qToBigEndian< quint32 >(static_cast< unsigned int >(len), &uc[2]);

msg.SerializeToArray(uc + 6, len);
msg.SerializeToArray(uc + 6, static_cast< int >(len));
}

void Connection::sendMessage(const ::google::protobuf::Message &msg, Mumble::Protocol::TCPMessageType msgType,
Expand Down
2 changes: 1 addition & 1 deletion src/EnvUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ bool waylandIsUsed() {
return getenv(QStringLiteral("WAYLAND_DISPLAY")) != "";
}

}; // namespace EnvUtils
} // namespace EnvUtils
2 changes: 1 addition & 1 deletion src/EnvUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ bool setenv(QString name, QString value);

bool waylandIsUsed();

}; // namespace EnvUtils
} // namespace EnvUtils

#endif
5 changes: 3 additions & 2 deletions src/LogEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

#include "LogEmitter.h"

LogEmitter::LogEmitter(QObject *p) : QObject(p){};
LogEmitter::LogEmitter(QObject *p) : QObject(p) {
}

void LogEmitter::addLogEntry(const QString &msg) {
emit newLogEntry(msg);
};
}
6 changes: 3 additions & 3 deletions src/MumbleConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ namespace Plugins {
constexpr int MAX_DATA_LENGTH = 1000;
constexpr int MAX_DATA_ID_LENGTH = 100;

}; // namespace PluginMessage
}; // namespace Plugins
}; // namespace Mumble
} // namespace PluginMessage
} // namespace Plugins
} // namespace Mumble

#endif // MUMBLE_MUMBLECONSTANTS_H_
Loading

0 comments on commit b5a67c0

Please sign in to comment.