Skip to content

Commit

Permalink
add pulse sample protection
Browse files Browse the repository at this point in the history
  • Loading branch information
xtaci committed Aug 28, 2024
1 parent 0b3010f commit 7296a7b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
5 changes: 4 additions & 1 deletion autotune.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ type autoTune struct {

// Sample adds a signal sample to the pulse buffer
func (tune *autoTune) Sample(bit bool, seq uint32) {
tune.pulses[seq%maxAutoTuneSamples] = pulse{bit, seq}
// ensure seq is in range [pulses[0].seq, pulses[0].seq + maxAutoTuneSamples]
if seq >= tune.pulses[0].seq && seq <= tune.pulses[0].seq+maxAutoTuneSamples {
tune.pulses[seq%maxAutoTuneSamples] = pulse{bit, seq}
}
}

// Find a period for a given signal
Expand Down
16 changes: 16 additions & 0 deletions autotune_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,20 @@ func TestAutoTune(t *testing.T) {
}
assert.Equal(t, -1, tune.FindPeriod(true))
assert.Equal(t, 4, tune.FindPeriod(false))

// minimal test
tune = autoTune{}
for i := 0; i < 1024; i++ {
if i%maxAutoTuneSamples == 0 {
tune.Sample(false, uint32(i))
} else {
tune.Sample(true, uint32(i))
}
}
assert.NotEqual(t, 0, tune.pulses[0].seq)
minSeq := tune.pulses[0].seq
t.Log("minimal seq", tune.pulses[0].seq)

tune.Sample(false, minSeq-1)
assert.Equal(t, minSeq, tune.pulses[0].seq)
}

0 comments on commit 7296a7b

Please sign in to comment.