cc/cc.go: Harden GetSnapshot() against nil pointer panic
* Retrieve SnapshotInfo only if the calling module is device-variant.
* Test for nil-ness before calling ctx.OtherModuleProvider().
Bug: 192647618
Test: Presubmit
Change-Id: Ida8582f756e1b15f8c14c816ed6e736b47995877
diff --git a/cc/cc.go b/cc/cc.go
index 15cb39a..af224e9 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -2065,9 +2065,9 @@
}
func GetSnapshot(c LinkableInterface, snapshotInfo **SnapshotInfo, actx android.BottomUpMutatorContext) SnapshotInfo {
- // Only modules with BOARD_VNDK_VERSION uses snapshot. Others use the zero value of
+ // Only device modules with BOARD_VNDK_VERSION uses snapshot. Others use the zero value of
// SnapshotInfo, which provides no mappings.
- if *snapshotInfo == nil {
+ if *snapshotInfo == nil && c.Device() {
// Only retrieve the snapshot on demand in order to avoid circular dependencies
// between the modules in the snapshot and the snapshot itself.
var snapshotModule []blueprint.Module
@@ -2076,16 +2076,16 @@
} else if recoverySnapshotVersion := actx.DeviceConfig().RecoverySnapshotVersion(); recoverySnapshotVersion != "current" && recoverySnapshotVersion != "" && c.InRecovery() {
snapshotModule = actx.AddVariationDependencies(nil, nil, "recovery_snapshot")
}
- if len(snapshotModule) > 0 {
+ if len(snapshotModule) > 0 && snapshotModule[0] != nil {
snapshot := actx.OtherModuleProvider(snapshotModule[0], SnapshotInfoProvider).(SnapshotInfo)
*snapshotInfo = &snapshot
// republish the snapshot for use in later mutators on this module
actx.SetProvider(SnapshotInfoProvider, snapshot)
- } else {
- *snapshotInfo = &SnapshotInfo{}
}
}
-
+ if *snapshotInfo == nil {
+ *snapshotInfo = &SnapshotInfo{}
+ }
return **snapshotInfo
}