Skip to content

Commit 1e74248

Browse files
committed
Added nil check gaurd to SetAuditors
Signed-off-by: Effi-S <effi.szt@gmail.com>
1 parent 1025c2c commit 1e74248

6 files changed

Lines changed: 32 additions & 10 deletions

File tree

cmd/tokengen/cobra/pp/zkatdlognoghv1/update.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ func Update(args *UpdateArgs) error {
9999
// Clear auditor and issuers if provided, and add them again.
100100
// If not provided, do not change them.
101101
if len(args.Auditors) > 0 {
102-
pp.SetAuditors(nil)
102+
if err := pp.SetAuditors(nil); err != nil {
103+
return errors.Wrapf(err, "failed to clear auditors")
104+
}
103105
}
104106
if len(args.Issuers) > 0 {
105107
pp.SetIssuers(nil)

integration/token/fungible/support.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,7 +1855,7 @@ func PrepareUpdatedPublicParams(network *integration.Infrastructure, auditor str
18551855
Validate() error
18561856
Serialize() ([]byte, error)
18571857
SetIssuers(identities []driver.Identity)
1858-
SetAuditors(identities []driver.Identity)
1858+
SetAuditors(identities []driver.Identity) error
18591859
AddAuditor(identity2 driver.Identity)
18601860
AddIssuer(identity2 driver.Identity)
18611861
}
@@ -1876,7 +1876,7 @@ func PrepareUpdatedPublicParams(network *integration.Infrastructure, auditor str
18761876
pp.AddAuditor(auditorId)
18771877
pp.AddIssuer(issuerId)
18781878
} else {
1879-
pp.SetAuditors([]driver.Identity{auditorId})
1879+
gomega.Expect(pp.SetAuditors([]driver.Identity{auditorId})).NotTo(gomega.HaveOccurred())
18801880
pp.SetIssuers([]driver.Identity{issuerId})
18811881
}
18821882

token/core/fabtoken/v1/setup/setup.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,15 @@ func (p *PublicParams) SetIssuers(ids []driver.Identity) {
225225
p.IssuerIDs = ids
226226
}
227227

228-
// SetAuditors sets the auditors to the passed identities
229-
func (p *PublicParams) SetAuditors(ids []driver.Identity) {
228+
// SetAuditors sets the auditor to the first of the passed identities.
229+
// fabtoken supports a single auditor, so it uses ids[0] and ignores the rest.
230+
// It returns an error if ids is empty or nil, leaving the current auditor untouched.
231+
func (p *PublicParams) SetAuditors(ids []driver.Identity) error {
232+
if len(ids) == 0 {
233+
return errors.New("no auditor identities provided")
234+
}
230235
p.Auditor = ids[0]
236+
return nil
231237
}
232238

233239
// Auditors returns the list of authorized auditors

token/core/fabtoken/v1/setup/setup_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,13 @@ func TestPublicParams_Methods(t *testing.T) {
119119

120120
// Test SetAuditors
121121
newAuditor := driver.Identity([]byte("auditor2"))
122-
pp.SetAuditors([]driver.Identity{newAuditor})
122+
assert.NoError(t, pp.SetAuditors([]driver.Identity{newAuditor}))
123+
assert.Equal(t, newAuditor, pp.AuditorIdentity())
124+
125+
// Test SetAuditors with an empty/nil slice returns an error and leaves
126+
// the current auditor untouched (guards against the index-out-of-range panic).
127+
assert.Error(t, pp.SetAuditors(nil))
128+
assert.Error(t, pp.SetAuditors([]driver.Identity{}))
123129
assert.Equal(t, newAuditor, pp.AuditorIdentity())
124130
})
125131

@@ -730,7 +736,7 @@ func TestPublicParams_SettersWithMultipleValues(t *testing.T) {
730736
[]byte("auditor1"),
731737
[]byte("auditor2"),
732738
}
733-
pp.SetAuditors(auditors)
739+
assert.NoError(t, pp.SetAuditors(auditors))
734740
assert.Equal(t, driver.Identity([]byte("auditor1")), pp.AuditorIdentity())
735741
assert.Equal(t, []driver.Identity{driver.Identity([]byte("auditor1"))}, pp.Auditors())
736742
}

token/core/zkatdlog/nogh/v1/setup/setup.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -620,9 +620,12 @@ func (p *PublicParams) SetIssuers(ids []driver.Identity) {
620620
p.IssuerIDs = ids
621621
}
622622

623-
// SetAuditors sets the auditors to the passed identities
624-
func (p *PublicParams) SetAuditors(ids []driver.Identity) {
623+
// SetAuditors sets the auditors to the passed identities.
624+
// zkatdlog supports a list of auditors, so an empty or nil slice simply clears it.
625+
// It always returns a nil error; the signature matches the shared setter contract.
626+
func (p *PublicParams) SetAuditors(ids []driver.Identity) error {
625627
p.AuditorIDs = ids
628+
return nil
626629
}
627630

628631
func (p *PublicParams) ComputeHash() ([]byte, error) {

token/core/zkatdlog/nogh/v1/setup/setup_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,14 @@ func TestPublicParamsModification(t *testing.T) {
289289

290290
// Test SetAuditors
291291
newAuditors := []driver.Identity{driver.Identity("newAuditor")}
292-
pp.SetAuditors(newAuditors)
292+
assert.NoError(t, pp.SetAuditors(newAuditors))
293293
assert.Equal(t, newAuditors, pp.Auditors())
294294

295+
// zkatdlog supports a list of auditors, so an empty/nil slice clears them
296+
// without error (unlike fabtoken's single-auditor setter).
297+
assert.NoError(t, pp.SetAuditors(nil))
298+
assert.Empty(t, pp.Auditors())
299+
295300
// Test AddIssuer
296301
issuer1 := driver.Identity("issuer1")
297302
issuer2 := driver.Identity("issuer2")

0 commit comments

Comments
 (0)