Skip to content

Commit

Permalink
validate ipcidr
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo committed Jun 12, 2024
1 parent 5d767c5 commit 32ec788
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions transcoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ func (t twrp) ValidateBytes(b []byte) error {
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 @@ func ipcidrStB(s string) ([]byte, error) {
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

0 comments on commit 32ec788

Please sign in to comment.