Skip to content

Comments

<fix>[multi]: batch guard NPE quality issues#3378

Open
zstack-robot-1 wants to merge 1 commit into5.5.6from
sync/ye.zou/fix/npe-batch-quality-issues@@2
Open

<fix>[multi]: batch guard NPE quality issues#3378
zstack-robot-1 wants to merge 1 commit into5.5.6from
sync/ye.zou/fix/npe-batch-quality-issues@@2

Conversation

@zstack-robot-1
Copy link
Collaborator

Summary

Batch fix for 25 NPE issues found by periodic quality scripts.

  • UpdateQueryImpl: guard val.getClass() NPE
  • LogSafeGson: return JsonNull when input is null
  • HostAllocatorChain: null check completion
  • VmInstanceVO: use Objects.equals to avoid NPE
  • SessionManagerImpl: guard null session
  • VmCapabilitiesJudger: guard null PS type result
  • CephPSMonBase: guard null self when evicted
  • CephPSBase: guard null mon.getSelf()
  • HostBase: guard null self when HostVO deleted
  • ExternalPSFactory: guard null URI protocol
  • LocalStorageBase: guard null errorCode.getCause()

Resolves

ZSTAC-69300, ZSTAC-69957, ZSTAC-71973, ZSTAC-81294, ZSTAC-70180, ZSTAC-70181, ZSTAC-78309, ZSTAC-78310, ZSTAC-70668, ZSTAC-71909, ZSTAC-80555, ZSTAC-81270, ZSTAC-70101, ZSTAC-72034, ZSTAC-73197, ZSTAC-79921, ZSTAC-81160, ZSTAC-81224 + more

Testing

  • Compile verified: mvn compile -pl affected-modules -am -Dmaven.test.skip
  • All changes are defensive null guards — low risk of behavioral change

sync from gitlab !9213

- UpdateQueryImpl: guard val.getClass() NPE
- LogSafeGson: return JsonNull when input is null
- HostAllocatorChain: null check completion
- VmInstanceVO: use Objects.equals to avoid NPE
- SessionManagerImpl: guard null session
- VmCapabilitiesJudger: guard null PS type result
- CephPSMonBase: guard null self when evicted
- CephPSBase: guard null mon.getSelf()
- HostBase: guard null self when HostVO deleted
- ExternalPSFactory: guard null URI protocol
- LocalStorageBase: guard null errorCode.getCause()

Resolves: ZSTAC-69300, ZSTAC-69957, ZSTAC-71973,
 ZSTAC-81294, ZSTAC-70180, ZSTAC-70181,
 ZSTAC-78309, ZSTAC-78310, ZSTAC-70668,
 ZSTAC-71909, ZSTAC-80555, ZSTAC-81270,
 ZSTAC-70101, ZSTAC-72034, ZSTAC-73197,
 ZSTAC-79921, ZSTAC-81160, ZSTAC-81224,
 ZSTAC-81805, ZSTAC-72304, ZSTAC-81804,
 ZSTAC-74898, ZSTAC-69215, ZSTAC-70151,
 ZSTAC-68933

Change-Id: I910e9b542ecd254fdf7e956f943316988a56a1f9
@coderabbitai
Copy link

coderabbitai bot commented Feb 20, 2026

总览

该拉取请求在多个 Java 文件中引入了防御性空指针安全检查,包括空值参数验证、空值条件判断和空值回退处理。这些更改通过提前检测和处理潜在的 null 值来增强代码的稳健性,防止 NullPointerException 异常。

变更

变更组 / 文件 摘要
主机分配器与连接
compute/src/main/java/org/zstack/compute/allocator/HostAllocatorChain.java, compute/src/main/java/org/zstack/compute/host/HostBase.java, plugin/ceph/src/main/java/org/zstack/storage/ceph/primary/CephPrimaryStorageMonBase.java
在异常处理和资源标识符生成中添加了空值守卫,确保在完成对象或 self 引用为 null 时安全地返回默认值,而不是触发异常。
存储与虚拟机功能
compute/src/main/java/org/zstack/compute/vm/VmCapabilitiesJudger.java, plugin/ceph/src/main/java/org/zstack/storage/ceph/primary/CephPrimaryStorageBase.java, plugin/localstorage/src/main/java/org/zstack/storage/primary/local/LocalStorageKvmBackend.java
在获取存储类型、监控节点信息和错误代码详情时添加了空值检查,防止空指针访问。
数据库与序列化
core/src/main/java/org/zstack/core/db/UpdateQueryImpl.java, core/src/main/java/org/zstack/core/log/LogSafeGson.java
改进了错误消息生成和 JSON 序列化的空值处理,避免在空对象上调用方法。
身份与生命周期管理
identity/src/main/java/org/zstack/identity/AuthorizationManager.java, header/src/main/java/org/zstack/header/vm/VmAbnormalLifeCycleStruct.java, plugin/localstorage/src/main/java/org/zstack/storage/primary/local/LocalStorageAllocatorFactory.java
为会话评估、主机 UUID 比较和协议解析器查询添加了空值验证,确保后续操作的输入有效性。

代码审查工作量估计

🎯 2 (Simple) | ⏱️ ~12 分钟

诗歌

