From 1944ee137312169bd6d598b01da0b6bd933e256c Mon Sep 17 00:00:00 2001 From: Yuya Kusakabe Date: Fri, 24 Nov 2023 16:37:12 +0900 Subject: [PATCH] Copy byte slice in datatype decoding Fixes #140 --- diam/datatype/address.go | 6 ++++-- diam/datatype/diamident.go | 4 +++- diam/datatype/diamuri.go | 3 ++- diam/datatype/group.go | 4 +++- diam/datatype/ipfrule.go | 4 +++- diam/datatype/ipv4.go | 4 +++- diam/datatype/ipv6.go | 4 +++- diam/datatype/octetstring.go | 4 +++- diam/datatype/qosfrule.go | 4 +++- diam/datatype/unknown.go | 4 +++- diam/datatype/utf8string.go | 4 +++- 11 files changed, 33 insertions(+), 12 deletions(-) diff --git a/diam/datatype/address.go b/diam/datatype/address.go index a168b20e..84c174b7 100644 --- a/diam/datatype/address.go +++ b/diam/datatype/address.go @@ -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) } @@ -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. diff --git a/diam/datatype/diamident.go b/diam/datatype/diamident.go index 6adc3ea8..cfe6e336 100644 --- a/diam/datatype/diamident.go +++ b/diam/datatype/diamident.go @@ -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. diff --git a/diam/datatype/diamuri.go b/diam/datatype/diamuri.go index 32ddf430..e3131396 100644 --- a/diam/datatype/diamuri.go +++ b/diam/datatype/diamuri.go @@ -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. diff --git a/diam/datatype/group.go b/diam/datatype/group.go index ff2830c3..52beea09 100644 --- a/diam/datatype/group.go +++ b/diam/datatype/group.go @@ -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. diff --git a/diam/datatype/ipfrule.go b/diam/datatype/ipfrule.go index 3beba60c..b2da0251 100644 --- a/diam/datatype/ipfrule.go +++ b/diam/datatype/ipfrule.go @@ -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. diff --git a/diam/datatype/ipv4.go b/diam/datatype/ipv4.go index de205f12..9732161e 100644 --- a/diam/datatype/ipv4.go +++ b/diam/datatype/ipv4.go @@ -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. diff --git a/diam/datatype/ipv6.go b/diam/datatype/ipv6.go index f20b4008..c4648c0d 100644 --- a/diam/datatype/ipv6.go +++ b/diam/datatype/ipv6.go @@ -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. diff --git a/diam/datatype/octetstring.go b/diam/datatype/octetstring.go index 4b7eb874..1f5d777c 100644 --- a/diam/datatype/octetstring.go +++ b/diam/datatype/octetstring.go @@ -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. diff --git a/diam/datatype/qosfrule.go b/diam/datatype/qosfrule.go index ddfeef4a..cb1f0980 100644 --- a/diam/datatype/qosfrule.go +++ b/diam/datatype/qosfrule.go @@ -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. diff --git a/diam/datatype/unknown.go b/diam/datatype/unknown.go index 7184dbd3..08d44d46 100644 --- a/diam/datatype/unknown.go +++ b/diam/datatype/unknown.go @@ -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. diff --git a/diam/datatype/utf8string.go b/diam/datatype/utf8string.go index ecd2f91a..fc71ba48 100644 --- a/diam/datatype/utf8string.go +++ b/diam/datatype/utf8string.go @@ -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.