Skip to content
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

iterate-over-empty-map warning on map initialised at the declaration #36

Open
NitriKx opened this issue Aug 31, 2023 · 4 comments
Open

Comments

@NitriKx
Copy link

NitriKx commented Aug 31, 2023

Hello,

I'm struggling to understand why the following code function has a iterate-over-empty-map:

func iter2(){
    m := map[string]int {
        "toto": 1
    }

    fmt.Println("iterating")
    for v := range m {
        fmt.Println("map item: ", v)
    }
}

Could you help me with that? 🙏
cc @Caomoji

@GrosQuildu
Copy link
Collaborator

Interesting. Looks like Semgrep coerces map[$T1]$T2{} and make(map[$T1]$T2).
Probably a Semgrep bug? Simple repro is that both queries below matches all three code lines:

- pattern: |
    $C = map[$T1] $T2{...}
- pattern: |
    $C = make(map[$T1] $T2, ...)
func main() {
	m := make(map[string]int)
	m2 := map[string]int{}
	m3 := map[string]int{"a":1}
}

Will report that upstream, unless @Vasco-jofra has some thoughts here.

@GrosQuildu
Copy link
Collaborator

semgrep/semgrep#8980

@GrosQuildu
Copy link
Collaborator

Waiting for upstream fix. More tests for the future:

func iter1_2(){
       // ruleid: iterate-over-empty-map
       m := make(map[string]int, 5)
       for v := range m {
               fmt.Println("map item: ", v)
       }
}

func iter1_3(){
       // ruleid: iterate-over-empty-map
       m := map[string]int{}
       for v := range m {
               fmt.Println("map item: ", v)
       }
}


func iter1_FP_5(){
       // ok: iterate-over-empty-map
       m := make(map[string]int, 1)

       m["v1"]--

       for v := range m {
               fmt.Println("map item: ", v)
       }
}

func iter1_FP_6(){
       // ok: iterate-over-empty-map
       m := map[string]int{"test": 1}
       for v := range m {
               fmt.Println("map item: ", v)
       }
}

@mschwager
Copy link
Member

mschwager commented Jun 6, 2024

I just ran into this too. I wrote a test here so we'll know when this is fixed upstream: #62

The above issue is very similar, or a dupe of semgrep/semgrep#9558

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants