mirror of
https://github.com/f4exb/sdrangel.git
synced 2026-08-05 18:36:38 -04:00
deviceapi: Avoid invalid iterator erase in removeBuddy
In std::vector<T,Allocator>::erase, The iterator pos must be valid and dereferenceable. Thus the end() iterator (which is valid, but is not dereferenceable) cannot be used as a value for pos. Protect against that by checking the result of std::find() before erasing a buddy entry. The buddy should always be present when removeBuddy() is called, but avoid undefined behavior if that assumption is violated. Signed-off-by: Robin Getz <rgetz503@gmail.com>
This commit is contained in:
@@ -774,13 +774,25 @@ void DeviceAPI::removeBuddy(DeviceAPI* buddy)
|
||||
{
|
||||
switch(buddy->m_streamType) {
|
||||
case StreamSingleRx:
|
||||
m_sourceBuddies.erase(std::find(m_sourceBuddies.begin(), m_sourceBuddies.end(), buddy));
|
||||
{
|
||||
auto it = std::find(m_sourceBuddies.begin(), m_sourceBuddies.end(), buddy);
|
||||
if (it != m_sourceBuddies.end())
|
||||
{
|
||||
m_sourceBuddies.erase(it);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case StreamSingleTx:
|
||||
m_sinkBuddies.erase(std::find(m_sinkBuddies.begin(), m_sinkBuddies.end(), buddy));
|
||||
{
|
||||
auto it = std::find(m_sinkBuddies.begin(), m_sinkBuddies.end(), buddy);
|
||||
if (it != m_sinkBuddies.end())
|
||||
{
|
||||
m_sinkBuddies.erase(it);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
qDebug("DeviceAPI::removeSourceBuddy: buddy %s(%s) is not of single Rx or Tx type",
|
||||
qDebug("DeviceAPI::removeBuddy: buddy %s(%s) is not of single Rx or Tx type",
|
||||
qPrintable(buddy->getHardwareId()),
|
||||
qPrintable(buddy->getSamplingDeviceSerial()));
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user