fix: orm/*, store/*: fix non-determinism from map iteration (#12751)

Reported by informalsystems/gosec's map iteration pass, this
change fixes non-determinism from iterating with a map, whose
definition in the Go spec guarantees that the order of keys
and values presented during iteration will be randomized.

Fixes #12428
Fixes #12430
Fixes #12431
This commit is contained in:
Emmanuel T Odeke
2022-07-27 21:10:32 +02:00
committed by GitHub
parent 37e765daa6
commit 2f4faa58c1
3 changed files with 30 additions and 3 deletions
+11 -1
View File
@@ -15,7 +15,17 @@ import (
)
func (m moduleDB) DefaultJSON(target ormjson.WriteTarget) error {
for name, table := range m.tablesByName {
tableNames := make([]protoreflect.FullName, 0, len(m.tablesByName))
for name := range m.tablesByName {
tableNames = append(tableNames, name)
}
sort.Slice(tableNames, func(i, j int) bool {
ti, tj := tableNames[i], tableNames[j]
return ti.Name() < tj.Name()
})
for _, name := range tableNames {
table := m.tablesByName[name]
w, err := target.OpenWriter(name)
if err != nil {
return err