Skip to content

Commit e6dde77

Browse files
committed
Bugfix: Avoid uint overflow for timeouts and other fields
1 parent d4f603d commit e6dde77

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

internal/app/publish/messagegenerator.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,17 @@ func (g *MessageGenerator) StateToRefereeMessage(matchState *state.State) (r *st
111111

112112
func updateTeam(teamInfo *state.Referee_TeamInfo, teamState *state.TeamInfo) {
113113
*teamInfo.Name = *teamState.Name
114-
*teamInfo.Score = uint32(*teamState.Goals)
115-
*teamInfo.RedCards = uint32(len(teamState.RedCards))
114+
*teamInfo.Score = unsigned32(*teamState.Goals)
115+
*teamInfo.RedCards = unsigned(len(teamState.RedCards))
116116
teamInfo.YellowCardTimes = mapYellowCardTimes(teamState.YellowCards)
117-
*teamInfo.YellowCards = uint32(len(teamState.YellowCards))
118-
*teamInfo.Timeouts = uint32(*teamState.TimeoutsLeft)
119-
*teamInfo.Goalkeeper = uint32(*teamState.Goalkeeper)
120-
*teamInfo.FoulCounter = uint32(len(teamState.Fouls))
121-
*teamInfo.BallPlacementFailures = uint32(*teamState.BallPlacementFailures)
117+
*teamInfo.YellowCards = unsigned(len(teamState.YellowCards))
118+
*teamInfo.Timeouts = unsigned32(*teamState.TimeoutsLeft)
119+
*teamInfo.Goalkeeper = unsigned32(*teamState.Goalkeeper)
120+
*teamInfo.FoulCounter = unsigned(len(teamState.Fouls))
121+
*teamInfo.BallPlacementFailures = unsigned32(*teamState.BallPlacementFailures)
122122
*teamInfo.BallPlacementFailuresReached = *teamState.BallPlacementFailuresReached
123123
*teamInfo.CanPlaceBall = *teamState.CanPlaceBall
124-
*teamInfo.MaxAllowedBots = uint32(*teamState.MaxAllowedBots)
124+
*teamInfo.MaxAllowedBots = unsigned32(*teamState.MaxAllowedBots)
125125
*teamInfo.BotSubstitutionIntent = *teamState.BotSubstitutionIntent
126126
timeoutTime, _ := ptypes.Duration(teamState.TimeoutTimeLeft)
127127
*teamInfo.TimeoutTime = mapTime(timeoutTime)
@@ -160,3 +160,16 @@ func newTeamInfo() (t *state.Referee_TeamInfo) {
160160
t.BotSubstitutionIntent = new(bool)
161161
return
162162
}
163+
164+
func unsigned32(v int32) uint32 {
165+
if v < 0 {
166+
return 0
167+
}
168+
return uint32(v)
169+
}
170+
func unsigned(v int) uint32 {
171+
if v < 0 {
172+
return 0
173+
}
174+
return uint32(v)
175+
}

internal/app/statemachine/change_command.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ func (s *StateMachine) processChangeNewCommand(newState *state.State, newCommand
1818
case state.Command_KICKOFF, state.Command_PENALTY:
1919
newState.CurrentActionTimeRemaining = ptypes.DurationProto(s.gameConfig.PrepareTimeout)
2020
case state.Command_TIMEOUT:
21-
*newState.TeamInfo(*newState.Command.ForTeam).TimeoutsLeft--
21+
if *newState.TeamInfo(*newState.Command.ForTeam).TimeoutsLeft > 0 {
22+
*newState.TeamInfo(*newState.Command.ForTeam).TimeoutsLeft--
23+
}
2224
}
2325

2426
// determine next command

0 commit comments

Comments
 (0)