You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
parseField should use triple slice assignments ([x:y:z]) to specify the capacity of slices when dealing with RawContent and RawValue fields. Defining the capacity of these slices will prevent other fields in a struct that reference the same underlying byte slice from inadvertently being mutated by operations that attempt to expand the slice (i.e. append).
What version of Go are you using (go version)? go1.6
What operating system and processor architecture are you using (go env)? Ubuntu 15.10 amd64
package main
import "fmt"
import "encoding/asn1"
type outer struct {
Raw asn1.RawContent
Version int `asn1:"optional,default:1,tag:0"`
Inner inner
Other asn1.RawValue
}
type inner struct {
Raw asn1.RawContent
Version int `asn1:"optional,default:2,tag:2"`
}
func main() {
a := outer{Version: 1, Inner: inner{Version:2}, Other: asn1.RawValue{1, 1, false, []byte{10}, []byte{1, 1, 10}}}
out, _ := asn1.Marshal(a)
var b outer
asn1.Unmarshal(out, &b)
fmt.Println(b.Raw)
// Since b.Inner.Raw and b.Raw share the shame underlying slice when
// append extends b.Inner.Raw (since it has no capacity) it is extending
// into the part of the underlying slice which is referenced by b.Raw and in
// doing so overwrites a section of that slice
_ = append((&b).Inner.Raw, []byte{255, 255, 255}...)
fmt.Println(b.Raw)
}
What did you expect to see?
In above example b.Raw shouldn't be affected by using append
What did you see instead?
The last three bytes of b.Raw are overwritten.
The text was updated successfully, but these errors were encountered:
rolandshoemaker
changed the title
encoding/asn1: When slicing bytes specify capacity
encoding/asn1: When slicing bytes specify capacity or allocate new slice
Mar 20, 2016
Thinking about it setting the capacity may not be the right approach since it means those arrays can never be grown without causing a panic. Perhaps it would be better (although less efficient) to create new slices for each field and copy into them...
parseField
should use triple slice assignments ([x:y:z]
) to specify the capacity of slices when dealing withRawContent
andRawValue
fields. Defining the capacity of these slices will prevent other fields in a struct that reference the same underlying byte slice from inadvertently being mutated by operations that attempt to expand the slice (i.e.append
).go version
)?go1.6
go env
)?Ubuntu 15.10 amd64
Basic example: https://play.golang.org/p/ibPZYcKNs5
In above example
b.Raw
shouldn't be affected by usingappend
The last three bytes of
b.Raw
are overwritten.The text was updated successfully, but these errors were encountered: