Skip to content

Commit 86768ee

Browse files
committed
Idiomatic change: using an int like a bool
1 parent f0326eb commit 86768ee

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

packet.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ func validateSequence(packetLen int, prefixByte uint8, sequence uint64, readPack
566566

567567
// replay protection (optional)
568568
if replayProtection != nil && PacketType(packetType) >= ConnectionKeepAlive {
569-
if replayProtection.AlreadyReceived(sequence) == 1 {
569+
if replayProtection.AlreadyReceived(sequence) {
570570
v := strconv.FormatUint(sequence, 10)
571571
return errors.New("ignored connection payload packet. sequence " + v + " already received (replay protection)")
572572
}

replay_protection.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ func (r *ReplayProtection) Reset() {
2121
}
2222

2323
// Tests that the sequence has not already been recv'd, adding it to the buffer if it's new.
24-
func (r *ReplayProtection) AlreadyReceived(sequence uint64) int {
24+
func (r *ReplayProtection) AlreadyReceived(sequence uint64) bool {
2525
if sequence&(uint64(1<<63)) != 0 {
26-
return 0
26+
return false
2727
}
2828

2929
if sequence+REPLAY_PROTECTION_BUFFER_SIZE <= r.MostRecentSequence {
30-
return 1
30+
return true
3131
}
3232

3333
if sequence > r.MostRecentSequence {
@@ -38,15 +38,15 @@ func (r *ReplayProtection) AlreadyReceived(sequence uint64) int {
3838

3939
if r.ReceivedPacket[index] == 0xFFFFFFFFFFFFFFFF {
4040
r.ReceivedPacket[index] = sequence
41-
return 0
41+
return false
4242
}
4343

4444
if r.ReceivedPacket[index] >= sequence {
45-
return 1
45+
return true
4646
}
4747

4848
r.ReceivedPacket[index] = sequence
49-
return 0
49+
return false
5050
}
5151

5252
func clearPacketBuffer(packets []uint64) {

replay_protection_test.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ func TestReplayProtection_Reset(t *testing.T) {
1515

1616
func TestReplayProtection(t *testing.T) {
1717
r := NewReplayProtection()
18-
for i := 0; i < 2; i+=1 {
18+
for i := 0; i < 2; i += 1 {
1919
r.Reset()
2020
if r.MostRecentSequence != 0 {
2121
t.Fatalf("sequence was not 0")
2222
}
2323

2424
// sequence numbers with high bit set should be ignored
2525
sequence := uint64(1 << 63)
26-
if r.AlreadyReceived(sequence) != 0 {
26+
if r.AlreadyReceived(sequence) {
2727
t.Fatalf("sequence numbers with high bit set should be ignored")
2828
}
2929

@@ -33,34 +33,34 @@ func TestReplayProtection(t *testing.T) {
3333

3434
// the first time we receive packets, they should not be already received
3535
maxSequence := uint64(REPLAY_PROTECTION_BUFFER_SIZE * 4)
36-
for sequence = 0; sequence < maxSequence; sequence+=1 {
37-
if r.AlreadyReceived(sequence) != 0 {
36+
for sequence = 0; sequence < maxSequence; sequence += 1 {
37+
if r.AlreadyReceived(sequence) {
3838
t.Fatalf("the first time we receive packets, they should not be already received")
3939
}
4040
}
4141

4242
// old packets outside buffer should be considered already received
43-
if r.AlreadyReceived(0) != 1 {
43+
if !r.AlreadyReceived(0) {
4444
t.Fatalf("old packets outside buffer should be considered already received")
4545
}
4646

4747
// packets received a second time should be flagged already received
48-
for sequence = maxSequence - 10; sequence < maxSequence; sequence+=1 {
49-
if r.AlreadyReceived(sequence) != 1 {
48+
for sequence = maxSequence - 10; sequence < maxSequence; sequence += 1 {
49+
if !r.AlreadyReceived(sequence) {
5050
t.Fatalf("packets received a second time should be flagged already received")
5151
}
5252
}
5353

5454
// jumping ahead to a much higher sequence should be considered not already received
55-
if r.AlreadyReceived(maxSequence+REPLAY_PROTECTION_BUFFER_SIZE) != 0 {
55+
if r.AlreadyReceived(maxSequence + REPLAY_PROTECTION_BUFFER_SIZE) {
5656
t.Fatalf("jumping ahead to a much higher sequence should be considered not already received")
5757
}
5858

5959
// old packets should be considered already received
60-
for sequence = 0; sequence < maxSequence; sequence+=1 {
61-
if r.AlreadyReceived(sequence) != 1 {
60+
for sequence = 0; sequence < maxSequence; sequence += 1 {
61+
if !r.AlreadyReceived(sequence) {
6262
t.Fatalf("old packets should be considered already received")
6363
}
6464
}
6565
}
66-
}
66+
}

0 commit comments

Comments
 (0)