Skip to content

Commit

Permalink
lxd/cluster: Add int upper bound check
Browse files Browse the repository at this point in the history
Signed-off-by: Julian Pelizäus <julian.pelizaeus@canonical.com>
  • Loading branch information
roosterfish committed May 16, 2024
1 parent 6f7defd commit 77c5df6
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions lxd/cluster/membership.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/x509"
"encoding/pem"
"fmt"
"math"
"os"
"path/filepath"
"sync"
Expand Down Expand Up @@ -294,9 +295,19 @@ func Accept(state *state.State, gateway *Gateway, name, address string, schema,
Name: name,
}

if count > 1 && voters < int(state.GlobalConfig.MaxVoters()) {
maxVoters := state.GlobalConfig.MaxVoters()
if maxVoters > math.MaxInt {
return nil, fmt.Errorf("Cannot convert maximum voter cluster members to int: Upper bound exceeded")
}

maxStandBy := state.GlobalConfig.MaxStandBy()
if maxStandBy > math.MaxInt {
return nil, fmt.Errorf("Cannot convert maximum standby cluster members to int: Upper bound exceeded")
}

if count > 1 && voters < int(maxVoters) {
node.Role = db.RaftVoter
} else if standbys < int(state.GlobalConfig.MaxStandBy()) {
} else if standbys < int(maxStandBy) {
node.Role = db.RaftStandBy
}

Expand Down Expand Up @@ -1100,10 +1111,20 @@ func newRolesChanges(state *state.State, gateway *Gateway, nodes []db.RaftNode,
}
}

maxVoters := state.GlobalConfig.MaxVoters()
if maxVoters > math.MaxInt {
return nil, fmt.Errorf("Cannot convert maximum voter nodes to int: Upper bound exceeded")
}

maxStandBy := state.GlobalConfig.MaxStandBy()
if maxStandBy > math.MaxInt {
return nil, fmt.Errorf("Cannot convert maximum standby nodes to int: Upper bound exceeded")
}

roles := &app.RolesChanges{
Config: app.RolesConfig{
Voters: int(state.GlobalConfig.MaxVoters()),
StandBys: int(state.GlobalConfig.MaxStandBy()),
Voters: int(maxVoters),
StandBys: int(maxStandBy),
},
State: cluster,
}
Expand Down

0 comments on commit 77c5df6

Please sign in to comment.