From e8809836584c2283cb87a0b68a1a467f01419fa5 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Mon, 6 May 2019 12:59:27 -0700 Subject: [PATCH] Lock before changing field of backends (#843) * Lock before changing field of backends The `SetBackend` method updates one of the members of the package-level variable `backends`. Since it's a pointer, it's probably safe on most architectures. But the `Backends` struct already has a `sync.RWMutex` field, and earlier in the file the `GetBackend` function goes through the trouble of explicitly acquiring a read and possibly a write lock. See: https://github.com/stripe/stripe-go/blob/d94ce0ed857fb1571b9b30bd24430bdac3d50c0f/stripe.go#L666 So to be safe, and consistent with the rest of the code, it makes sense to acquire a write lock before updating the `backends` value. --- stripe.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/stripe.go b/stripe.go index 16f198d76f..32ec794955 100644 --- a/stripe.go +++ b/stripe.go @@ -814,6 +814,9 @@ func SetAppInfo(info *AppInfo) { // SetBackend sets the backend used in the binding. func SetBackend(backend SupportedBackend, b Backend) { + backends.mu.Lock() + defer backends.mu.Unlock() + switch backend { case APIBackend: backends.API = b