Commit 1e62329d by zhouhaibao

[+]filterStyjMerchant 方法返回值逻辑混乱处理,潜在的运行时异常:List.addAll 操作风险

1 parent 1aed88ec
...@@ -989,17 +989,16 @@ public class LoginService { ...@@ -989,17 +989,16 @@ public class LoginService {
} else { } else {
if (CollectionUtils.isNotEmpty(backupStoreIdList)) { if (CollectionUtils.isNotEmpty(backupStoreIdList)) {
List<Integer> excludeIds = request.getExcludeIds();
List<Integer> originExcludeIds = request.getExcludeIds();
// 如果为 null,初始化为一个新列表;否则使用原列表 // 如果为 null,初始化为一个新列表;否则使用原列表
if (excludeIds == null) { List<Integer> newExcludeIds = originExcludeIds == null ? new ArrayList<>() : new ArrayList<>(originExcludeIds);
excludeIds = new ArrayList<>();
}
// 直接追加,无需再判断 backupStoreIdList 是否为空(addAll 内部会处理空集合,但通常建议确保 backupStoreIdList 不为 null) // 直接追加,无需再判断 backupStoreIdList 是否为空(addAll 内部会处理空集合,但通常建议确保 backupStoreIdList 不为 null)
excludeIds.addAll(backupStoreIdList); newExcludeIds.addAll(backupStoreIdList);
request.setExcludeIds(excludeIds); request.setExcludeIds(newExcludeIds);
} }
return merchantIdList; return merchantIdList;
...@@ -1062,9 +1061,9 @@ public class LoginService { ...@@ -1062,9 +1061,9 @@ public class LoginService {
.filter(warningStoreSet::contains) .filter(warningStoreSet::contains)
.collect(Collectors.toList()); .collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(collected)){ if (CollectionUtils.isNotEmpty(collected)){
return NO_PERMISSION_LIST;
}else{
return collected; return collected;
}else{
return NO_PERMISSION_LIST;
} }
}else{ }else{
...@@ -1073,17 +1072,15 @@ public class LoginService { ...@@ -1073,17 +1072,15 @@ public class LoginService {
} else { } else {
if (CollectionUtils.isNotEmpty(warningStoreList)) { if (CollectionUtils.isNotEmpty(warningStoreList)) {
List<Integer> excludeIds = req.getExcludeIds(); List<Integer> originExcludeIds = req.getExcludeIds();
// 如果为 null,初始化为一个新列表;否则使用原列表 // 如果为 null,初始化为一个新列表;否则使用原列表
if (excludeIds == null) { List<Integer> newExcludeIds = originExcludeIds == null ? new ArrayList<>() : new ArrayList<>(originExcludeIds);
excludeIds = new ArrayList<>();
}
// 直接追加,无需再判断 backupStoreIdList 是否为空(addAll 内部会处理空集合,但通常建议确保 backupStoreIdList 不为 null) // 直接追加,无需再判断 backupStoreIdList 是否为空(addAll 内部会处理空集合,但通常建议确保 backupStoreIdList 不为 null)
excludeIds.addAll(warningStoreList); newExcludeIds.addAll(warningStoreList);
req.setExcludeIds(excludeIds); req.setExcludeIds(newExcludeIds);
} }
return merchantIdList; return merchantIdList;
......
  • @wangxw1
    commented
    Master

    你好,我是资深的 Java 开发工程师。针对你提供的 LoginService.java 代码变更,我进行了详细的 Code Review。

    本次变更主要涉及 List 操作的安全性增强(防御性拷贝)以及一段业务逻辑的调整。虽然修复了潜在的 UnsupportedOperationException 问题,但引入了严重的逻辑风险和空指针隐患。

    以下是审查结果:

    一、关键问题与优化建议(Top 3)

    1. 业务逻辑严重反转与潜在 Bug(高风险)

    • 位置:Line 1062-1066
    • 问题:代码将原本的 if-else 返回值逻辑进行了完全对调。
      • 原逻辑:若 collected(命中警告集合的列表)非空,返回 NO_PERMISSION_LIST;否则返回空列表。这通常表示“发现违规/警告则拒绝”。
      • 新逻辑:若 collected 非空,返回 collected;若为空(即未命中警告),反而返回 NO_PERMISSION_LIST。
      • 风险:这意味着“如果没有命中警告,就没有权限”。这通常违背常规的安全放行逻辑(即“无警告应放行”)。除非业务需求发生了颠覆性改变,否则这是一个严重的逻辑 Bug。
    • 建议:请再次确认业务需求。如果本意是“存在警告店铺时拦截”,原逻辑可能是对的(或者部分对),新逻辑极有可能是错误的。

    2. 误导性的注释与空指针异常(NPE)隐患(中高风险)

    • 位置:Line 995-996, 1078-1079
    • 问题:注释中写道 // 直接追加,无需再判断 backupStoreIdList 是否为空(addAll 内部会处理空集合...)。这是一个错误的认知。
      • ArrayList.addAll(Collection c) 方法在传入 null 时会直接抛出 NullPointerException。它只能处理“空集合”(Empty Collection),不能处理 null 对象。
      • 虽然外层有 if (CollectionUtils.isNotEmpty(backupStoreIdList)) 判断,但在代码维护中,依赖外层判断而写出“误导性注释”是危险的。
    • 建议:修正注释,或者在 addAll 之前使用 org.apache.commons.collections4.CollectionUtils.emptyIfNull() 等工具方法包裹参数,确保绝对安全。

    3. 代码重复与违反 DRY 原则(可维护性)

    • 位置:Line 989-999 与 Line 1073-1081
    • 问题:针对 excludeIds 的“获取 -> 防御性拷贝 -> 添加元素 -> 回写”这一逻辑块在代码中完全重复出现了两次。
    • 建议:提取一个私有工具方法,例如 private void addExcludeIdsSafe(Request req, List<Integer> idsToAdd)。这不仅减少了重复代码,还能统一处理上述的 NPE 检查逻辑,提高代码整洁度。

    二、评分明细

    维度 评分 扣分理由
    功能正确性 15/40 1062行逻辑反转极大概率导致业务阻断;addAll 的注释存在技术性误导,虽有外层判空但在重构时易引发 NPE。
    安全性 20/30 修复了 Arrays.asList 等不可变列表调用 addAll 导致的崩溃问题(防御性拷贝是好的),但业务逻辑的反转引入了由于逻辑错误导致的安全(权限)风险。
    可维护性 12/20 存在明显的代码重复(Copy-Paste);关键逻辑变更缺乏详细注释解释为何反转逻辑。
    性能 5/5 创建新的 ArrayList 进行拷贝带来的性能开销微乎其微,属于合理操作。
    提交信息 4/5 提交信息清晰指出了 addAll 风险和逻辑处理,但未能解释为何要反转逻辑(Logic inversion),导致审查者困惑。

    总分:56分

Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!