Skip to content

Commit

Permalink
fix: add a wrapper for std::valarray to catch out of range access
Browse files Browse the repository at this point in the history
If we have a recording with the following layout: A B C where A: part
where function f is accessed
B: lost samples
C: part where function g is accessed
then f won't have the lost events cost while g does. This will cause a
crash when the model tries to access it.

This patch replaces the typedef ItemCost with a real wrapper that will
catch the out range access.

fixes: #629
  • Loading branch information
lievenhey authored and milianw committed May 23, 2024
1 parent 6957440 commit d8de369
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
82 changes: 81 additions & 1 deletion src/models/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,87 @@ struct FrameLocation
Data::Location location;
};

using ItemCost = std::valarray<qint64>;
class ItemCost
{
public:
ItemCost(std::size_t size = 0)
: m_cost(size)
{
}

ItemCost(qint64 value, std::size_t count)
: m_cost(value, count)
{
}

ItemCost(std::valarray<qint64> cost)
: m_cost(std::move(cost))
{
}

void resize(std::size_t newSize, qint64 value = 0)
{
m_cost.resize(newSize, value);
}
std::size_t size() const
{
return m_cost.size();
}

ItemCost operator+(const ItemCost& rhs) const
{
return {m_cost + rhs.m_cost};
}

ItemCost operator-(const ItemCost& rhs) const
{
return {m_cost - rhs.m_cost};
}

ItemCost& operator+=(const ItemCost& rhs)
{
m_cost += rhs.m_cost;
return *this;
}

ItemCost& operator-=(const ItemCost& rhs)
{
m_cost -= rhs.m_cost;
return *this;
}

qint64& operator[](int index)
{
if (static_cast<std::size_t>(index) >= m_cost.size()) {
resize(index + 1);
}
return m_cost[index];
}
qint64 operator[](int index) const
{
if (static_cast<std::size_t>(index) < m_cost.size()) {
return m_cost[index];
}
return 0;
}

qint64 sum() const
{
return m_cost.sum();
}

auto begin() const
{
return std::begin(m_cost);
}
auto end() const
{
return std::end(m_cost);
}

private:
std::valarray<qint64> m_cost;
};

QDebug operator<<(QDebug stream, const ItemCost& cost);

Expand Down
2 changes: 2 additions & 0 deletions src/recordpage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
#include <Solid/Device>
#include <Solid/Processor>

#include <cmath>

#include "multiconfigwidget.h"
#include "perfoutputwidgetkonsole.h"
#include "perfoutputwidgettext.h"
Expand Down
3 changes: 1 addition & 2 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#pragma once

#include <valarray>
#include <QHashFunctions>
#include <QtGlobal>

Expand All @@ -20,7 +19,7 @@ struct Symbol;
struct FileLine;
struct LocationCost;
class Costs;
using ItemCost = std::valarray<qint64>;
class ItemCost;
}

namespace KParts {
Expand Down

0 comments on commit d8de369

Please sign in to comment.