Skip to content

Commit

Permalink
uintptr test: Fix overflow on architectures with 32-bit pointers (#100)
Browse files Browse the repository at this point in the history
Fixes #99

The current uintptr test assumes that pointers are 64-bit, so the test
fails to compile on architectures with 32-bit pointers. Instead, cast
-1 to uintptr, which matches math.MaxUint64 on 64-bit architectures, and
math.MaxUint32 on 32-bit architectures.

Verified by using GOARCH=386
  • Loading branch information
prashantv committed Jul 9, 2021
1 parent 557b938 commit 997edd6
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions uintptr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ package atomic

import (
"encoding/json"
"math"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -69,8 +69,11 @@ func TestUintptr(t *testing.T) {
t.Run("String", func(t *testing.T) {
// Use an integer with the signed bit set. If we're converting
// incorrectly, we'll get a negative value here.
atom := NewUintptr(uintptr(math.MaxUint64))
assert.Equal(t, "18446744073709551615", atom.String(),
// Use an int variable, as constants cause compile-time overflows.
negative := -1
atom := NewUintptr(uintptr(negative))
want := fmt.Sprint(uintptr(negative))
assert.Equal(t, want, atom.String(),
"String() returned an unexpected value.")
})
}

0 comments on commit 997edd6

Please sign in to comment.