From 6a8bb0562e6cb74f99e8138886708c3468d418fd Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Wed, 30 Nov 2022 00:17:58 -0500 Subject: [PATCH] ramfs: Correct usage of Nodes in Entry. * We do not want to set fNode in the constructor but let the invocation of Link() take care of doing so instead. * Link/Unlink manipulate linked-lists. Thus it is dangerous to use them in tandem on the same Node object. Add a check for this. * Minor cleanup. --- .../kernel/file_systems/ramfs/Entry.cpp | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/add-ons/kernel/file_systems/ramfs/Entry.cpp b/src/add-ons/kernel/file_systems/ramfs/Entry.cpp index d4ff177f35..f0877a7163 100644 --- a/src/add-ons/kernel/file_systems/ramfs/Entry.cpp +++ b/src/add-ons/kernel/file_systems/ramfs/Entry.cpp @@ -13,7 +13,7 @@ // constructor Entry::Entry(const char *name, Node *node, Directory *parent) : fParent(parent), - fNode(node), + fNode(NULL), fName(name), fReferrerLink(), fIterators() @@ -40,17 +40,18 @@ Entry::InitCheck() const status_t Entry::Link(Node *node) { - status_t error = (node ? B_OK : B_BAD_VALUE); + if (node == NULL) + return B_BAD_VALUE; + if (node == fNode) + return B_OK; + + // We first link to the new node and then unlink the old one. + Node *oldNode = fNode; + status_t error = node->Link(this); if (error == B_OK) { - // We first link to the new node and then unlink the old one. So, no - // harm is done, if both are the same. - Node *oldNode = fNode; - error = node->Link(this); - if (error == B_OK) { - fNode = node; - if (oldNode) - oldNode->Unlink(this); - } + fNode = node; + if (oldNode) + oldNode->Unlink(this); } return error; } @@ -101,4 +102,3 @@ Entry::GetAllocationInfo(AllocationInfo &info) info.AddStringAllocation(fName.GetLength()); fNode->GetAllocationInfo(info); } -