internal/atlas: check overflows

This commit is contained in:
Hajime Hoshi 2023-08-06 13:46:41 +09:00
parent 1ae5e022b6
commit a50d9e6291

View File

@ -17,6 +17,7 @@ package atlas
import ( import (
"fmt" "fmt"
"image" "image"
"math"
"runtime" "runtime"
"sync" "sync"
@ -66,12 +67,16 @@ const baseCountToPutOnSourceBackend = 10
func putImagesOnSourceBackend(graphicsDriver graphicsdriver.Graphics) error { func putImagesOnSourceBackend(graphicsDriver graphicsdriver.Graphics) error {
// The counter usedAsDestinationCount is updated at most once per frame (#2676). // The counter usedAsDestinationCount is updated at most once per frame (#2676).
for i := range imagesUsedAsDestination { for i := range imagesUsedAsDestination {
if i.usedAsDestinationCount < math.MaxInt {
i.usedAsDestinationCount++ i.usedAsDestinationCount++
}
delete(imagesUsedAsDestination, i) delete(imagesUsedAsDestination, i)
} }
for i := range imagesToPutOnSourceBackend { for i := range imagesToPutOnSourceBackend {
if i.usedAsSourceCount < math.MaxInt {
i.usedAsSourceCount++ i.usedAsSourceCount++
}
if i.usedAsSourceCount >= baseCountToPutOnSourceBackend*(1<<uint(min(i.usedAsDestinationCount, 31))) { if i.usedAsSourceCount >= baseCountToPutOnSourceBackend*(1<<uint(min(i.usedAsDestinationCount, 31))) {
if err := i.putOnSourceBackend(graphicsDriver); err != nil { if err := i.putOnSourceBackend(graphicsDriver); err != nil {
return err return err