Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LinkedLruHashMap: Bad state when remove() removes the last entry #385

Closed
hugovangalen opened this issue Nov 16, 2017 · 2 comments
Closed
Labels

Comments

@hugovangalen
Copy link

hugovangalen commented Nov 16, 2017

My own code crashed in the method _removeLru with a null pointer access when calling _tail.next.

After debugging I discovered that the cause seems to be in the remove() method.

There exists a check whether the removed entry is equal to _head, or it is equal to _tail.

If it happens to be the last item that gets removed, that is not handled correctly. Right now it branches into the entry == _head, resets _head, but leaves _tail in its old value, which is wrong.

If my understanding is correct, in that case both _tail and _head need to be reset to null. Adding a test for entry == _head && entry == _tail to reset the _tail and _head to null appears to fix the problem:

  @override
  V remove(Object key) {
    final entry = _entries.remove(key);
    if (entry != null) {
      if (entry == _head && entry == _tail) {
        _head = null;
        _tail = null;
      } else if (entry == _head) {
        _head = _head.next;
      } else if (entry == _tail) {
        _tail.previous.next = null;
        _tail = _tail.previous;
      } else {
        entry.previous.next = entry.next;
      }

      return entry.value;
    }
    return null;
  }
@cbracken
Copy link
Member

Fixed by #386 and released in v0.26.1. Thanks for diagnosing and reporting this!

@hugovangalen
Copy link
Author

@cbracken No problem. I'd like to stress that there are still issues with dangling references. See ticket #412.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants