]> Cypherpunks repositories - gostls13.git/commitdiff
sort: avoid overflow in pivot calculation.
authorRob Pike <r@golang.org>
Tue, 30 Nov 2010 18:37:57 +0000 (10:37 -0800)
committerRob Pike <r@golang.org>
Tue, 30 Nov 2010 18:37:57 +0000 (10:37 -0800)
thanks to snilsson@nada.kth.se for the original CL.

R=gri
CC=golang-dev, snilsson
https://golang.org/cl/3280044

src/pkg/sort/sort.go

index c5b848414a8854f0a24fe64d1382fbac96de5c9d..2abe22d5c755c530c0497f2d80672380134d89eb 100644 (file)
@@ -63,7 +63,7 @@ func swapRange(data Interface, a, b, n int) {
 }
 
 func doPivot(data Interface, lo, hi int) (midlo, midhi int) {
-       m := (lo + hi) / 2
+       m := lo + (hi-lo)/2 // Written like this to avoid integer overflow.
        if hi-lo > 40 {
                // Tukey's ``Ninther,'' median of three medians of three.
                s := (hi - lo) / 8