Skip to content

Commit

Permalink
Fix overflow checking for xxxRepeat (#558)
Browse files Browse the repository at this point in the history
  • Loading branch information
marco6 authored Jul 5, 2024
1 parent 046347d commit 7000200
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions starlark/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"log"
"math/big"
"math/bits"
"sort"
"strings"
"sync/atomic"
Expand Down Expand Up @@ -1192,8 +1193,8 @@ func tupleRepeat(elems Tuple, n Int) (Tuple, error) {
return nil, nil
}
// Inv: i > 0, len > 0
sz := len(elems) * i
if sz < 0 || sz >= maxAlloc { // sz < 0 => overflow
of, sz := bits.Mul(uint(len(elems)), uint(i))
if of != 0 || sz >= maxAlloc { // of != 0 => overflow
// Don't print sz.
return nil, fmt.Errorf("excessive repeat (%d * %d elements)", len(elems), i)
}
Expand Down Expand Up @@ -1224,8 +1225,8 @@ func stringRepeat(s String, n Int) (String, error) {
return "", nil
}
// Inv: i > 0, len > 0
sz := len(s) * i
if sz < 0 || sz >= maxAlloc { // sz < 0 => overflow
of, sz := bits.Mul(uint(len(s)), uint(i))
if of != 0 || sz >= maxAlloc { // of != 0 => overflow
// Don't print sz.
return "", fmt.Errorf("excessive repeat (%d * %d elements)", len(s), i)
}
Expand Down

0 comments on commit 7000200

Please sign in to comment.