Distinguish metadata slice views from cycles

This commit is contained in:
2026-11-26 01:30:49 +00:00
parent b6f04297d4
commit 97bcf8cae6
4 changed files with 78 additions and 3 deletions

View File

@@ -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)
}