Skip to content

Commit

Permalink
codeql - safe handling of int32 conversion
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Avetisyan <[email protected]>
  • Loading branch information
havetisyan committed May 19, 2024
1 parent ce700a5 commit 6faa82d
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions libs/go/zmscli/quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ func (cli Zms) GetQuota(dn string) (*string, error) {
return cli.dumpByFormat(quota, oldYamlConverter)
}

func getQuotaValue(value string) (int32, error) {
val, err := strconv.ParseInt(value, 10, 32)
if err != nil {
return 0, err
}
return int32(val), nil
}

func (cli Zms) SetQuota(dn string, attrs []string) (*string, error) {
quota, err := cli.Zms.GetQuota(zms.DomainName(dn))
if err != nil {
Expand All @@ -54,33 +62,34 @@ func (cli Zms) SetQuota(dn string, attrs []string) (*string, error) {
return nil, fmt.Errorf("bad quota syntax: zms-cli help set-quota")
}
key := attr[0:idx]
value, err := strconv.Atoi(attr[idx+1:])

value, err := getQuotaValue(attr[idx+1:])
if err != nil {
return nil, fmt.Errorf("bad quota syntax: zms-cli help set-quota")
return nil, err
}
switch key {
case "role":
quota.Role = int32(value)
quota.Role = value
case "role-member":
quota.RoleMember = int32(value)
quota.RoleMember = value
case "group":
quota.Group = int32(value)
quota.Group = value
case "group-member":
quota.GroupMember = int32(value)
quota.GroupMember = value
case "subdomain":
quota.Subdomain = int32(value)
quota.Subdomain = value
case "policy":
quota.Policy = int32(value)
quota.Policy = value
case "assertion":
quota.Assertion = int32(value)
quota.Assertion = value
case "service":
quota.Service = int32(value)
quota.Service = value
case "service-host":
quota.ServiceHost = int32(value)
quota.ServiceHost = value
case "public-key":
quota.PublicKey = int32(value)
quota.PublicKey = value
case "entity":
quota.Entity = int32(value)
quota.Entity = value
default:
return nil, fmt.Errorf("bad quota syntax: zms-cli help set-quota")
}
Expand Down

0 comments on commit 6faa82d

Please sign in to comment.