diff options
author | Rui Gonçalves <ruippeixotog@gmail.com> | 2015-12-06 19:25:00 +0000 |
---|---|---|
committer | Rui Gonçalves <ruippeixotog@gmail.com> | 2015-12-06 19:25:00 +0000 |
commit | c17b18e173e07f421f9171272d94d84908526609 (patch) | |
tree | 3008e9e5be2acc44a6ffc7e56e84fe91fbcb0cb8 /src/library | |
parent | 5e99f82159842c5e2b60a1427e129b0b720a0d38 (diff) | |
download | scala-c17b18e173e07f421f9171272d94d84908526609.tar.gz scala-c17b18e173e07f421f9171272d94d84908526609.tar.bz2 scala-c17b18e173e07f421f9171272d94d84908526609.zip |
SI-9581 Fix overflow on Vector take and drop methods
Fixes the index/length comparison in `Vector#take` and `Vector#drop` so that they handle all possible integer values.
Given the collection's invariants `startIndex >= endIndex` and `0 >= startIndex, endIndex`, it is sufficient to change the arithmetic in the comparison as done in this commit to avoid overflows. As cases when `n <= 0` are handled beforehand, `endIndex - n` cannot overflow, contrary to `startIndex + n`. If without the danger of overflows the condition yields true, on the other hand, `startIndex + n` cannot overflow as it is smaller than `endIndex` (as the previous formulation of the condition shows).
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/collection/immutable/Vector.scala | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/src/library/scala/collection/immutable/Vector.scala b/src/library/scala/collection/immutable/Vector.scala index cd2d3f843b..5a9734a99e 100644 --- a/src/library/scala/collection/immutable/Vector.scala +++ b/src/library/scala/collection/immutable/Vector.scala @@ -156,7 +156,7 @@ override def companion: GenericCompanion[Vector] = Vector override def take(n: Int): Vector[A] = { if (n <= 0) Vector.empty - else if (startIndex + n < endIndex) + else if (startIndex < endIndex - n) dropBack0(startIndex + n) else this @@ -165,7 +165,7 @@ override def companion: GenericCompanion[Vector] = Vector override def drop(n: Int): Vector[A] = { if (n <= 0) this - else if (startIndex + n < endIndex) + else if (startIndex < endIndex - n) dropFront0(startIndex + n) else Vector.empty |