Skip to content

Commit

Permalink
Handle unprintable characters in kiss codes.
Browse files Browse the repository at this point in the history
  • Loading branch information
beevik committed Oct 15, 2017
1 parent 0201bad commit 8ee33ea
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ntp.go
Expand Up @@ -548,11 +548,18 @@ func toInterval(t int8) time.Duration {
}

func kissCode(id uint32) string {
isPrintable := func(ch byte) bool { return ch >= 32 && ch <= 126 }

b := []byte{
byte(id >> 24),
byte(id >> 16),
byte(id >> 8),
byte(id),
}
for _, ch := range b {
if !isPrintable(ch) {
return ""
}
}
return string(b)
}
4 changes: 4 additions & 0 deletions ntp_test.go
Expand Up @@ -326,6 +326,10 @@ func TestKissCode(t *testing.T) {
{0x52415445, "RATE"},
{0x524d4f54, "RMOT"},
{0x53544550, "STEP"},
{0x01010101, ""},
{0xfefefefe, ""},
{0x01544450, ""},
{0x41544401, ""},
}
for _, c := range codes {
assert.Equal(t, kissCode(c.id), c.str)
Expand Down

0 comments on commit 8ee33ea

Please sign in to comment.