Skip to content

Commit

Permalink
B #6580: Fix casting in Template::remove (#3057)
Browse files Browse the repository at this point in the history
The Template::remove could crash if user assign Single value to attribute, which is expected to be a Vector, e.g. SCHED_ACTION, PCI, ...

Other minor changes:
* refactor the 'set' method - no need to call private _set
* Simplify some callers of the Template::remove method
  • Loading branch information
paczerny authored May 14, 2024
1 parent 52420f3 commit 5dda6c3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 40 deletions.
43 changes: 24 additions & 19 deletions include/Template.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,16 +221,15 @@ class Template
* will be freed when the template destructor is called.
* @param attr pointer to the attribute
*/
virtual void set(Attribute * attr);
void set(Attribute * attr);

virtual void set(std::vector<SingleAttribute *>& values)
{
_set<SingleAttribute>(values);
}

virtual void set(std::vector<VectorAttribute *>& values)
template<typename T>
void set(const std::vector<T *>& values)
{
_set<VectorAttribute>(values);
for (auto v : values)
{
set(v);
}
}

/**
Expand Down Expand Up @@ -302,7 +301,15 @@ class Template

for ( auto i = index.first; i != index.second; i++,j++ )
{
values.push_back(static_cast<T *>(i->second));
auto att = dynamic_cast<T*>(i->second);

if (!att)
{
delete i->second;
continue;
}

values.push_back(att);
}

attributes.erase(index.first, index.second);
Expand All @@ -319,7 +326,14 @@ class Template

for ( auto i = index.first; i != index.second; i++,j++ )
{
std::unique_ptr<T> va(static_cast<T *>(i->second));
std::unique_ptr<T> va(dynamic_cast<T *>(i->second));

if (!va)
{
delete i->second;
continue;
}

values.push_back(std::move(va));
}

Expand Down Expand Up @@ -699,15 +713,6 @@ class Template
return const_cast<T *>(
static_cast<const Template&>(*this).__get<T>(s));
}

template<typename T>
void _set(const std::vector<T *>& values)
{
for (auto v : values)
{
set(v);
}
}
};

/* -------------------------------------------------------------------------- */
Expand Down
14 changes: 3 additions & 11 deletions src/host/HostShareDatastore.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ using namespace std;

void HostShareDatastore::set_monitorization(Template& ht)
{
vector<Attribute *> vector_ds;
vector<VectorAttribute *> vector_ds;

// -------------------------------------------------------------------------
// Get overall datastore information
Expand All @@ -47,17 +47,9 @@ void HostShareDatastore::set_monitorization(Template& ht)

ht.remove("DS", vector_ds);

for (auto it = vector_ds.begin(); it != vector_ds.end(); ++it)
for (auto ds: vector_ds)
{
auto ds_attr = dynamic_cast<VectorAttribute*>(*it);

if (ds_attr == 0)
{
delete *it;
continue;
}

set(*it);
set(ds);
}
}

Expand Down
11 changes: 2 additions & 9 deletions src/monitor/src/data_model/VirtualMachineBase.cc
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ static bool isVolatile(const VectorAttribute * disk)

void VirtualMachineBase::init_storage_usage()
{
vector<Attribute *> disks;
vector<VectorAttribute *> disks;

long long size;
long long snapshot_size;
Expand All @@ -168,15 +168,8 @@ void VirtualMachineBase::init_storage_usage()

int num = vm_template->remove("DISK", disks);

for (auto d : disks)
for (auto disk : disks)
{
const VectorAttribute * disk = dynamic_cast<const VectorAttribute*>(d);

if (disk == 0)
{
continue;
}

if (disk->vector_value("SIZE", size) != 0)
{
continue;
Expand Down
2 changes: 1 addition & 1 deletion src/vm/Backups.cc
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ int Backups::from_xml(const ObjectXML* xml)
int Backups::parse(Template *tmpl, bool can_increment,
bool append, std::string& error_str)
{
vector<VectorAttribute *> cfg_a;
vector<Attribute *> cfg_a;

int iattr;
bool battr;
Expand Down

0 comments on commit 5dda6c3

Please sign in to comment.