Fix int8 overflow when operating on match and player point values

This commit is contained in:
Trevor Slocum 2024-07-30 14:41:41 -07:00
parent 78fdbe2206
commit 58f62bff48
3 changed files with 32 additions and 5 deletions

View file

@ -666,7 +666,7 @@ func (g *serverGame) handleWin() bool {
var reset bool
if g.Winner == 1 {
winEvent.Player = g.Player1.Name
g.Player1.Points = g.Player1.Points + winPoints*g.DoubleValue
g.Player1.Points = add8(g.Player1.Points, mul8(winPoints, g.DoubleValue))
if g.Player1.Points < g.Points {
reset = true
} else {
@ -674,7 +674,7 @@ func (g *serverGame) handleWin() bool {
}
} else {
winEvent.Player = g.Player2.Name
g.Player2.Points = g.Player2.Points + winPoints*g.DoubleValue
g.Player2.Points = add8(g.Player2.Points, mul8(winPoints, g.DoubleValue))
if g.Player2.Points < g.Points {
reset = true
} else {

View file

@ -556,6 +556,31 @@ func RandInt(max int) int {
return int(i.Int64())
}
// add8 adds two int8 values without overflowing.
func add8(a int8, b int8) int8 {
v := a
for i := int8(0); i < b; i++ {
a++
if a < 0 {
return 127
}
v++
}
return v
}
// mul8 multiplies two int8 values without overflowing.
func mul8(a int8, b int8) int8 {
var v int8
for i := int8(0); i < b; i++ {
v = add8(v, a)
if v == 127 {
return v
}
}
return v
}
func gnubgPosition(g *bgammon.Game) string {
var opponent int8 = 2
start := 0

View file

@ -395,9 +395,11 @@ COMMANDS:
}
points, err := strconv.Atoi(string(gamePoints))
if err != nil || points < 1 || points > 99 {
if err != nil || points < 1 {
sendUsage()
continue
} else if points > 127 {
points = 127
}
// Set default game name.
@ -614,10 +616,10 @@ COMMANDS:
var reset bool
if clientGame.Winner == 1 {
clientGame.Player1.Points = clientGame.Player1.Points + clientGame.DoubleValue
clientGame.Player1.Points = add8(clientGame.Player1.Points, clientGame.DoubleValue)
reset = clientGame.Player1.Points < clientGame.Points
} else {
clientGame.Player2.Points = clientGame.Player2.Points + clientGame.DoubleValue
clientGame.Player2.Points = add8(clientGame.Player2.Points, clientGame.DoubleValue)
reset = clientGame.Player2.Points < clientGame.Points
}
clientGame.addReplayHeader()