From 40578d9d5f9faba2089f481bc956b26657340b0e Mon Sep 17 00:00:00 2001 From: TorchedSammy <38820196+TorchedSammy@users.noreply.github.com> Date: Sun, 19 Dec 2021 22:37:44 -0400 Subject: [PATCH] fix: use a rwmutex for aliases map --- aliases.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/aliases.go b/aliases.go index b79bbe6..ec1ecae 100644 --- a/aliases.go +++ b/aliases.go @@ -2,6 +2,7 @@ package main import ( "strings" + "sync" "github.com/yuin/gopher-lua" ) @@ -10,16 +11,21 @@ var aliases *hilbishAliases type hilbishAliases struct { aliases map[string]string + mu *sync.RWMutex } // initialize aliases map func NewAliases() *hilbishAliases { return &hilbishAliases{ aliases: make(map[string]string), + mu: &sync.RWMutex{}, } } func (h *hilbishAliases) Add(alias, cmd string) { + h.mu.Lock() + defer h.mu.Unlock() + h.aliases[alias] = cmd } @@ -28,10 +34,16 @@ func (h *hilbishAliases) All() map[string]string { } func (h *hilbishAliases) Delete(alias string) { + h.mu.Lock() + defer h.mu.Unlock() + delete(h.aliases, alias) } func (h *hilbishAliases) Resolve(cmdstr string) string { + h.mu.RLock() + defer h.mu.RUnlock() + args := strings.Split(cmdstr, " ") for h.aliases[args[0]] != "" { alias := h.aliases[args[0]]