forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor LinkNode and add CHECKs for unsafe conditions
Calling base::LinkNode::Insert{Before,After} is invalid if it's already in a list and can easily lead to undefined behavior. This extracts them into a non-templated base and adds CHECKs. Bug: None Change-Id: Ib00fcd9ba513181336c4b02e846fa935fe5f46cf Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2405709 Commit-Queue: Collin Baker <collinbaker@chromium.org> Reviewed-by: Daniel Cheng <dcheng@chromium.org> Cr-Commit-Position: refs/heads/master@{#809092}
- Loading branch information
Showing
4 changed files
with
143 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright 2020 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "base/containers/linked_list.h" | ||
|
||
#include "base/check_op.h" | ||
|
||
namespace base { | ||
|
||
namespace internal { | ||
|
||
LinkNodeBase::LinkNodeBase() = default; | ||
|
||
LinkNodeBase::LinkNodeBase(LinkNodeBase* previous, LinkNodeBase* next) | ||
: previous_(previous), next_(next) {} | ||
|
||
LinkNodeBase::LinkNodeBase(LinkNodeBase&& rhs) { | ||
next_ = rhs.next_; | ||
rhs.next_ = nullptr; | ||
previous_ = rhs.previous_; | ||
rhs.previous_ = nullptr; | ||
|
||
// If the node belongs to a list, next_ and previous_ are both non-null. | ||
// Otherwise, they are both null. | ||
if (next_) { | ||
next_->previous_ = this; | ||
previous_->next_ = this; | ||
} | ||
} | ||
|
||
void LinkNodeBase::RemoveFromList() { | ||
previous_->next_ = next_; | ||
next_->previous_ = previous_; | ||
// next() and previous() return non-null if and only this node is not in any | ||
// list. | ||
next_ = nullptr; | ||
previous_ = nullptr; | ||
} | ||
|
||
void LinkNodeBase::InsertBeforeBase(LinkNodeBase* e) { | ||
CHECK_EQ(previous_, nullptr); | ||
CHECK_EQ(next_, nullptr); | ||
next_ = e; | ||
previous_ = e->previous_; | ||
e->previous_->next_ = this; | ||
e->previous_ = this; | ||
} | ||
|
||
void LinkNodeBase::InsertAfterBase(LinkNodeBase* e) { | ||
CHECK_EQ(previous_, nullptr); | ||
CHECK_EQ(next_, nullptr); | ||
next_ = e->next_; | ||
previous_ = e; | ||
e->next_->previous_ = this; | ||
e->next_ = this; | ||
} | ||
|
||
} // namespace internal | ||
|
||
} // namespace base |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters