tpl: Fix crash when using imageConfig

defaultImageConfigCache is now initialized statically instead of relying on it
being initialized in the reset function.

Fixes #2806
This commit is contained in:
Tristan Rice 2016-12-25 04:43:50 -08:00 committed by Bjørn Erik Pedersen
parent 8b0c3b9b27
commit d055862a41
2 changed files with 30 additions and 15 deletions

View file

@ -393,19 +393,23 @@ func ResetCaches() {
// imageConfigCache is a lockable cache for image.Config objects. It must be // imageConfigCache is a lockable cache for image.Config objects. It must be
// locked before reading or writing to config. // locked before reading or writing to config.
var imageConfigCache struct { type imageConfigCache struct {
sync.RWMutex
config map[string]image.Config config map[string]image.Config
sync.RWMutex
}
var defaultImageConfigCache = imageConfigCache{
config: map[string]image.Config{},
} }
// resetImageConfigCache initializes and resets the imageConfig cache for the // resetImageConfigCache initializes and resets the imageConfig cache for the
// imageConfig template function. This should be run once before every batch of // imageConfig template function. This should be run once before every batch of
// template renderers so the cache is cleared for new data. // template renderers so the cache is cleared for new data.
func resetImageConfigCache() { func resetImageConfigCache() {
imageConfigCache.Lock() defaultImageConfigCache.Lock()
defer imageConfigCache.Unlock() defer defaultImageConfigCache.Unlock()
imageConfigCache.config = map[string]image.Config{} defaultImageConfigCache.config = map[string]image.Config{}
} }
// imageConfig returns the image.Config for the specified path relative to the // imageConfig returns the image.Config for the specified path relative to the
@ -421,9 +425,9 @@ func imageConfig(path interface{}) (image.Config, error) {
} }
// Check cache for image config. // Check cache for image config.
imageConfigCache.RLock() defaultImageConfigCache.RLock()
config, ok := imageConfigCache.config[filename] config, ok := defaultImageConfigCache.config[filename]
imageConfigCache.RUnlock() defaultImageConfigCache.RUnlock()
if ok { if ok {
return config, nil return config, nil
@ -436,9 +440,9 @@ func imageConfig(path interface{}) (image.Config, error) {
config, _, err = image.DecodeConfig(f) config, _, err = image.DecodeConfig(f)
imageConfigCache.Lock() defaultImageConfigCache.Lock()
imageConfigCache.config[filename] = config defaultImageConfigCache.config[filename] = config
imageConfigCache.Unlock() defaultImageConfigCache.Unlock()
return config, err return config, err
} }

View file

@ -629,6 +629,17 @@ func TestImageConfig(t *testing.T) {
input []byte input []byte
expected image.Config expected image.Config
}{ }{
// Make sure that the cache is initialized by default.
{
resetCache: false,
path: "a.png",
input: blankImage(10, 10),
expected: image.Config{
Width: 10,
Height: 10,
ColorModel: color.NRGBAModel,
},
},
{ {
resetCache: true, resetCache: true,
path: "a.png", path: "a.png",
@ -685,8 +696,8 @@ func TestImageConfig(t *testing.T) {
t.Errorf("[%d] imageConfig: expected '%v', got '%v'", i, this.expected, result) t.Errorf("[%d] imageConfig: expected '%v', got '%v'", i, this.expected, result)
} }
if len(imageConfigCache.config) == 0 { if len(defaultImageConfigCache.config) == 0 {
t.Error("imageConfigCache should have at least 1 item") t.Error("defaultImageConfigCache should have at least 1 item")
} }
} }
@ -705,8 +716,8 @@ func TestImageConfig(t *testing.T) {
// test cache clearing // test cache clearing
ResetCaches() ResetCaches()
if len(imageConfigCache.config) != 0 { if len(defaultImageConfigCache.config) != 0 {
t.Error("ResetCaches should have cleared imageConfigCache") t.Error("ResetCaches should have cleared defaultImageConfigCache")
} }
} }