Fix data race in LoadStandardTags

By loading the tags in a `sync.Once`.
pull/63/head
Bjørn Erik Pedersen 2021-07-17 12:42:14 +02:00
parent a6301f85c8
commit 41dc97a713
No known key found for this signature in database
GPG Key ID: 330E6E2BD4859D8F
1 changed files with 56 additions and 55 deletions

View File

@ -176,6 +176,7 @@ func (it *IndexedTag) DoesSupportType(tagType exifcommon.TagTypePrimitive) bool
// TagIndex is a tag-lookup facility.
type TagIndex struct {
tagsInit sync.Once
tagsByIfd map[string]map[uint16]*IndexedTag
tagsByIfdR map[string]map[string]*IndexedTag
@ -253,10 +254,8 @@ func (ti *TagIndex) getOne(ifdPath string, id uint16) (it *IndexedTag, err error
}
}()
if len(ti.tagsByIfd) == 0 {
err := LoadStandardTags(ti)
err = LoadStandardTags(ti)
log.PanicIf(err)
}
ti.mutex.Lock()
defer ti.mutex.Unlock()
@ -384,10 +383,8 @@ func (ti *TagIndex) GetWithName(ii *exifcommon.IfdIdentity, name string) (it *In
}
}()
if len(ti.tagsByIfdR) == 0 {
err := LoadStandardTags(ti)
err = LoadStandardTags(ti)
log.PanicIf(err)
}
ifdPath := ii.UnindexedString()
@ -408,6 +405,8 @@ func LoadStandardTags(ti *TagIndex) (err error) {
}
}()
ti.tagsInit.Do(func() {
// Read static data.
encodedIfds := make(map[string][]encodedTag)
@ -471,5 +470,7 @@ func LoadStandardTags(ti *TagIndex) (err error) {
tagsLogger.Debugf(nil, "(%d) tags loaded.", count)
})
return nil
}