Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nit: validate ipcidr #247

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions transcoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@
var TranscoderIP4 = NewTranscoderFromFunctions(ip4StB, ip4BtS, nil)
var TranscoderIP6 = NewTranscoderFromFunctions(ip6StB, ip6BtS, nil)
var TranscoderIP6Zone = NewTranscoderFromFunctions(ip6zoneStB, ip6zoneBtS, ip6zoneVal)
var TranscoderIPCIDR = NewTranscoderFromFunctions(ipcidrStB, ipcidrBtS, nil)
var TranscoderIPCIDR = NewTranscoderFromFunctions(ipcidrStB, ipcidrBtS, ipcidrValidate)

func ipcidrBtS(b []byte) (string, error) {
if len(b) != 1 {
return "", fmt.Errorf("invalid length (should be == 1)")
if err := ipcidrValidate(b); err != nil {
return "", err

Check warning on line 63 in transcoders.go

View check run for this annotation

Codecov / codecov/patch

transcoders.go#L63

Added line #L63 was not covered by tests
}
return strconv.Itoa(int(b[0])), nil
}
Expand All @@ -73,6 +73,13 @@
return []byte{byte(uint8(ipMask))}, nil
}

func ipcidrValidate(b []byte) error {
if len(b) != 1 {
return fmt.Errorf("invalid length (should be == 1)")

Check warning on line 78 in transcoders.go

View check run for this annotation

Codecov / codecov/patch

transcoders.go#L78

Added line #L78 was not covered by tests
}
return nil
}

func ip4StB(s string) ([]byte, error) {
i := net.ParseIP(s).To4()
if i == nil {
Expand Down