🐰 空值守卫护周全,
异常消弭于无形,
防御十处皆稳妥,
代码固若金汤城!
小兔欢呼新安全!

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed 标题遵循指定的格式 [scope]: ,长度为44字符,未超过72字符限制,准确描述了PR的主要改动。
Description check ✅ Passed 描述与变更集相关,详细列举了修复的NPE问题、受影响的模块和测试情况,与代码改动相符。

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch sync/ye.zou/fix/npe-batch-quality-issues@@2

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ast-grep (0.40.5)
plugin/ceph/src/main/java/org/zstack/storage/ceph/primary/CephPrimaryStorageBase.java

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
plugin/localstorage/src/main/java/org/zstack/storage/primary/local/LocalStorageAllocatorFactory.java (1)

388-393: 空指针防护逻辑正确,但错误码复用可能导致排查困难。

这里对 parser 的空检查是正确的防御性编程实践。但 ORG_ZSTACK_STORAGE_PRIMARY_LOCAL_10023 错误码在第 386 行已用于 "invalid uri" 场景,现在又用于 "unsupported protocol" 场景。两种不同的错误原因共用同一错误码,在日志排查时可能造成混淆。

建议为"不支持的协议"场景分配独立的错误码,以便于问题定位。

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@plugin/localstorage/src/main/java/org/zstack/storage/primary/local/LocalStorageAllocatorFactory.java`
around lines 388 - 393, The code in LocalStorageAllocatorFactory checks parser
== null correctly but reuses ORG_ZSTACK_STORAGE_PRIMARY_LOCAL_10023 (already
used for "invalid uri"), which makes diagnostics confusing; define a new
distinct error code constant (e.g. ORG_ZSTACK_STORAGE_PRIMARY_LOCAL_10024 or
similarly named) for the "unsupported protocol" case and replace the thrown
OperationFailureException that currently references
ORG_ZSTACK_STORAGE_PRIMARY_LOCAL_10023 with the new constant while keeping the
same argerr message and the use of parser.parseUri(...).hostUuid to preserve
behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@compute/src/main/java/org/zstack/compute/allocator/HostAllocatorChain.java`:
- Around line 149-152: The catch block in HostAllocatorChain swallows exceptions
when completion is null (dryRun), causing callers to hang; update the handler so
that if completion != null it calls
completion.fail(inerr(ORG_ZSTACK_COMPUTE_ALLOCATOR_10019, errMsg)), but if
completion == null (dryRun) rethrow a runtime exception (e.g., new
CloudRuntimeException or similar) created with
inerr(ORG_ZSTACK_COMPUTE_ALLOCATOR_10019, errMsg) or wrap the original Throwable
as the cause, ensuring the error surfaces to the caller instead of being
swallowed.

---

Nitpick comments:
In
`@plugin/localstorage/src/main/java/org/zstack/storage/primary/local/LocalStorageAllocatorFactory.java`:
- Around line 388-393: The code in LocalStorageAllocatorFactory checks parser ==
null correctly but reuses ORG_ZSTACK_STORAGE_PRIMARY_LOCAL_10023 (already used
for "invalid uri"), which makes diagnostics confusing; define a new distinct
error code constant (e.g. ORG_ZSTACK_STORAGE_PRIMARY_LOCAL_10024 or similarly
named) for the "unsupported protocol" case and replace the thrown
OperationFailureException that currently references
ORG_ZSTACK_STORAGE_PRIMARY_LOCAL_10023 with the new constant while keeping the
same argerr message and the use of parser.parseUri(...).hostUuid to preserve
behavior.

Comment on lines +149 to +152
String errMsg = t != null ? t.toString() : "unknown error";
if (completion != null) {
completion.fail(inerr(ORG_ZSTACK_COMPUTE_ALLOCATOR_10019, errMsg));
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

dryRun 异常时未回调,可能导致调用方挂起。

dryRun() 场景下 completion 为空,当前 catch 只检查 completion,会吞掉异常导致调用方永远收不到失败回调。

🛠️ 建议修复
-            String errMsg = t != null ? t.toString() : "unknown error";
-            if (completion != null) {
-                completion.fail(inerr(ORG_ZSTACK_COMPUTE_ALLOCATOR_10019, errMsg));
-            }
+            String errMsg = t != null ? t.toString() : "unknown error";
+            if (isDryRun) {
+                if (dryRunCompletion != null) {
+                    dryRunCompletion.fail(inerr(ORG_ZSTACK_COMPUTE_ALLOCATOR_10019, errMsg));
+                }
+            } else if (completion != null) {
+                completion.fail(inerr(ORG_ZSTACK_COMPUTE_ALLOCATOR_10019, errMsg));
+            }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@compute/src/main/java/org/zstack/compute/allocator/HostAllocatorChain.java`
around lines 149 - 152, The catch block in HostAllocatorChain swallows
exceptions when completion is null (dryRun), causing callers to hang; update the
handler so that if completion != null it calls
completion.fail(inerr(ORG_ZSTACK_COMPUTE_ALLOCATOR_10019, errMsg)), but if
completion == null (dryRun) rethrow a runtime exception (e.g., new
CloudRuntimeException or similar) created with
inerr(ORG_ZSTACK_COMPUTE_ALLOCATOR_10019, errMsg) or wrap the original Throwable
as the cause, ensuring the error surfaces to the caller instead of being
swallowed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants