LibDeviceTree: Fix stale pointers in phandle and parent handling

We need to set them after we've created the full tree, as otherwise the
HashMap containing the nodes may reallocate and invalidate the pointers.
This commit is contained in:
Hendiadyoin1 2024-04-27 13:43:13 +02:00 committed by Andrew Kaster
commit ffa9875fa6
Notes: sideshowbarker 2024-07-16 18:26:46 +09:00
2 changed files with 65 additions and 8 deletions

View file

@ -13,6 +13,7 @@
#include <AK/IterationDecision.h>
#include <AK/MemoryStream.h>
#include <AK/Optional.h>
#include <AK/RecursionDecision.h>
#include <AK/Span.h>
namespace DeviceTree {
@ -168,6 +169,28 @@ public:
// bonus points if it could automatically recurse in the tree under some conditions,
// like "simple-bus" or "pci-bridge" nodes
auto for_each_node(CallableAs<ErrorOr<RecursionDecision>, StringView, DeviceTreeNodeView const&> auto callback) const
{
auto iterate = [&](auto self, StringView name, DeviceTreeNodeView const& node) -> ErrorOr<RecursionDecision> {
auto result = TRY(callback(name, node));
if (result == RecursionDecision::Recurse) {
for (auto const& [name, child] : node.children()) {
auto child_result = TRY(self(self, name, child));
if (child_result == RecursionDecision::Break)
return RecursionDecision::Break;
}
return RecursionDecision::Continue;
}
return result;
};
return iterate(iterate, "/"sv, *this);
}
DeviceTreeNodeView const* phandle(u32 phandle) const
{
if (phandle >= m_phandles.size())
@ -184,6 +207,28 @@ private:
{
}
auto for_each_node(CallableAs<ErrorOr<RecursionDecision>, StringView, DeviceTreeNodeView&> auto callback)
{
auto iterate = [&](auto self, StringView name, DeviceTreeNodeView& node) -> ErrorOr<RecursionDecision> {
auto result = TRY(callback(name, node));
if (result == RecursionDecision::Recurse) {
for (auto& [name, child] : node.children()) {
auto child_result = TRY(self(self, name, child));
if (child_result == RecursionDecision::Break)
return RecursionDecision::Break;
}
return RecursionDecision::Continue;
}
return result;
};
return iterate(iterate, "/"sv, *this);
}
ErrorOr<void> set_phandle(u32 phandle, DeviceTreeNodeView* node)
{
if (m_phandles.size() > phandle && m_phandles[phandle] != nullptr)