Skip to content

Commit

Permalink
Fix: Loop Variable Capturing Issue in Go 1.22 (#171)
Browse files Browse the repository at this point in the history
* fix: remove copying loop variable

* fix:utilize new int range syntax instead of traditional 3 clause loop

* Remove unnecesary assignment

Co-authored-by: Davis Goodin <[email protected]>

---------

Co-authored-by: Davis Goodin <[email protected]>
  • Loading branch information
mertakman and dagood authored Sep 9, 2024
1 parent fc0ef3a commit 9dbdc19
Show file tree
Hide file tree
Showing 7 changed files with 5 additions and 11 deletions.
5 changes: 1 addition & 4 deletions hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ var hashes = [...]crypto.Hash{
func TestHash(t *testing.T) {
msg := []byte("testing")
for _, ch := range hashes {
ch := ch
t.Run(ch.String(), func(t *testing.T) {
t.Parallel()
if !openssl.SupportsHash(ch) {
Expand Down Expand Up @@ -90,7 +89,6 @@ func TestHash(t *testing.T) {
func TestHash_BinaryMarshaler(t *testing.T) {
msg := []byte("testing")
for _, ch := range hashes {
ch := ch
t.Run(ch.String(), func(t *testing.T) {
t.Parallel()
if !openssl.SupportsHash(ch) {
Expand Down Expand Up @@ -122,7 +120,6 @@ func TestHash_BinaryMarshaler(t *testing.T) {
func TestHash_Clone(t *testing.T) {
msg := []byte("testing")
for _, ch := range hashes {
ch := ch
t.Run(ch.String(), func(t *testing.T) {
t.Parallel()
if !openssl.SupportsHash(ch) {
Expand Down Expand Up @@ -163,7 +160,7 @@ func TestHash_ByteWriter(t *testing.T) {
h := cryptoToHash(ch)()
initSum := h.Sum(nil)
bw := h.(io.ByteWriter)
for i := 0; i < len(msg); i++ {
for i := range len(msg) {
bw.WriteByte(msg[i])
}
h.Reset()
Expand Down
2 changes: 1 addition & 1 deletion hkdf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ func TestHKDFMultiRead(t *testing.T) {
hkdf := newHKDF(tt.hash, tt.master, tt.salt, tt.info)
out := make([]byte, len(tt.out))

for b := 0; b < len(tt.out); b++ {
for b := range len(tt.out) {
n, err := io.ReadFull(hkdf, out[b:b+1])
if n != 1 || err != nil {
t.Errorf("test %d.%d: not enough output bytes: have %d, need %d .", i, b, n, len(tt.out))
Expand Down
1 change: 0 additions & 1 deletion hmac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ func TestHMAC(t *testing.T) {
{"sha512", NewSHA512},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
h := NewHMAC(tt.fn, nil)
Expand Down
4 changes: 2 additions & 2 deletions openssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ const wordBytes = bits.UintSize / 8
func (z BigInt) byteSwap() {
for i, d := range z {
var n uint = 0
for j := 0; j < wordBytes; j++ {
for j := range wordBytes {
n |= uint(byte(d)) << (8 * (wordBytes - j - 1))
d >>= 8
}
Expand Down Expand Up @@ -400,7 +400,7 @@ func bnToBinPad(bn C.GO_BIGNUM_PTR, to []byte) error {
if pad < 0 {
return errors.New("openssl: destination buffer too small")
}
for i := 0; i < pad; i++ {
for i := range pad {
to[i] = 0
}
if int(C.go_openssl_BN_bn2bin(bn, base(to[pad:]))) != n {
Expand Down
2 changes: 1 addition & 1 deletion openssl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestMain(m *testing.M) {
fmt.Println("OpenSSL version:", openssl.VersionText())
fmt.Println("FIPS enabled:", openssl.FIPS())
status := m.Run()
for i := 0; i < 5; i++ {
for range 5 {
// Run GC a few times to avoid false positives in leak detection.
runtime.GC()
// Sleep a bit to let the finalizers run.
Expand Down
1 change: 0 additions & 1 deletion rsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

func TestRSAKeyGeneration(t *testing.T) {
for _, size := range []int{2048, 3072} {
size := size
t.Run(strconv.Itoa(size), func(t *testing.T) {
t.Parallel()
priv, pub := newRSAKey(t, size)
Expand Down
1 change: 0 additions & 1 deletion tls1prf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ func TestTLS1PRF(t *testing.T) {
t.Skip("TLS PRF is not supported")
}
for _, tt := range tls1prfTests {
tt := tt
t.Run(tt.hash.String(), func(t *testing.T) {
if !openssl.SupportsHash(tt.hash) {
t.Skip("skipping: hash not supported")
Expand Down

0 comments on commit 9dbdc19

Please sign in to comment.