Skip to content

Commit 584db3c

Browse files
committed
pkg/cdi: lock-protect setting/reading validator.
Protect setting/reading external validator against read/write race. Signed-off-by: Krisztian Litkey <[email protected]>
1 parent 378db26 commit 584db3c

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

pkg/cdi/spec.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"io/ioutil"
2222
"os"
2323
"path/filepath"
24+
"sync"
2425

2526
oci "github.com/opencontainers/runtime-spec/specs-go"
2627
"github.com/pkg/errors"
@@ -41,6 +42,7 @@ var (
4142

4243
// Externally set CDI Spec validation function.
4344
specValidator func(*cdi.Spec) error
45+
validatorLock sync.RWMutex
4446
)
4547

4648
// Spec represents a single CDI Spec. It is usually loaded from a
@@ -249,11 +251,16 @@ func ParseSpec(data []byte) (*cdi.Spec, error) {
249251
// is used for extra CDI Spec content validation whenever a Spec file
250252
// loaded (using ReadSpec() or NewSpec()) or written (Spec.Write()).
251253
func SetSpecValidator(fn func(*cdi.Spec) error) {
254+
validatorLock.Lock()
255+
defer validatorLock.Unlock()
252256
specValidator = fn
253257
}
254258

255259
// validateSpec validates the Spec using the extneral validator.
256260
func validateSpec(raw *cdi.Spec) error {
261+
validatorLock.RLock()
262+
defer validatorLock.RUnlock()
263+
257264
if specValidator == nil {
258265
return nil
259266
}

0 commit comments

Comments
 (0)