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

SingleConsumerQueue::size can return over-flowed int size #10

Closed
nitsanw opened this issue Apr 15, 2015 · 2 comments
Closed

SingleConsumerQueue::size can return over-flowed int size #10

nitsanw opened this issue Apr 15, 2015 · 2 comments

Comments

@nitsanw
Copy link

nitsanw commented Apr 15, 2015

This is unlikely, but there's nothing to stop the unbounded queue to grow beyond MAX_INT length and so:

@Override
public int size() {
  Node<E> h = head;
  Node<E> cursor = h.getNextRelaxed();
  int size = 0;
  while (cursor != null) { // should be while (cursor != null && size < MAX_INT)
    cursor = cursor.getNextRelaxed();
    size++;
  }
  return size;
}
@ben-manes
Copy link
Owner

All JDK queues, and in general throughout the collections framework, do not handle sizes larger than Integer.MAX_VALUE. Two examples are LinkedList and LinkedBlockingQueue. And of course array-based versions will crash due to the length limit of an array. Because this has not been an issue in practice, it is generally acceptable to not follow the contract strictly and leave it unhandled until deemed necessary.

The only JDK collections that I'm aware of that does handle the size threshold is ConcurrentHashMap and ConcurrentSkipList[Set, Map].

I think this is unlikely enough to not be covered.

@ben-manes
Copy link
Owner

Actually I might as well fix this, so I'll add it with #9's changes.

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

No branches or pull requests

2 participants