Skip to content

Commit

Permalink
Merge pull request #293 from gjabell/master
Browse files Browse the repository at this point in the history
store: fix shrinking slice during store
  • Loading branch information
guelfey authored Jan 6, 2022
2 parents e3cfec0 + f23a857 commit 081901d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
9 changes: 2 additions & 7 deletions dbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,13 +277,8 @@ func storeSliceIntoInterface(dest, src reflect.Value) error {
func storeSliceIntoSlice(dest, src reflect.Value) error {
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())
} else if dest.Len() > src.Len() {
dest.Set(dest.Slice(0, src.Len()))
}
for i := 0; i < src.Len(); i++ {
err := store(dest.Index(i), getVariantValue(src.Index(i)))
Expand Down
15 changes: 15 additions & 0 deletions store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,18 @@ func TestStoreNested(t *testing.T) {
dest, src)
}
}

func TestStoreSmallerSliceToLargerSlice(t *testing.T) {
src := []string{"baz"}
dest := []interface{}{"foo", "bar"}
err := Store([]interface{}{src}, &dest)
if err != nil {
t.Fatal(err)
}
if len(dest) != 1 {
t.Fatal("Expected dest slice to shrink")
}
if dest[0].(string) != "baz" {
t.Fatal("Wrong element saved in dest slice")
}
}

0 comments on commit 081901d

Please sign in to comment.