Skip to content
This repository has been archived by the owner on Mar 18, 2021. It is now read-only.

Return value of MaxBigInt and family may change unexpectedly #4

Open
keep94 opened this issue Jun 27, 2019 · 0 comments
Open

Return value of MaxBigInt and family may change unexpectedly #4

keep94 opened this issue Jun 27, 2019 · 0 comments

Comments

@keep94
Copy link

keep94 commented Jun 27, 2019

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant