Use deep copy when copying bpPropertySets
Previously, only a shallow copy was made so the copy ends up sharing
some contents with the original. That was a potential source of bugs
as the copy was being made in order to be mutated.
This change switches to a deep copy; renaming the methods from
copy -> deepCopy to clarify the intent.
Makes the bpPropertySet member of bpModule a *bpPropertySet to avoid
unnecessary copying of bpPropertySet.
Bug: 142940300
Test: m nothing
Change-Id: I3f2eaa9fffab4e61d5a7cec81aa42fee9fdfec44
diff --git a/sdk/bp.go b/sdk/bp.go
index 19fb70d..ae06a09 100644
--- a/sdk/bp.go
+++ b/sdk/bp.go
@@ -51,13 +51,23 @@
return s.properties[name]
}
-func (s *bpPropertySet) copy() bpPropertySet {
+func (s *bpPropertySet) deepCopy() *bpPropertySet {
propertiesCopy := make(map[string]interface{})
for p, v := range s.properties {
- propertiesCopy[p] = v
+ var valueCopy interface{}
+ if ps, ok := v.(*bpPropertySet); ok {
+ valueCopy = ps.deepCopy()
+ } else if values, ok := v.([]string); ok {
+ valuesCopy := make([]string, len(values))
+ copy(valuesCopy, values)
+ valueCopy = valuesCopy
+ } else {
+ valueCopy = v
+ }
+ propertiesCopy[p] = valueCopy
}
- return bpPropertySet{
+ return &bpPropertySet{
properties: propertiesCopy,
order: append([]string(nil), s.order...),
}
@@ -95,15 +105,15 @@
}
type bpModule struct {
- bpPropertySet
+ *bpPropertySet
moduleType string
}
var _ android.BpModule = (*bpModule)(nil)
-func (m *bpModule) copy() *bpModule {
+func (m *bpModule) deepCopy() *bpModule {
return &bpModule{
- bpPropertySet: m.bpPropertySet.copy(),
+ bpPropertySet: m.bpPropertySet.deepCopy(),
moduleType: m.moduleType,
}
}
@@ -134,8 +144,9 @@
func (f *bpFile) newModule(moduleType string) *bpModule {
module := &bpModule{
- moduleType: moduleType,
+ moduleType: moduleType,
+ bpPropertySet: &bpPropertySet{},
}
- (&module.bpPropertySet).init()
+ module.bpPropertySet.init()
return module
}