Removing unsafe from ToActor

This might be a future proof way of doing these conversions, but they contain new allocations
This commit is contained in:
mariusor 2022-05-24 12:10:24 +02:00
parent a60c25cc5c
commit 7ed0d30e7c
No known key found for this signature in database
GPG key ID: DBF5E47F5DBC4D21
2 changed files with 10 additions and 5 deletions

View file

@ -740,6 +740,8 @@ func ToActivity(it Item) (*Activity, error) {
case Activity:
return &i, nil
case *IntransitiveActivity:
// TODO(marius): look at ToActor on how to copy the item to an Activity that we newly allocate
// Otherwise this behaviour of forcing the type to a "smaller" one will raise -race conditions
return (*Activity)(unsafe.Pointer(i)), nil
case IntransitiveActivity:
return (*Activity)(unsafe.Pointer(&i)), nil

View file

@ -7,7 +7,6 @@ import (
"fmt"
"reflect"
"time"
"unsafe"
"github.com/valyala/fastjson"
)
@ -496,11 +495,15 @@ func ToActor(it Item) (*Actor, error) {
case Actor:
return &i, nil
case *Object:
// TODO(marius): this is unsafe as Actor has a different memory layout than Object
// Everything should be fine as long as you don't try to read the Actor specific collections
return (*Actor)(unsafe.Pointer(i)), nil
// NOTE(marius): memory layout for Object is "smaller" than "Actor", so doing an unsafe pointer cast
// has led to -race conditions
a := new(Actor)
CopyItemProperties(a, i)
return a, nil
case Object:
return (*Actor)(unsafe.Pointer(&i)), nil
a := new(Actor)
CopyItemProperties(a, i)
return a, nil
default:
// NOTE(marius): this is an ugly way of dealing with the interface conversion error: types from different scopes
typ := reflect.TypeOf(new(Actor))