Skip to content

Commit

Permalink
[#1301] Return zero for invalid identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
fivitti committed Apr 3, 2024
1 parent 28aa8a2 commit 66f17eb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
7 changes: 6 additions & 1 deletion backend/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,13 @@ func IsHexIdentifier(text string) bool {

// Counts the number of bytes in the provided hex identifier.
// It doesn't check if the input is a valid hex identifier. It may return
// a value even for malformed input.
// a value even for malformed input. Returns zero if the input is not a hex
// identifier.
func CountHexIdentifierBytes(text string) int {
if !IsHexIdentifier(text) {
return 0
}

// Remove any whitespace and colons.
replacer := strings.NewReplacer(" ", "", ":", "")
identifier := replacer.Replace(text)
Expand Down
12 changes: 6 additions & 6 deletions backend/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ func TestCountHexIdentifierBytes(t *testing.T) {
// Invalid output for invalid input.
require.Zero(t, CountHexIdentifierBytes(" "))
require.Zero(t, CountHexIdentifierBytes(""))
require.Equal(t, 3, CountHexIdentifierBytes("1234gh"))
require.Equal(t, 2, CountHexIdentifierBytes("12:56:"))
require.Equal(t, 2, CountHexIdentifierBytes("12:56:9"))
require.Equal(t, 2, CountHexIdentifierBytes("ab,cd"))
require.Equal(t, 2, CountHexIdentifierBytes("ab: cd"))
require.Equal(t, 2, CountHexIdentifierBytes("abcde"))
require.Zero(t, CountHexIdentifierBytes("1234gh"))
require.Zero(t, CountHexIdentifierBytes("12:56:"))
require.Zero(t, CountHexIdentifierBytes("12:56:9"))
require.Zero(t, CountHexIdentifierBytes("ab,cd"))
require.Zero(t, CountHexIdentifierBytes("ab: cd"))
require.Zero(t, CountHexIdentifierBytes("abcde"))
}

// Check if BytesToHex works.
Expand Down

0 comments on commit 66f17eb

Please sign in to comment.