Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

False Negative: ContainerSizeCmpZero.ql misses impossible size checks once `length()` or `size()` is copied into locals.

未关闭
#21,538 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
48/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
冷清
技术栈
java
领域
devtools, security

调研方向

从 Likely Bugs/Likely Typos/ContainerSizeCmpZero.ql 开始,检查它如何识别直接的容器大小比较。使用指定的 PosCase1_Var1.java 到 PosCase4_Var5.java 示例复现该问题。当 checker 能够标记列出的始终为真和始终为假的比较时即视为完成,即使 size 或 length 如示例所示被复制、使用别名或包装。

由索引模型根据 Issue 内容生成。

描述

Version
codeql 2.24.3

Checker

  • Checker id: Likely Bugs/Likely Typos/ContainerSizeCmpZero.ql
  • Checker description: This checker detects comparisons of container size (array length, string length, collection size, or map size) to zero that are always true or always false because container sizes cannot be negative.

Description of the false negative

All of these cases are still impossible or tautological comparisons on container sizes. The only change is that the result of length() or size() is first copied into a local variable, or zero is produced by a tiny helper before the comparison happens.

That does not change the fact that container sizes are non-negative.

Affected test cases

PosCase1_Var1.java

len < 0 is still always false. Pulling arr.length into a local should not hide that.

// Comparing array length to 0 with less-than should be flagged as always false.
package scensct.var.pos;

import java.util.*;

public class PosCase1_Var1 {
    public static void main(String[] args) {
        int[] arr = new int[5];
        // Use a temporary variable for length
        int len = arr.length;
        if (len < 0) { // Always false
            System.out.println("Unreachable");
        }
    }
}
PosCase2_Var1.java

count >= 0 is always true for a collection size. The extra local does not make it meaningful.

// Comparing collection size to 0 with greater-than-or-equal should be flagged as always true.
package scensct.var.pos;

import java.util.*;

public class PosCase2_Var1 {
    public static void main(String[] args) {
        // Variant 1: Use a temporary variable and rename collection
        Collection<Integer> items = new HashSet<>();
        int count = items.size();
        if (count >= 0) { // Always true
            System.out.println("Always true");
        }
    }
}
PosCase2_Var5.java

The alias and ternary operator add noise, but size >= 0 is still a tautology.

// Comparing collection size to 0 with greater-than-or-equal should be flagged as always true.
package scensct.var.pos;

import java.util.*;

public class PosCase2_Var5 {
    public static void main(String[] args) {
        // Variant 5: Introduce aliasing and ternary operator
        List<Double> original = new LinkedList<>();
        List<Double> alias = original;
        int size = alias.size();
        String result = size >= 0 ? "Always true" : "Never printed";
        System.out.println(result);
    }
}
PosCase3_Var1.java

0 > length is just another spelling of an always-false negative-size check.

// Comparing integer literal 0 to string length with greater-than should be flagged as always false.
package scensct.var.pos;

import java.util.*;

public class PosCase3_Var1 {
    public static void main(String[] args) {
        String text = "test";
        int length = text.length();
        // Using a temporary variable for the comparison
        if (0 > length) {
            System.out.println("Unreachable");
        }
    }
}
PosCase4_Var1.java

zero <= dataMap.size() is always true. The boolean temporary only hides the same impossible check.

// Comparing integer literal 0 to map size with less-than-or-equal should be flagged as always true.
package scensct.var.pos;

import java.util.*;

public class PosCase4_Var1 {
    public static void main(String[] args) {
        // Variant 1: Lexical refactoring - rename map and use explicit type
        HashMap<Integer, String> dataMap = new HashMap<>();
        int zero = 0;
        boolean condition = zero <= dataMap.size();
        if (condition) {
            System.out.println("Always true");
        }
    }
}
PosCase4_Var5.java

The helper getZero() does not change the comparison. 0 <= map.size() remains always true.

// Comparing integer literal 0 to map size with less-than-or-equal should be flagged as always true.
package scensct.var.pos;

import java.util.*;

public class PosCase4_Var5 {
    // Variant 5: Complex expression with method call
    private static int getZero() {
        return 0;
    }
    
    public static void main(String[] args) {
        Map<Integer, String> map = new LinkedHashMap<>();
        // Use method call for zero and nested comparison
        if (getZero() <= map.size() && true) {
            System.out.println("Always true");
        }
    }
}

Cause analysis

The issue here is not ambiguity; it is loss of simple numeric reasoning once size() or length() is one step removed from the comparison. Likely Bugs/Likely Typos/ContainerSizeCmpZero.ql appears to require a very direct AST shape and stops recognizing the same always-true or always-false condition when a local or helper is introduced.

That is narrower than developers expect. These are still straightforward container-size sanity bugs.

References

None known.

主要语言
CodeQL
星标
10.1k
派生
2.1k
平均合并
2 天 16 小时
30 天内合并 PR
143

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

github/codeql 的其他 Issue

查看 github/codeql 的全部 Issue

相似的 Issue

更多 DevTools Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。