Skip to content

Commit

Permalink
Copy byte slice in datatype decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
higebu committed Nov 24, 2023
1 parent 845be29 commit 1944ee1
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 12 deletions.
6 changes: 4 additions & 2 deletions diam/datatype/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type Address []byte

// DecodeAddress decodes an Address data type from byte array.
func DecodeAddress(b []byte) (Type, error) {
d := make([]byte, len(b))
copy(d, b)
if len(b) < 3 {
return nil, fmt.Errorf("Not enough data to make an Address from byte[%d] = %+v", len(b), b)
}
Expand All @@ -32,9 +34,9 @@ func DecodeAddress(b []byte) (Type, error) {
return nil, errors.New("Invalid length for IPv6")
}
default:
return Address(b), nil
return Address(d), nil
}
return Address(b[2:]), nil
return Address(d[2:]), nil
}

// Serialize implements the Type interface.
Expand Down
4 changes: 3 additions & 1 deletion diam/datatype/diamident.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ type DiameterIdentity OctetString

// DecodeDiameterIdentity decodes a DiameterIdentity from byte array.
func DecodeDiameterIdentity(b []byte) (Type, error) {
return DiameterIdentity(b), nil
d := make([]byte, len(b))
copy(d, b)
return DiameterIdentity(d), nil
}

// Serialize implements the Type interface.
Expand Down
3 changes: 2 additions & 1 deletion diam/datatype/diamuri.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ type DiameterURI OctetString

// DecodeDiameterURI decodes a DiameterURI from byte array.
func DecodeDiameterURI(b []byte) (Type, error) {
return DiameterURI(OctetString(b)), nil
d := make([]byte, len(b))
return DiameterURI(OctetString(d)), nil
}

// Serialize implements the Type interface.
Expand Down
4 changes: 3 additions & 1 deletion diam/datatype/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ type Grouped []byte

// DecodeGrouped decodes a Grouped data type from byte array.
func DecodeGrouped(b []byte) (Type, error) {
return Grouped(b), nil
d := make([]byte, len(b))
copy(d, b)
return Grouped(d), nil
}

// Serialize implements the Type interface.
Expand Down
4 changes: 3 additions & 1 deletion diam/datatype/ipfrule.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ type IPFilterRule OctetString

// DecodeIPFilterRule decodes an IPFilterRule data type from byte array.
func DecodeIPFilterRule(b []byte) (Type, error) {
return IPFilterRule(OctetString(b)), nil
d := make([]byte, len(b))
copy(d, b)
return IPFilterRule(OctetString(d)), nil
}

// Serialize implements the Type interface.
Expand Down
4 changes: 3 additions & 1 deletion diam/datatype/ipv4.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ func DecodeIPv4(b []byte) (Type, error) {
if len(b) != 4 {
return IPv4{0, 0, 0, 0}, nil
}
return IPv4(b), nil
d := make([]byte, len(b))
copy(d, b)
return IPv4(d), nil
}

// Serialize implements the Type interface.
Expand Down
4 changes: 3 additions & 1 deletion diam/datatype/ipv6.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ func DecodeIPv6(b []byte) (Type, error) {
if len(b) != net.IPv6len {
return IPv6(make(net.IP, net.IPv6len)), nil
}
return IPv6(b), nil
d := make([]byte, len(b))
copy(d, b)
return IPv6(d), nil
}

// Serialize implements the Type interface.
Expand Down
4 changes: 3 additions & 1 deletion diam/datatype/octetstring.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ type OctetString string

// DecodeOctetString decodes an OctetString from byte array.
func DecodeOctetString(b []byte) (Type, error) {
return OctetString(b), nil
d := make([]byte, len(b))
copy(d, b)
return OctetString(d), nil
}

// Serialize implements the Type interface.
Expand Down
4 changes: 3 additions & 1 deletion diam/datatype/qosfrule.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ type QoSFilterRule OctetString

// DecodeQoSFilterRule decodes an QoSFilterRule data type from byte array.
func DecodeQoSFilterRule(b []byte) (Type, error) {
return QoSFilterRule(OctetString(b)), nil
d := make([]byte, len(b))
copy(d, b)
return QoSFilterRule(OctetString(d)), nil
}

// Serialize implements the Type interface.
Expand Down
4 changes: 3 additions & 1 deletion diam/datatype/unknown.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ type Unknown []byte

// DecodeUnknown decodes an Unknown from byte array.
func DecodeUnknown(b []byte) (Type, error) {
return Unknown(b), nil
d := make([]byte, len(b))
copy(d, b)
return Unknown(d), nil
}

// Serialize implements the Type interface.
Expand Down
4 changes: 3 additions & 1 deletion diam/datatype/utf8string.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ type UTF8String OctetString

// DecodeUTF8String decodes an UTF8String data type from byte array.
func DecodeUTF8String(b []byte) (Type, error) {
return UTF8String(OctetString(b)), nil
d := make([]byte, len(b))
copy(d, b)
return UTF8String(OctetString(d)), nil
}

// Serialize implements the Type interface.
Expand Down

0 comments on commit 1944ee1

Please sign in to comment.