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

rules/sdk: implement pass to only permit key iteration over maps, not value #5

Merged
merged 1 commit into from
Nov 15, 2021

Commits on Nov 15, 2021

  1. rules/sdk: implement pass to only permit key iteration over maps, not…

    … value
    
    This pass exists to curtail non-determinism in the cosmos-sdk
    which stemmed from iterating over maps during upgrades and that
    caused a chaotic debug for weeks. With this change, we'll now
    enforce and report failed iterations, with the rule being that
    a map in a range should involve ONLY one of these 2 operations:
    * for k := range m { delete(m, k) } for fast map clearing
    * for k := range m { keys = append(keys, k) } to retrieve keys & sort
    
    thus we shall get this report:
    ```shell
    [gosec] 2021/11/09 03:18:57 Rule error: *sdk.mapRanging => the value in the range statement should be nil: want: for key := range m (main.go:19)
    [gosec] 2021/11/09 03:18:57 Rule error: *sdk.mapRanging => the value in the range statement should be nil: want: for key := range m (main.go:27)
    ```
    
    from the code below:
    
    ```go
    package main
    
    func main() {
    	m := map[string]int{
    		"a": 0,
    		"b": 1,
    		"c": 2,
    		"d": 3,
    	}
    
    	makeMap := func() map[string]string { return nil }
    
    	keys := make([]string, 0, len(m))
    	for k := range m {
    		keys = append(keys, k)
    	}
    
    	values := make([]int, 0, len(m))
    	for _, value := range m {
    		values = append(values, value)
    	}
    
    	type kv struct {
    		k, v interface{}
    	}
    	kvL := make([]*kv, 0, len(m))
    	for k, v := range m {
    		kvL = append(kvL, &kv{k, v})
    	}
    
    	for k := range m {
    		delete(m, k)
    	}
    
    	for k := range makeMap() {
    		delete(m, k)
    	}
    
    	for k := range do() {
    		delete(m, k)
    	}
    }
    
    func do() map[string]string { return nil }
    ```
    
    Updates cosmos/cosmos-sdk#10189
    Updates cosmos/cosmos-sdk#10188
    Updates cosmos/cosmos-sdk#10190
    odeke-em committed Nov 15, 2021
    Configuration menu
    Copy the full SHA
    8577495 View commit details
    Browse the repository at this point in the history