Closed GoogleCodeExporter closed 9 years ago
Original comment by kak@google.com
on 24 Sep 2014 at 9:33
One thing that could be done is
Object[] array = new Object[elements.size()];
checkState(array == elements.toArray(array));
return construct(array);
instead of
return construct(elements.toArray());
I'm not really sure it's worth it just to handle invalid Collection
implementations though.
Original comment by tavianator@gmail.com
on 24 Sep 2014 at 6:18
Calling toArray(array) will help with unintentional bugs. It's worth
consideration, though I wonder if it has performance disadvantages for some
collections.
The problem of malicious implementations is one that we've intentionally
decided not to tackle. We do need to fix our docs.
Original comment by cpov...@google.com
on 24 Sep 2014 at 6:24
I believe the sense in which the wiki page meant things was that it was safe to
*pass* ImmutableCollections to untrusted libraries, though there obviously
isn't anything we can do about reflection.
Original comment by lowas...@google.com
on 24 Sep 2014 at 6:26
Passing an own array would not help against malicious implementations as they
could grab the array anyway. Concerning contracts, it's even worse as I can't
see anything like "no references to it are maintained by this collection" in
the javadoc of `Collection.toArray(T[])`, though it's somehow obvious.
Original comment by Maaarti...@gmail.com
on 25 Sep 2014 at 4:29
Maaartinus, what do you mean? The Collection.toArray Javadoc states:
> The returned array will be "safe" in that no references to it are maintained
by this collection. (In other words, this method must allocate a new array even
if this collection is backed by an array). The caller is thus free to modify
the returned array.
Original comment by wasserman.louis
on 25 Sep 2014 at 4:33
I mean the proposal from comment #2 to use *the other* toArray method which
gives no such guarantee. Doing something like
<T> T[] toArray(T[] a) {
this.gotcha = super.toArray();
return this.gotcha;
}
would violate common sense, but no explicitly stated contract.
Original comment by Maaarti...@gmail.com
on 25 Sep 2014 at 5:11
This issue has been migrated to GitHub.
It can be found at https://github.com/google/guava/issues/<issue id>
Original comment by cgdecker@google.com
on 1 Nov 2014 at 4:08
Original comment by cgdecker@google.com
on 1 Nov 2014 at 4:17
Original comment by cgdecker@google.com
on 3 Nov 2014 at 9:07
Original issue reported on code.google.com by
phwend...@gmail.com
on 24 Sep 2014 at 6:41Attachments: