From cfdd1cd5be095449d40e7831fd4d622a20e3e004 Mon Sep 17 00:00:00 2001 From: BurtonQin Date: Mon, 10 Feb 2020 21:49:46 +0800 Subject: [PATCH] cache, context, session: add lock to fix inconsistent field protection --- cache/memory.go | 3 +++ context/input.go | 2 ++ session/sess_cookie.go | 2 ++ 3 files changed, 7 insertions(+) diff --git a/cache/memory.go b/cache/memory.go index 1fec2eff..d8314e3c 100644 --- a/cache/memory.go +++ b/cache/memory.go @@ -218,9 +218,12 @@ func (bc *MemoryCache) vacuum() { } for { <-time.After(bc.dur) + bc.RLock() if bc.items == nil { + bc.RUnlock() return } + bc.RUnlock() if keys := bc.expiredKeys(); len(keys) != 0 { bc.clearItems(keys) } diff --git a/context/input.go b/context/input.go index 76040616..2a2bc75e 100644 --- a/context/input.go +++ b/context/input.go @@ -71,7 +71,9 @@ func (input *BeegoInput) Reset(ctx *Context) { input.CruSession = nil input.pnames = input.pnames[:0] input.pvalues = input.pvalues[:0] + input.dataLock.Lock() input.data = nil + input.dataLock.Unlock() input.RequestBody = []byte{} } diff --git a/session/sess_cookie.go b/session/sess_cookie.go index 145e53c9..6ad5debc 100644 --- a/session/sess_cookie.go +++ b/session/sess_cookie.go @@ -74,7 +74,9 @@ func (st *CookieSessionStore) SessionID() string { // SessionRelease Write cookie session to http response cookie func (st *CookieSessionStore) SessionRelease(w http.ResponseWriter) { + st.lock.Lock() encodedCookie, err := encodeCookie(cookiepder.block, cookiepder.config.SecurityKey, cookiepder.config.SecurityName, st.values) + st.lock.Unlock() if err == nil { cookie := &http.Cookie{Name: cookiepder.config.CookieName, Value: url.QueryEscape(encodedCookie),