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: list 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 committed May 16, 2024
1 parent 0cdebbb commit 96603d4
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 3 deletions.
73 changes: 73 additions & 0 deletions src/models/data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,79 @@

using namespace Data;

ItemCost::ItemCost(std::size_t size)
: m_cost(size)
{
}

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

ItemCost::ItemCost(std::valarray<qint64>&& cost)
: m_cost(cost)
{
}
ItemCost::~ItemCost() = default;

void ItemCost::resize(std::size_t newSize, qint64 value)
{
m_cost.resize(newSize, value);
}

std::size_t ItemCost::size() const
{
return m_cost.size();
}

ItemCost ItemCost::operator+(const ItemCost& rhs) const
{
return static_cast<std::valarray<qint64>>(m_cost + rhs.m_cost);
}

ItemCost ItemCost::operator-(const ItemCost& rhs) const
{
return static_cast<std::valarray<qint64>>(m_cost - rhs.m_cost);
}

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

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

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

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

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

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

namespace {

ItemCost buildTopDownResult(const BottomUp& bottomUpData, const Costs& bottomUpCosts, TopDown* topDownData,
Expand Down
27 changes: 26 additions & 1 deletion src/models/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,32 @@ struct FrameLocation
Data::Location location;
};

using ItemCost = std::valarray<qint64>;
class ItemCost
{
public:
ItemCost(std::size_t size = 0);
ItemCost(qint64 value, std::size_t count);
ItemCost(std::valarray<qint64>&& cost);
~ItemCost();

void resize(std::size_t newSize, qint64 value = 0);
std::size_t size() const;

ItemCost operator+(const ItemCost& rhs) const;
ItemCost operator-(const ItemCost& rhs) const;
ItemCost& operator+=(const ItemCost& rhs);

qint64& operator[](int index);
qint64 operator[](int index) const;

qint64 sum() const;

auto begin() const;
auto end() const;

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 96603d4

Please sign in to comment.