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
This commit is contained in:
Paul Duffin 2020-01-14 15:53:11 +00:00
parent dca59a516d
commit cc72e981c7
2 changed files with 21 additions and 10 deletions

View File

@ -51,13 +51,23 @@ func (s *bpPropertySet) getValue(name string) interface{} {
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 @@ func (s *bpPropertySet) insertAfter(position string, name string, value interfac
}
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) AddModule(module android.BpModule) {
func (f *bpFile) newModule(moduleType string) *bpModule {
module := &bpModule{
moduleType: moduleType,
moduleType: moduleType,
bpPropertySet: &bpPropertySet{},
}
(&module.bpPropertySet).init()
module.bpPropertySet.init()
return module
}

View File

@ -196,7 +196,7 @@ func (s *sdk) buildSnapshot(ctx android.ModuleContext) android.OutputPath {
for _, unversioned := range builder.prebuiltOrder {
// Copy the unversioned module so it can be modified to make it versioned.
versioned := unversioned.copy()
versioned := unversioned.deepCopy()
name := versioned.properties["name"].(string)
versioned.setProperty("name", builder.versionedSdkMemberName(name))
versioned.insertAfter("name", "sdk_member_name", name)
@ -286,7 +286,7 @@ func generateBpContents(contents *generatedContents, bpFile *bpFile) {
for _, bpModule := range bpFile.order {
contents.Printfln("")
contents.Printfln("%s {", bpModule.moduleType)
outputPropertySet(contents, &bpModule.bpPropertySet)
outputPropertySet(contents, bpModule.bpPropertySet)
contents.Printfln("}")
}
}