configuration: ignore an empty configuration struct in setConfiguration (#21)

If the plugin leaves the configuration struct empty, go will optimize away allocations of the zero-width struct failing the `setConfiguration` check that prevents a user from introducing race conditions.
This commit is contained in:
Jesse Hallam 2018-11-15 08:02:55 -05:00 committed by Hanzei
parent b303e8da00
commit 76ba0b57a7

View file

@ -1,6 +1,8 @@
package main
import (
"reflect"
"github.com/pkg/errors"
)
@ -53,6 +55,13 @@ func (p *Plugin) setConfiguration(configuration *configuration) {
defer p.configurationLock.Unlock()
if configuration != nil && p.configuration == configuration {
// Ignore assignment if the configuration struct is empty. Go will optimize the
// allocation for same to point at the same memory address, breaking the check
// above.
if reflect.ValueOf(*configuration).NumField() == 0 {
return
}
panic("setConfiguration called with the existing configuration")
}