-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RandomAlphaString fix for 32-bit #200
Conversation
string.go
Outdated
@@ -28,16 +28,16 @@ func RandomAlphaString(size int) string { | |||
if err != nil { | |||
panic(err) | |||
} | |||
val := int(valBig.Int64()) | |||
val := valBig.Int64() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I appreciate the very local fix. I'm not sure I grok what a 32-bit build has to do with int(bigInt.Int64()) -> a negative number. I'm curious if we know whether we're:
- Working around a golang bug or
- We mis-read/neglected reading the golang documentation describing this behavior of
big.Int
So as a devil's advocate to the fix here, I feel the complexity of maintaining this is the use of a cryptographically secure number generator. Which goes through this bigint stuff.
I don't think we need cryptographic secure number generation here. There's no (documented) production use of the function in rdk/app/goutils. And if we do need cryptographic generation, we shouldn't expose some string-ifying function for it and let users dictate how long the output should be.
Can we instead just do:
alphas = fmt.Sprintf("%s%s", alphaLowers, strings.ToUpper(alphaLowers))
...
idx := math/rand.Intn(len(alphas))
val := alphas[idx]
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
int(random_positive_64_bit_number) on a 32-bit platform can be negative from truncation I think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I see now. The reproducer is much more clear about what's going on than this code. Thanks for adding that to the PR description!
If my above suggestion is too much code slinging for a "just fix it" issue can we just change:
maxIntBig = big.NewInt(math.MaxInt64)
to MaxInt32
instead?
The 64-bit numbers here are just to make the selection "as uniform as possible". 32-bit is perfectly fine to be effectively uniform selection.
Abe Winter seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
For security reasons, this PR must be labeled with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
In 32-bit builds, RandomAlphaString can crash with 'index out of range'
Note: golang won't let you compile a simple
array[-1]
because it knows negative numbers shouldn't be array indices, but this sample will repro if you want to see the failure:Then
GOARCH=386 go build main.go && main
(if you're on M1 mac, do GOARCH=arm)