Distinguish metadata slice views from cycles
This commit is contained in:
@@ -24,6 +24,7 @@ func CloneMetadata(metadata map[string]any) (map[string]any, error) {
|
||||
type metadataVisit struct {
|
||||
typ reflect.Type
|
||||
ptr uintptr
|
||||
len int
|
||||
}
|
||||
|
||||
func cloneMetadataValue(value reflect.Value, location string, active map[metadataVisit]struct{}) (reflect.Value, error) {
|
||||
@@ -71,6 +72,9 @@ func cloneMetadataValue(value reflect.Value, location string, active map[metadat
|
||||
if value.IsNil() {
|
||||
return reflect.Zero(value.Type()), nil
|
||||
}
|
||||
if value.Len() == 0 {
|
||||
return reflect.MakeSlice(value.Type(), 0, 0), nil
|
||||
}
|
||||
leave, err := enterMetadataValue(value, active, location)
|
||||
if err != nil {
|
||||
return reflect.Value{}, err
|
||||
@@ -111,6 +115,9 @@ func cloneMetadataValue(value reflect.Value, location string, active map[metadat
|
||||
|
||||
func enterMetadataValue(value reflect.Value, active map[metadataVisit]struct{}, location string) (func(), error) {
|
||||
visit := metadataVisit{typ: value.Type(), ptr: value.Pointer()}
|
||||
if value.Kind() == reflect.Slice {
|
||||
visit.len = value.Len()
|
||||
}
|
||||
if _, exists := active[visit]; exists {
|
||||
return nil, fmt.Errorf("%s contains a cycle", location)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user