Skip to content

Error when shrinking a slice property #287

@gjabell

Description

@gjabell

I am writing a dbus server which exposes an ObjectPath slice as a property. Adding a new element to the slice and calling prop.Set works as expected, but trying to remove an element from the slice fails with: dbus.Store: type mismatch: slices are different lengths need: 0 have: 1 due to this check:

dbus/dbus.go

Lines 281 to 286 in 8e438d3

if dest.Len() != src.Len() {
return fmt.Errorf(
"dbus.Store: type mismatch: "+
"slices are different lengths "+
"need: %d have: %d",
src.Len(), dest.Len())
.

Is there a reason why this check exists? The behavior seems to work correctly with the following change:

diff --git i/dbus.go w/dbus.go
index e9d014f..32ab49f 100644
--- i/dbus.go
+++ w/dbus.go
@@ -275,16 +275,9 @@ func storeSliceIntoInterface(dest, src reflect.Value) error {
 }
 
 func storeSliceIntoSlice(dest, src reflect.Value) error {
-	if dest.IsNil() || dest.Len() < src.Len() {
+	if dest.IsNil() || dest.Len() != src.Len() {
 		dest.Set(reflect.MakeSlice(dest.Type(), src.Len(), src.Cap()))
 	}
-	if dest.Len() != src.Len() {
-		return fmt.Errorf(
-			"dbus.Store: type mismatch: "+
-				"slices are different lengths "+
-				"need: %d have: %d",
-			src.Len(), dest.Len())
-	}
 	for i := 0; i < src.Len(); i++ {
 		err := store(dest.Index(i), getVariantValue(src.Index(i)))
 		if err != nil {

I can open a PR to fix this if that is a valid change, otherwise some guidance on how to do this properly would be appreciated.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions