math icon indicating copy to clipboard operation
math copied to clipboard

Return value of MaxBigInt and family may change unexpectedly

Open keep94 opened this issue 6 years ago • 0 comments

big.Int instances are mutable. Which means that if a caller calls math.MaxBigInt(a, b) and later changes a or b, then what math.MaxBigInt returned could change unexpectedly

Consider the following code

a := big.NewInt(3)
b := big.NewInt(5)
c := math.MaxBigInt(a, b)  // c = 5, c and b point to the same big.Int
b.SetInt64(2)
// Oops, now c = 2, not 5. Caller won't be expecting this.

Although it would be slower, consider having math.MaxBigInt() and similar functions return a copy of the max value rather than the actual max value.

keep94 avatar Jun 27 '19 04:06 keep94