From 532e05cb0f135d763890f4146e14da036cc3b248 Mon Sep 17 00:00:00 2001 From: noavarice Date: Wed, 12 Apr 2017 20:38:14 +0300 Subject: [PATCH] fix: fixed segfault after accepting group invite Fix #4327 Description: - QString::arg call with mulpiple arguments replaced with chained QString::arg calls; - dynamic memory allocation for array of names replaced with allocation on the stack, unfortunately I do not notice this while making Core refactoring. Also code style is formatted with script --- src/core/core.cpp | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/src/core/core.cpp b/src/core/core.cpp index 194455677..ca51b2705 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -535,7 +535,7 @@ void Core::onGroupNamelistChange(Tox*, uint32_t groupId, uint32_t peerId, CoreAV::invalidateGroupCallPeerSource(groupId, peerId); } - qDebug() << QString("Group namelist change %1:%2 %3").arg(groupId, peerId, change); + qDebug() << QString("Group namelist change %1:%2 %3").arg(groupId).arg(peerId).arg(change); emit static_cast(core)->groupNamelistChanged(groupId, peerId, change); } @@ -1020,17 +1020,17 @@ QByteArray Core::getToxSaveData() } // Declared to avoid code duplication -#define GET_FRIEND_PROPERTY(property, function, checkSize)\ - const size_t property##Size = function##_size(tox, ids[i], nullptr);\ - if ((!checkSize || property##Size) && property##Size != SIZE_MAX) {\ - uint8_t* prop = new uint8_t[property##Size];\ - if (function(tox, ids[i], prop, nullptr)) {\ - QString propStr = ToxString(prop, property##Size).getQString();\ - emit friend##property##Changed(ids[i], propStr);\ - }\ - \ - delete[] prop;\ - }\ +#define GET_FRIEND_PROPERTY(property, function, checkSize) \ + const size_t property##Size = function##_size(tox, ids[i], nullptr); \ + if ((!checkSize || property##Size) && property##Size != SIZE_MAX) { \ + uint8_t* prop = new uint8_t[property##Size]; \ + if (function(tox, ids[i], prop, nullptr)) { \ + QString propStr = ToxString(prop, property##Size).getQString(); \ + emit friend##property##Changed(ids[i], propStr); \ + } \ + \ + delete[] prop; \ + } void Core::loadFriends() { @@ -1171,10 +1171,7 @@ QStringList Core::getGroupPeerNames(int groupId) const return {}; } - std::vector namesArray(nPeers, new uint8_t[TOX_MAX_NAME_LENGTH]); - std::vector lengths(nPeers); TOX_ERR_CONFERENCE_PEER_QUERY error; - uint32_t count = tox_conference_peer_count(tox, groupId, &error); if (!parsePeerQueryError(error)) { return {}; @@ -1187,17 +1184,14 @@ QStringList Core::getGroupPeerNames(int groupId) const QStringList names; for (uint32_t i = 0; i < nPeers; ++i) { - lengths[i] = tox_conference_peer_get_name_size(tox, groupId, i, &error); - bool ok = tox_conference_peer_get_name(tox, groupId, i, namesArray[i], &error); - if (parsePeerQueryError(error) && ok) { - names.append(ToxString(namesArray[i], lengths[i]).getQString()); + uint8_t name[TOX_MAX_NAME_LENGTH] = {0}; + size_t length = tox_conference_peer_get_name_size(tox, groupId, i, &error); + bool ok = tox_conference_peer_get_name(tox, groupId, i, name, &error); + if (ok && parsePeerQueryError(error)) { + names.append(ToxString(name, length).getQString()); } } - for (uint8_t* name : namesArray) { - delete[] name; - } - return names; }