]> Cypherpunks repositories - gostls13.git/commitdiff
sort: fix computation of maxDepth to avoid infinite loop
authorStefan Nilsson <snilsson@nada.kth.se>
Tue, 20 Mar 2012 21:23:12 +0000 (14:23 -0700)
committerRobert Griesemer <gri@golang.org>
Tue, 20 Mar 2012 21:23:12 +0000 (14:23 -0700)
The current computation loops indefinitely if n > 1<<30 (for 32-bit ints).

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

src/pkg/sort/sort.go

index 60f2d9ab40a1610a10c33671db3c6ce6205881a3..ca715645af4af2a404874b327f1046987dfacace 100644 (file)
@@ -186,10 +186,10 @@ func quickSort(data Interface, a, b, maxDepth int) {
 // Sort sorts data.
 // The algorithm used is not guaranteed to be a stable sort.
 func Sort(data Interface) {
-       // Switch to heapsort if depth of 2*ceil(lg(n)) is reached.
+       // Switch to heapsort if depth of 2*ceil(lg(n+1)) is reached.
        n := data.Len()
        maxDepth := 0
-       for 1<<uint(maxDepth) < n {
+       for i := n; i > 0; i >>= 1 {
                maxDepth++
        }
        maxDepth *= 2