1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-25 17:28:50 -05:00
sdrangel/sdrbase/device/deviceuserargs.cpp
Daniele Forsi dbf630c424 Fix memleaks found with AddressSanitizer/LeakSanitizer
Fixes:
Direct leak of 32 byte(s) in 1 object(s) allocated from:
    #0 0x7eff8bcf46c8 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:95
    #1 0x7eff897b484d in DeviceUserArgs::serialize() const sdrbase/device/deviceuserargs.cpp:43
    #2 0x7eff897f4904 in MainSettings::save() const sdrbase/settings/mainsettings.cpp:237
    #3 0x7eff8b578654 in MainWindow::closeEvent(QCloseEvent*) sdrgui/mainwindow.cpp:1749
    #4 0x7eff877a5dc7 in QWidget::event(QEvent*) (/lib/x86_64-linux-gnu/libQt5Widgets.so.5+0x1a5dc7) (BuildId: 983eca66d9695a1892aa796da4160d8d6f9b9ac4)

Direct leak of 32 byte(s) in 1 object(s) allocated from:
    #0 0x7eff8bcf46c8 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:95
    #1 0x7eff897b484d in DeviceUserArgs::serialize() const sdrbase/device/deviceuserargs.cpp:43
    #2 0x7eff897f4904 in MainSettings::save() const sdrbase/settings/mainsettings.cpp:237
    #3 0x7eff8b552c9a in MainWindow::~MainWindow() sdrgui/mainwindow.cpp:326
    #4 0x564e8b36e1e5 in runQtApplication app/main.cpp:212
    #5 0x564e8b36bab7 in main app/main.cpp:248
    #6 0x7eff862456c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
2024-05-19 19:44:45 +02:00

144 lines
4.6 KiB
C++

///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2012 maintech GmbH, Otto-Hahn-Str. 15, 97204 Hoechberg, Germany //
// written by Christian Daniel //
// Copyright (C) 2015-2019 Edouard Griffiths, F4EXB <f4exb06@gmail.com> //
// Copyright (C) 2022 Jon Beniston, M7RCE <jon@beniston.com> //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation as version 3 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License V3 for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#include <QDataStream>
#include <QIODevice>
#include "util/simpleserializer.h"
#include "deviceuserargs.h"
QDataStream &operator<<(QDataStream &ds, const DeviceUserArgs::Args &inObj)
{
ds << inObj.m_id << inObj.m_sequence << inObj.m_args << inObj.m_nonDiscoverable;
return ds;
}
QDataStream &operator>>(QDataStream &ds, DeviceUserArgs::Args &outObj)
{
ds >> outObj.m_id >> outObj.m_sequence >> outObj.m_args >> outObj.m_nonDiscoverable;
return ds;
}
QByteArray DeviceUserArgs::serialize() const
{
SimpleSerializer s(1);
QByteArray data;
QDataStream stream(&data, QIODevice::WriteOnly);
stream << m_argsByDevice;
s.writeBlob(1, data);
return s.final();
}
bool DeviceUserArgs::deserialize(const QByteArray& data)
{
SimpleDeserializer d(data);
if (!d.isValid()) {
return false;
}
if(d.getVersion() == 1)
{
QByteArray data;
d.readBlob(1, &data);
QDataStream readStream(&data, QIODevice::ReadOnly);
readStream >> m_argsByDevice;
return true;
}
else
{
return false;
}
}
QString DeviceUserArgs::findUserArgs(const QString& id, int sequence)
{
for (int i = 0; i < m_argsByDevice.size(); i++)
{
if ((m_argsByDevice.at(i).m_id == id) && (m_argsByDevice.at(i).m_sequence == sequence)) {
return m_argsByDevice.at(i).m_args;
}
}
return "";
}
void DeviceUserArgs::addDeviceArgs(const QString& id, int sequence, const QString& deviceArgs, bool nonDiscoverable)
{
int i = 0;
for (; i < m_argsByDevice.size(); i++)
{
if ((m_argsByDevice.at(i).m_id == id) && (m_argsByDevice.at(i).m_sequence == sequence)) {
break;
}
}
if (i == m_argsByDevice.size()) {
m_argsByDevice.push_back(Args(id, sequence, deviceArgs, nonDiscoverable));
}
}
void DeviceUserArgs::addOrUpdateDeviceArgs(const QString& id, int sequence, const QString& deviceArgs, bool nonDiscoverable)
{
int i = 0;
for (; i < m_argsByDevice.size(); i++)
{
if ((m_argsByDevice.at(i).m_id == id) && (m_argsByDevice.at(i).m_sequence == sequence)) {
m_argsByDevice[i].m_args = deviceArgs;
}
}
if (i == m_argsByDevice.size()) {
m_argsByDevice.push_back(Args(id, sequence, deviceArgs, nonDiscoverable));
}
}
void DeviceUserArgs::updateDeviceArgs(const QString& id, int sequence, const QString& deviceArgs, bool nonDiscoverable)
{
int i = 0;
for (; i < m_argsByDevice.size(); i++)
{
if ((m_argsByDevice.at(i).m_id == id) && (m_argsByDevice.at(i).m_sequence == sequence))
{
m_argsByDevice[i].m_args = deviceArgs;
m_argsByDevice[i].m_nonDiscoverable = nonDiscoverable;
}
}
}
void DeviceUserArgs::deleteDeviceArgs(const QString& id, int sequence)
{
int i = 0;
for (; i < m_argsByDevice.size(); i++)
{
if ((m_argsByDevice.at(i).m_id == id) && (m_argsByDevice.at(i).m_sequence == sequence))
{
m_argsByDevice.takeAt(i);
return;
}
}
}