<fix>[storage]: ZSTAC-80789 return defensive copy from getPreferBackupStorageTypes#3547
<fix>[storage]: ZSTAC-80789 return defensive copy from getPreferBackupStorageTypes#3547zstack-robot-2 wants to merge 3 commits into5.5.12from
Conversation
…s to prevent Bean mutation Resolves: ZSTAC-80789 Change-Id: I9348a655bea0bbcff53abfd2c44f73342190f1ac
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: http://open.zstack.ai:20001/code-reviews/zstack-cloud.yaml (via .coderabbit.yaml) Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
概述三个存储工厂类(Expon、XInfini、Zbs)中的 变更
代码审查工作量估计🎯 2 (Simple) | ⏱️ ~15 分钟 诗
Important Pre-merge checks failedPlease resolve all errors before merging. Addressing warnings is optional. ❌ Failed checks (1 error, 1 warning)
✅ Passed checks (1 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
plugin/xinfini/src/main/java/org/zstack/xinfini/XInfiniStorageFactory.java (1)
58-60: 潜在的空指针异常风险如果
preferBackupStorageTypes在 setter 被调用前为 null,new ArrayList<>(null)会抛出NullPointerException。考虑到这是一个 Spring Bean,setter 通常会在初始化时被调用,但建议添加防御性检查以增强健壮性:
🛡️ 建议添加空值检查
`@Override` public List<String> getPreferBackupStorageTypes() { + if (preferBackupStorageTypes == null) { + return new ArrayList<>(); + } return new ArrayList<>(preferBackupStorageTypes); }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@plugin/xinfini/src/main/java/org/zstack/xinfini/XInfiniStorageFactory.java` around lines 58 - 60, The getter getPreferBackupStorageTypes can throw NPE when preferBackupStorageTypes is null; modify it to defensively handle null by returning Collections.emptyList() or new ArrayList<>() when preferBackupStorageTypes == null, otherwise return a new ArrayList<>(preferBackupStorageTypes); ensure any setter or initialization code for preferBackupStorageTypes remains unchanged but the getter never constructs ArrayList from a null reference.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@plugin/xinfini/src/main/java/org/zstack/xinfini/XInfiniStorageFactory.java`:
- Around line 58-60: The getter getPreferBackupStorageTypes can throw NPE when
preferBackupStorageTypes is null; modify it to defensively handle null by
returning Collections.emptyList() or new ArrayList<>() when
preferBackupStorageTypes == null, otherwise return a new
ArrayList<>(preferBackupStorageTypes); ensure any setter or initialization code
for preferBackupStorageTypes remains unchanged but the getter never constructs
ArrayList from a null reference.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: http://open.zstack.ai:20001/code-reviews/zstack-cloud.yaml (via .coderabbit.yaml)
Review profile: CHILL
Plan: Pro
Run ID: 2e5e39da-3ef7-4aa5-8ee0-0bb75bc3147a
📒 Files selected for processing (4)
plugin/expon/src/main/java/org/zstack/expon/ExponStorageFactory.javaplugin/xinfini/src/main/java/org/zstack/xinfini/XInfiniStorageFactory.javaplugin/zbs/src/main/java/org/zstack/storage/zbs/ZbsStorageFactory.javastorage/src/test/java/org/zstack/storage/addon/primary/ExternalPrimaryStorageSelectBsTest.java
…efensive copy fix Resolves: ZSTAC-80789 Change-Id: I23b24745b605b0d861dd3fdab11cf2044b31e5fe
Rewrite ExternalPrimaryStorageSelectBackupStorageCase to create a VM on ZBS ExternalPrimaryStorage via API, attach both ImageStoreBackupStorage and CephBackupStorage to the zone, and focus solely on the ZSTAC-80789 retainAll corruption bug. Remove unrelated ZSTAC-71706 tests. Resolves: ZSTAC-80789 Change-Id: Ia4ff4433a93606bf8ff4f876ae2693388181cac9
问题
ExternalPrimaryStorage.handle(SelectBackupStorageMsg)中selector.getPreferBackupStorageTypes()直接返回 Spring Bean 内部字段引用,后续retainAll()原地修改了 Bean 状态,导致一次触发后所有后续克隆操作的备份存储类型过滤永久失效。修复
在
XInfiniStorageFactory、ExponStorageFactory、ZbsStorageFactory三个实现中,getPreferBackupStorageTypes()返回new ArrayList<>(preferBackupStorageTypes)防御性拷贝。测试
ExternalPrimaryStorageSelectBsTest2 个 UT 验证修复有效且能复现原始 bugResolves: ZSTAC-80789
sync from gitlab !9406