False Negative: ContainerSizeCmpZero.ql misses impossible size checks once `length()` or `size()` is copied into locals.
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Aptitud para principiantes
- 48/100
Línea de trabajo
Comienza con Likely Bugs/Likely Typos/ContainerSizeCmpZero.ql e inspecciona cómo reconoce las comparaciones directas del tamaño de los contenedores. Reproduce el problema con los ejemplos indicados PosCase1_Var1.java a PosCase4_Var5.java. La tarea está terminada cuando el checker marque las comparaciones indicadas que siempre son verdaderas y las que siempre son falsas, incluso cuando size o length se copian, se usan como alias o se envuelven como se muestra.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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.
- Lenguaje dominante
- CodeQL
- Estrellas
- 10.1k
- Forks
- 2.1k
- Merge medio
- 2 d 16 h
- PR fusionados (30 d)
- 143
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de github/codeql
-
agentic-workflows
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
-
false-positive javascript
Dificultad 2/5 1-3 horas Aptitud para principiantes 84/100
-
C#: cs/simplifiable-boolean-expression false positive on Nullable<bool> compared with a literal Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
-
false-positive
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
Todos los issues de github/codeql
Issues similares
-
ZCode 3.14.3 に対応する Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
supermomonga/zcode-acp#24 ·
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 90/100
learningequality/ricecooker#747 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 84/100
KhronosGroup/glTF-Blender-IO#2769 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 88/100