Skip to content

Fix NPE problem when update instance metadata with null value. #13629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

KomachiSion
Copy link
Collaborator

Please do not create a Pull Request without creating an issue first.

What is the purpose of the change

XXXXX

Brief changelog

XX

Verifying this change

XXXX

Follow this checklist to help us incorporate your contribution quickly and easily:

  • Make sure there is a Github issue filed for the change (usually before you start working on it). Trivial changes like typos do not require a Github issue. Your pull request should address just this issue, without pulling in other changes - one PR resolves one issue.
  • Format the pull request title like [ISSUE #123] Fix UnknownException when host config not exist. Each commit in the pull request should have a meaningful subject line and body.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Write necessary unit-test to verify your logic correction, more mock a little better when cross module dependency exist. If the new feature or significant change is committed, please remember to add integration-test in test module.
  • Run mvn -B clean package apache-rat:check findbugs:findbugs -Dmaven.test.skip=true to make sure basic checks pass. Run mvn clean install -DskipITs to make sure unit-test pass. Run mvn clean test-compile failsafe:integration-test to make sure integration-test pass.

@KomachiSion KomachiSion added this to the 2.5.2 milestone Jul 22, 2025
@KomachiSion KomachiSion added kind/bug Category issues or prs related to bug. area/Naming labels Jul 22, 2025
Copy link

lingma-agents bot commented Jul 22, 2025

修复更新实例元数据时null值导致的NPE问题

变更概述

问题修复

  • 组件InstanceOperatorClientImpl
  • 修复内容:在构建实例元数据时,新增filterNullValue方法过滤元数据中值为null的键值对。
    • 修改buildMetadata方法,将instance.getMetadata()替换为过滤后的结果,避免后续操作中因null值引发NPE
    • 技术细节:使用Java Stream的filter操作过滤entry.getValue() != null的条目,通过Collectors.toMap生成新Map
  • 目的:确保元数据存储时不会包含null值,提升服务实例配置的健壮性。

测试更新

  • 组件InstanceOperatorClientImplTest
  • 新增测试testUpdateInstanceWithNullValue验证元数据包含null值时的处理逻辑。
    • 模拟实例元数据中包含null值的场景,验证更新后extendData字段为空。
    • 使用Mockito的argThat断言参数中的extendData是否被正确过滤。
    • 目的:覆盖边界条件测试,确保修复方案有效性。
变更文件
文件路径 变更说明
naming/​src/​main/​java/​com/​alibaba/​nacos/​naming/​core/​InstanceOperatorClientImpl.​java 在元数据构建时过滤null值,避免NPE问题
naming/​src/​test/​java/​com/​alibaba/​nacos/​naming/​core/​InstanceOperatorClientImplTest.​java 新增处理null值的测试用例

💡 小贴士

与 lingma-agents 交流的方式

📜 直接回复评论
直接回复本条评论,lingma-agents 将自动处理您的请求。例如:

  • 在当前代码中添加详细的注释说明。

  • 请详细介绍一下你说的 LRU 改造方案,并使用伪代码加以说明。

📜 在代码行处标记
在文件的特定位置创建评论并 @lingma-agents。例如:

  • @lingma-agents 分析这个方法的性能瓶颈并提供优化建议。

  • @lingma-agents 对这个方法生成优化代码。

📜 在讨论中提问
在任何讨论中 @lingma-agents 来获取帮助。例如:

  • @lingma-agents 请总结上述讨论并提出解决方案。

  • @lingma-agents 请根据讨论内容生成优化代码。

Copy link

@lingma-agents lingma-agents bot left a comment

Choose a reason for hiding this comment

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

🔎 代码评审报告

🎯 评审意见概览
严重度 数量 说明
🔴 Blocker 0 阻断性问题,需立即修复。例如:系统崩溃、关键功能不可用或严重安全漏洞。
🟠 Critical 1 严重问题,高优先级修复。例如:核心功能异常或性能瓶颈影响用户体验。
🟡 Major 0 主要问题,建议修复。例如:非核心功能缺陷或代码维护性较差。
🟢 Minor 1 次要问题,酬情优化。例如:代码格式不规范或注释缺失。

总计: 2 个问题

📋 评审意见详情
💡 代码实现建议
以下是文件级别的代码建议,聚焦于代码的可读性、可维护性和潜在问题。
☕ naming/src/main/java/com/alibaba/nacos/naming/core/InstanceOperatorClientImpl.java (1 💬)
☕ naming/src/test/java/com/alibaba/nacos/naming/core/InstanceOperatorClientImplTest.java (1 💬)
🚀 架构设计建议
以下是对代码架构和设计的综合分析,聚焦于跨文件交互、系统一致性和潜在优化空间。
🔍1. 新增的filterNullValue方法未处理metadata为null的情况可能导致其他调用点NPE

在InstanceOperatorClientImpl的filterNullValue方法中,未处理传入的metadata参数本身为null的情况。该方法直接调用metadata.entrySet(),若传入的metadata为null会导致NPE。虽然当前调用点buildMetadata方法在调用时确保了通过instance.getMetadata()获取的metadata不为null(因为Instance类可能有默认构造的metadata),但其他调用方可能直接传递外部传入的metadata参数,存在潜在风险。此问题需要从系统层面确保所有调用filterNullValue方法的位置都已进行null检查,或在方法内部添加null处理逻辑。

📌 关键代码

private Map<String, String> filterNullValue(Map<String, String> metadata) {
    return metadata.entrySet().stream().filter(entry -> entry.getValue() != null)
            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

⚠️ 潜在风险

若其他组件在调用该方法时传入null的metadata参数,将直接导致NPE,破坏系统稳定性。需要系统性检查所有调用filterNullValue的路径,或在方法内部添加null安全处理逻辑(如返回空map或抛出明确异常)。

🔍2. 测试用例未覆盖metadata整体为null的场景

新增的测试用例testUpdateInstanceWithNullValue仅测试了metadata中某个键的值为null的情况,但未模拟metadata对象本身为null的场景。根据现有代码逻辑,当instance.getMetadata()返回null时,调用result.getExtendData().putAll()会导致NPE。虽然当前Instance类可能保证metadata不为null,但若后续代码修改或外部传入实例时,可能引入此类风险。需要补充metadata为null的测试场景以确保全面覆盖。

📌 关键代码

instance.getMetadata().put("nullValue", null);
instanceOperatorClient.updateInstance("A", Constants.DEFAULT_GROUP, "C", instance);

⚠️ 潜在风险

若外部传入的instance对象metadata字段为null时,系统将抛出NPE,但当前测试未覆盖该场景,可能遗漏真实环境中的异常情况。

🔍3. 跨文件的metadata null安全处理不一致

在InstanceOperatorClientImpl的buildMetadata方法中,通过filterNullValue处理metadata的null值,但未处理metadata本身的null情况。而在InstanceOperatorClientImplTest的测试用例中,直接调用instance.getMetadata().put(),假设metadata不为null。这种不一致性可能导致其他使用metadata的组件(如metadataOperateService)在未进行null检查的情况下处理空metadata对象,引发隐藏的NPE风险。需要确保整个系统对metadata的null安全处理有统一的契约。

📌 关键代码

result.getExtendData().putAll(filterNullValue(instance.getMetadata()));

⚠️ 潜在风险

其他未遵循null安全处理的组件可能因metadata为null而崩溃,需要在整个系统中建立metadata的null处理规范,并在关键路径添加防御性检查。

审查详情
📒 文件清单 (2 个文件)
📝 变更: 2 个文件

📝 变更文件:

  • naming/src/main/java/com/alibaba/nacos/naming/core/InstanceOperatorClientImpl.java
  • naming/src/test/java/com/alibaba/nacos/naming/core/InstanceOperatorClientImplTest.java

💡 小贴士

与 lingma-agents 交流的方式

📜 直接回复评论
直接回复本条评论,lingma-agents 将自动处理您的请求。例如:

  • 在当前代码中添加详细的注释说明。

  • 请详细介绍一下你说的 LRU 改造方案,并使用伪代码加以说明。

📜 在代码行处标记
在文件的特定位置创建评论并 @lingma-agents。例如:

  • @lingma-agents 分析这个方法的性能瓶颈并提供优化建议。

  • @lingma-agents 对这个方法生成优化代码。

📜 在讨论中提问
在任何讨论中 @lingma-agents 来获取帮助。例如:

  • @lingma-agents 请总结上述讨论并提出解决方案。

  • @lingma-agents 请根据讨论内容生成优化代码。

Comment on lines +147 to +150
private Map<String, String> filterNullValue(Map<String, String> metadata) {
return metadata.entrySet().stream().filter(entry -> entry.getValue() != null)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
Copy link

Choose a reason for hiding this comment

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

在filterNullValue方法中未处理传入的metadata为null的情况,可能导致NPE。

🟠 Critical | 🐞 Bugs

📋 问题详情

当传入的metadata参数为null时,调用metadata.entrySet()会抛出NullPointerException。需要在方法内部添加对null的检查,返回一个空Map以避免异常。

💡 解决方案

在filterNullValue方法中添加对metadata为null的检查,返回空Map以避免NPE:

private Map<String, String> filterNullValue(Map<String, String> metadata) {
+    if (metadata == null) {
+        return Collections.emptyMap();
+    }
     return metadata.entrySet().stream().filter(entry -> entry.getValue() != null)
             .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

您的反馈对我们很重要!(建议右键在新标签页中打开以下链接)

有用意见👍无用意见👎错误意见❌

@Test
void testUpdateInstanceWithNullValue() throws NacosException {
Instance instance = new Instance();
instance.setServiceName("C");
Copy link

Choose a reason for hiding this comment

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

测试用例testUpdateInstanceWithNullValue未初始化instance的metadata,可能导致NPE。

🟢 Minor | 🧹 Code Smells

📋 问题详情

在testUpdateInstanceWithNullValue测试中,直接调用instance.getMetadata().put(...)可能导致NullPointerException,因为instance.getMetadata()可能返回null。需要先初始化metadata为一个空的HashMap。

💡 解决方案

在测试用例中初始化metadata以避免NPE:

instance = new Instance();
+   instance.setMetadata(new HashMap<>());
     instance.getMetadata().put("nullValue", null);

您的反馈对我们很重要!(建议右键在新标签页中打开以下链接)

有用意见👍无用意见👎错误意见❌

Copy link

Thanks for your this PR. 🙏
Please check again for your PR changes whether contains any usage/api/configuration change such as Add new API , Add new configuration, Change default value of configuration.
If so, please add or update documents(markdown type) in docs/next/ for repository nacos-group/nacos-group.github.io


感谢您提交的PR。 🙏
请再次查看您的PR内容,确认是否包含任何使用方式/API/配置参数的变更,如:新增API新增配置参数修改默认配置等操作。
如果是,请确保在提交之前,在仓库nacos-group/nacos-group.github.io中的docs/next/目录下添加或更新文档(markdown格式)。

@KomachiSion KomachiSion merged commit bab1c9f into alibaba:v2.x-develop Jul 22, 2025
3 checks passed
@KomachiSion KomachiSion deleted the v2.x-develop-fix-update-metadata-with-null-value branch July 22, 2025 11:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/Naming kind/bug Category issues or prs related to bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant