avoid a g++-12 warning about potential null pointer dereference

* spot/twaalgos/determinize.cc (sorted_nodes): Rewrite to
avoid reallocation of temporary vector.
This commit is contained in:
Alexandre Duret-Lutz 2022-12-09 16:35:05 +01:00
parent 16373cfb10
commit 2495004afd

View file

@ -472,15 +472,23 @@ namespace spot
std::vector<safra_state::safra_node_t> res; std::vector<safra_state::safra_node_t> res;
for (const auto& n: s.nodes_) for (const auto& n: s.nodes_)
{ {
int brace = n.second; // First, count the number of braces.
std::vector<int> tmp; unsigned nbraces = 0;
while (brace >= 0) for (int brace = n.second; brace >= 0; brace = s.braces_[brace])
++nbraces;
// Then list them in reverse order. Since we know the
// number of braces, we can allocate exactly what we need.
if (nbraces > 0)
{ {
// FIXME: is there a smarter way? std::vector<int> tmp(nbraces, 0);
tmp.insert(tmp.begin(), brace); for (int brace = n.second; brace >= 0; brace = s.braces_[brace])
brace = s.braces_[brace]; tmp[--nbraces] = brace;
res.emplace_back(n.first, std::move(tmp));
}
else
{
res.emplace_back(n.first, std::vector<int>{});
} }
res.emplace_back(n.first, std::move(tmp));
} }
std::sort(res.begin(), res.end(), compare()); std::sort(res.begin(), res.end(), compare());
return res; return res;