Nil pointer panic trying to retrieve value from a map[string]struct{}

Hi :smile:!

I’m receiving a runtime error: invalid memory address or nil pointer dereference accessing to a nil map:

var orgsToSendToPrometheus map[string]struct{}

func MustRegisterOverheadByOrg(org string) bool {
	if _, ok := orgsToSendToPrometheus[allOrgs]; ok {
		return true
	}
	_, ok := orgsToSendToPrometheus[org]
	return ok
}

That panics in line → _, ok := orgsToSendToPrometheus[org]

As I understand, read , delete, length, and range loops, are safe to perform on a nil map since it behaves like an empty map.

Could anyone help me?

Thanks!
Miki

Hello there. Can you provide error trace? And can you also specify where org comes from? For now, I don’t see any problems with accessing nil map: playground

I am not sure if the problem is with “allOrgs” key…

You told it panics with org key

See also: The Go Programming Language Specification - The Go Programming Language

“The value of an uninitialized map is nil.”

" A new, empty map value is made using the built-in function make, which takes the map type and …"

Hi all!

Thank you for your responses.

org parameter is a string and could not be nil. Zero value is empty string. It always have a value != “” because I check it before calling MustRegisterOverheadByOrg.

allOrgs parameter is a string too with “all” value initialized at package level.

Here you have all code in the file :arrow_down:

image

The error trace:

gitlab.xmltravelgate.com/hub-aggregator/kit/log.(*Logger).Error
/go/pkg/mod/gitlab.xmltravelgate.com/hub-aggregator/kit@v1.46.22/log/logger.go:68
gitlab.xmltravelgate.com/hub-aggregator/booking/common/kit/audit.Middleware.func1.1.1
/go/src/bf/booking/common/kit/audit/audit.go:119
runtime.gopanic
/usr/local/go/src/runtime/panic.go:884
runtime.panicmem
/usr/local/go/src/runtime/panic.go:260
runtime.sigpanic
/usr/local/go/src/runtime/signal_unix.go:841
memeqbody
/usr/local/go/src/internal/bytealg/equal_amd64.s:151
runtime.mapaccess2_faststr
/usr/local/go/src/runtime/map_faststr.go:133
gitlab.xmltravelgate.com/hub-aggregator/booking/common/connectx/hardCode.MustRegisterOverheadByOrg
/go/src/bf/booking/common/connectx/hardCode/operation_sla.go:30
gitlab.xmltravelgate.com/hub-aggregator/booking/pkg/flow/bfhttp.RegisterSLAStats
/go/src/bf/booking/pkg/flow/bfhttp/metric.go:178
gitlab.xmltravelgate.com/hub-aggregator/kit/http.(*Handler).ServeHTTP
/go/pkg/mod/gitlab.xmltravelgate.com/hub-aggregator/kit@v1.46.22/http/handler.go:90
gitlab.xmltravelgate.com/hub-aggregator/booking/common/connectx.TransactionMiddleware.func1.1
/go/src/bf/booking/common/connectx/transaction.go:124
net/http.HandlerFunc.ServeHTTP
/usr/local/go/src/net/http/server.go:2122
github.com/travelgateX/go-jwt-tools.Middleware.func1.1
/go/pkg/mod/github.com/travelgate!x/go-jwt-tools@v1.5.0/auth.go:42
net/http.HandlerFunc.ServeHTTP
/usr/local/go/src/net/http/server.go:2122
gitlab.xmltravelgate.com/hub-aggregator/tgx-kit/header.Middleware.func1.1
/go/pkg/mod/gitlab.xmltravelgate.com/hub-aggregator/tgx-kit@v1.77.68/header/header.go:235
net/http.HandlerFunc.ServeHTTP
/usr/local/go/src/net/http/server.go:2122
gitlab.xmltravelgate.com/hub-aggregator/booking/common/kit/audit.Middleware.func1.1
/go/src/bf/booking/common/kit/audit/audit.go:165
net/http.HandlerFunc.ServeHTTP
/usr/local/go/src/net/http/server.go:2122
github.com/gorilla/handlers.CompressHandlerLevel.func1
/go/pkg/mod/github.com/gorilla/handlers@v1.5.1/compress.go:141
net/http.HandlerFunc.ServeHTTP
/usr/local/go/src/net/http/server.go:2122
gitlab.xmltravelgate.com/hub-aggregator/booking/common/stats.Middleware.func1.1
/go/src/bf/booking/common/stats/stats.go:209
net/http.HandlerFunc.ServeHTTP
/usr/local/go/src/net/http/server.go:2122
github.com/gorilla/handlers.(*cors).ServeHTTP
/go/pkg/mod/github.com/gorilla/handlers@v1.5.1/cors.go:54
gitlab.xmltravelgate.com/hub-aggregator/kit/http.Duplicate.func1.1
/go/pkg/mod/gitlab.xmltravelgate.com/hub-aggregator/kit@v1.46.22/http/duplicate.go:206
net/http.HandlerFunc.ServeHTTP
/usr/local/go/src/net/http/server.go:2122
github.com/gorilla/mux.(*Router).ServeHTTP
/go/pkg/mod/github.com/gorilla/mux@v1.8.0/mux.go:210
net/http.serverHandler.ServeHTTP
/usr/local/go/src/net/http/server.go:2936
net/http.(*conn).serve
/usr/local/go/src/net/http/server.go:1995

Thank you!

Yeah and specs also state:

A nil map is equivalent to an empty map except that no elements may be added.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.