Skip to content

Commit

Permalink
Prevent overflow in sort functions provided to slices.SortFunc
Browse files Browse the repository at this point in the history
Replaces subtraction based comparisions with calls to cmp.Compare
for all ordered types to avoid overflows.
  • Loading branch information
rosstimothy committed Feb 26, 2024
1 parent 0c9d397 commit 5223966
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion api/types/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func (i *InstanceV1) AppendControlLog(entries ...InstanceControlLogEntry) {
i.Spec.ControlLog[idx].Time = entry.Time.UTC()
}
slices.SortFunc(i.Spec.ControlLog, func(a, b InstanceControlLogEntry) int {
return int(a.Time.UnixNano() - b.Time.UnixNano())
return a.Time.Compare(b.Time)
})
}

Expand Down
3 changes: 2 additions & 1 deletion lib/auth/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package auth

import (
"cmp"
"context"
"crypto/tls"
"crypto/x509"
Expand Down Expand Up @@ -1445,7 +1446,7 @@ func applyResources(ctx context.Context, service *Services, resources []types.Re
slices.SortFunc(resources, func(a, b types.Resource) int {
priorityA := ResourceApplyPriority[a.GetKind()]
priorityB := ResourceApplyPriority[b.GetKind()]
return priorityA - priorityB
return cmp.Compare(priorityA, priorityB)
})
for _, resource := range resources {
// Unwrap "new style" resources.
Expand Down
3 changes: 2 additions & 1 deletion lib/auth/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package migration

import (
"cmp"
"context"
"encoding/json"
"slices"
Expand Down Expand Up @@ -97,7 +98,7 @@ func Apply(ctx context.Context, b backend.Backend, opts ...func(c *applyConfig))
}()

slices.SortFunc(cfg.migrations, func(a, b migration) int {
return int(a.Version() - b.Version())
return cmp.Compare(a.Version(), b.Version())
})

current, err := getCurrentMigration(ctx, b)
Expand Down
5 changes: 3 additions & 2 deletions lib/events/azsessions/azsessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package azsessions

import (
"cmp"
"context"
"encoding/base64"
"fmt"
Expand Down Expand Up @@ -324,7 +325,7 @@ func (h *Handler) CompleteUpload(ctx context.Context, upload events.StreamUpload
// cleaned up before a new attempt

parts = slices.Clone(parts)
slices.SortFunc(parts, func(a, b events.StreamPart) int { return int(a.Number - b.Number) })
slices.SortFunc(parts, func(a, b events.StreamPart) int { return cmp.Compare(a.Number, b.Number) })

partURLs := make([]string, 0, len(parts))
for _, part := range parts {
Expand Down Expand Up @@ -496,7 +497,7 @@ func (h *Handler) ListParts(ctx context.Context, upload events.StreamUpload) ([]
}
}

slices.SortFunc(parts, func(a, b events.StreamPart) int { return int(a.Number - b.Number) })
slices.SortFunc(parts, func(a, b events.StreamPart) int { return cmp.Compare(a.Number, b.Number) })

return parts, nil
}
Expand Down

0 comments on commit 5223966

Please sign in to comment.