Skip to content

Commit 5ff859f

Browse files
rdimitrovclaude
andcommitted
Reject a nil config in New instead of panicking
New dereferenced config.RepoMap without checking the config pointer, so New(nil) panicked. NewConfig returns a nil config alongside its error, so a caller that ignores the error reaches this path. Also move client construction after validation, so the invalid-input paths no longer allocate a client they immediately discard. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Signed-off-by: Radoslav Dimitrov <radoslav@stacklok.com>
1 parent 7ff73ae commit 5ff859f

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

metadata/multirepo/multirepo.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,8 @@ func NewConfig(repoMap []byte, roots map[string][]byte) (*MultiRepoConfig, error
122122

123123
// New returns a multi-repository TUF client. All repositories described in the provided map file are initialized too
124124
func New(config *MultiRepoConfig) (*MultiRepoClient, error) {
125-
// create a multi repo client instance
126-
client := &MultiRepoClient{
127-
Config: config,
128-
TUFClients: map[string]*updater.Updater{},
125+
if config == nil {
126+
return nil, fmt.Errorf("no multi-repository config provided")
129127
}
130128

131129
// validate the map file before initializing anything, so that a malformed map
@@ -135,6 +133,12 @@ func New(config *MultiRepoConfig) (*MultiRepoClient, error) {
135133
return nil, err
136134
}
137135

136+
// create a multi repo client instance
137+
client := &MultiRepoClient{
138+
Config: config,
139+
TUFClients: map[string]*updater.Updater{},
140+
}
141+
138142
// create TUF clients for each repository listed in the map file
139143
if err := client.initTUFClients(); err != nil {
140144
return nil, err

metadata/multirepo/multirepo_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,12 @@ func TestNewRejectsConfigWithoutRepoMap(t *testing.T) {
228228
t.Fatal("New() should reject a config with no repository map")
229229
}
230230
}
231+
232+
func TestNewRejectsNilConfig(t *testing.T) {
233+
// NewConfig returns a nil config alongside its error, so a caller that
234+
// ignores the error passes nil straight into New.
235+
_, err := New(nil)
236+
if err == nil {
237+
t.Fatal("New() should reject a nil config")
238+
}
239+
}

0 commit comments

Comments
 (0)