From 4e42a9b3534f2937d1dc22513c215da412352bef Mon Sep 17 00:00:00 2001 From: Piotr Grabowski Date: Thu, 18 Jul 2024 14:02:37 +0200 Subject: [PATCH] Fix a nil pointer dereference in GetSourceNames() (#541) Fix a nil pointer dereference in `GetSourceNames()`, which happened quite often for me while developing Quesma - I often start Quesma before Elastic is fully up, therefore `im.sources` would be nil (failed to load from Elastic) and `GetSourceNames()` would panic. --- quesma/elasticsearch/index_management.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/quesma/elasticsearch/index_management.go b/quesma/elasticsearch/index_management.go index 5d58ff1d5..d4caeb8a7 100644 --- a/quesma/elasticsearch/index_management.go +++ b/quesma/elasticsearch/index_management.go @@ -50,12 +50,17 @@ func (im *indexManagement) ReloadIndices() { } func (im *indexManagement) GetSources() Sources { - return *im.sources.Load() + if s := im.sources.Load(); s != nil { + return *s + } else { + logger.Warn().Msg("Indices are not yet loaded, returning empty sources.") + return Sources{} + } } func (im *indexManagement) GetSourceNames() map[string]bool { names := make(map[string]bool) - sources := *im.sources.Load() + sources := im.GetSources() for _, stream := range sources.DataStreams { names[stream.Name] = true }