Skip to content

Infer incorrectly reports NULL_DEREFERENCE for HashMap operations #1933

@clearlove2077

Description

@clearlove2077

Hi, Infer frequently reports false warnings about NULL_DEREFERENCE in common HashMap usage patterns. Below is a minimized code example. At line 7, there is no actual null dereference bug, yet Infer generates a warning as shown in the log below. This appears to be a false positive.

Minimized Code Example

import java.util.*;

class Main {
    void check(HashMap<String, String> map) {
        for (String key : map.keySet()) {
            String s = map.get(key);
            if (s.equals("1")) { // report a false warning at this line
                // ...
            } else {
                // ...
            }
        }
    }

    public static void main(String[] args) {
        Main m = new Main();
        HashMap<String, String> map = new HashMap<>();
        map.put("1", "2");
        m.check(map);
    }
}

Log

Main.java:7: error: Null Dereference
  `s` could be null (null value originating from line 6) and is dereferenced. 
  5.           for (String key : map.keySet()) {
  6.               String s = map.get(key);
  7. >             if (s.equals("1")) {
  8.                   // ...
  9.               } else {

Infer version: v1.2.0

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions