Skip to content

Commit

Permalink
fix(api): Protects DisableConfigDir from concurrent access using mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
yyoshiki41 committed Apr 16, 2023
1 parent 5348f88 commit 773a454
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"bufio"
"io"
"os"
"sync"
"time"

"github.com/pdfcpu/pdfcpu/pkg/log"
Expand Down Expand Up @@ -161,11 +162,19 @@ func EnsureDefaultConfigAt(path string) error {
return model.EnsureDefaultConfigAt(path)
}

var (
// mutexDisableConfigDir protects DisableConfigDir from concurrent access.
// NOTE: This is not a guard for model.ConfigPath variable.
mutexDisableConfigDir sync.Mutex
)

// DisableConfigDir disables the configuration directory.
// Any needed default configuration will be loaded from configuration.go
// Since the config dir also contains the user font dir, this also limits font usage to the default core font set
// No user fonts will be available.
func DisableConfigDir() {
mutexDisableConfigDir.Lock()
defer mutexDisableConfigDir.Unlock()
// Call if you don't want to use a specific configuration
// and also do not need to use user fonts.
model.ConfigPath = "disable"
Expand Down
36 changes: 36 additions & 0 deletions pkg/api/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package api

import (
"sync"
"testing"

"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model"
)

func Test(t *testing.M) {
t.Run()
}

func TestDisableConfigDir(t *testing.T) {
t.Parallel()
DisableConfigDir()

if model.ConfigPath != "disable" {
t.Errorf("model.ConfigPath != \"disable\" (%s)", model.ConfigPath)
}
}

func TestDisableConfigDir_Parallel(t *testing.T) {
t.Parallel()

var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
DisableConfigDir()
}()
}
wg.Wait()
t.Log("DisableConfigDir is passed")
}

0 comments on commit 773a454

Please sign in to comment.