skmgoldin / sol-dll Goto Github PK
View Code? Open in Web Editor NEWDoubly-linked list in Solidity
Doubly-linked list in Solidity
The changes in 52d97ca address most of the inconsistent list states from re-inserting an existing node, but one remains: if the last node is reinserted with itself as the previous node, the node sets its next and previous pointers to itself, and the actual previous item in the list isn't updated to point to the right place.
(I haven't written a test case for this, so feel free to ask for one if you don't see the bug.)
Recommendation
diff --git a/contracts/DLL.sol b/contracts/DLL.sol
index 18d4965..5226132 100644
--- a/contracts/DLL.sol
+++ b/contracts/DLL.sol
@@ -52,11 +52,13 @@ library DLL {
*/
function insert(Data storage self, uint _prev, uint _curr, uint _next) public {
require(_curr != NULL_NODE_ID);
- require(_prev == NULL_NODE_ID || contains(self, _prev));
-
remove(self, _curr);
+ require(_prev == NULL_NODE_ID || contains(self, _prev));
+ require(_next == NULL_NODE_ID || contains(self, _next));
+
require(getNext(self, _prev) == _next);
+ require(getPrev(self, _next) == _prev);
self.dll[_curr].prev = _prev;
self.dll[_curr].next = _next;
Remove the current item from the list as early as possible in the function, right after the null node check. Since _prev
will no longer be in the list, the contains()
check will fail and the cycle won't be created. Make it harder to exploit lists that somehow get into a broken state by checking the invariants for _next
, even though we expect them to always be consistent with the checks for _prev
. Add a README that describes DLL
as a list of unique elements.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.