Skip to content

Commit

Permalink
protect against assigning a non-existent declaration to a set. with r…
Browse files Browse the repository at this point in the history
…egression test.
  • Loading branch information
jessepeterson committed Jun 9, 2023
1 parent 7f38da5 commit 7fd4786
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 5 additions & 0 deletions storage/file/sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package file
import (
"context"
"fmt"
"os"
"path"
"path/filepath"
)
Expand All @@ -18,6 +19,10 @@ func (s *File) RetrieveSetDeclarations(_ context.Context, setName string) ([]str
func (s *File) StoreSetDeclaration(_ context.Context, setName, declarationID string) (bool, error) {
s.mu.Lock()
defer s.mu.Unlock()
_, err := os.Stat(s.declarationFilename(declarationID))
if err != nil {
return false, fmt.Errorf("checking declaration: %w", err)
}
// set the forward reference
changed, err := setOrRemoveIn(s.setFilename(setName), declarationID, true)
if err != nil {
Expand Down
8 changes: 7 additions & 1 deletion storage/internal/test/sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ type setAndDeclStorage interface {
}

func testSet(t *testing.T, storage setAndDeclStorage, ctx context.Context, decl *ddm.Declaration, setName string) {
// associate (wrong)
_, err := storage.StoreSetDeclaration(ctx, setName, decl.Identifier+"_invalid")
if err == nil {
t.Fatal("should be an error")
}

// associate
_, err := storage.StoreSetDeclaration(ctx, setName, decl.Identifier)
_, err = storage.StoreSetDeclaration(ctx, setName, decl.Identifier)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 7fd4786

Please sign in to comment.