-
Notifications
You must be signed in to change notification settings - Fork 300
fix rand.Intn painc #118
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
base: master
Are you sure you want to change the base?
fix rand.Intn painc #118
Conversation
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.
Pull Request Overview
This PR addresses panic issues in the base64Captcha library by replacing direct rand.Intn calls with a safer wrapper function randIntn that prevents panics when negative or zero parameters are passed.
- Introduces a new
randIntnfunction that returns 0 for non-positive inputs instead of panicking - Replaces all
rand.Intncalls throughout the codebase with the saferrandIntnfunction - Removes unused
math/randimports where appropriate
Reviewed Changes
Copilot reviewed 9 out of 10 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| randIntn.go | New helper function that wraps rand.Intn with panic prevention |
| random_math.go | Updates RandText and randIntRange functions to use safe wrapper |
| item_digit.go | Updates strikeThrough and randomBrightness functions to use safe wrapper |
| item_char.go | Updates multiple drawing functions and reorganizes imports |
| item_audio.go | Updates makeBackgroundSound function to use safe wrapper |
| fonts.go | Updates randFontFrom function and removes unused import |
| driver_language.go | Updates generateRandomRune function and removes unused import |
| driver_digit.go | Updates DrawCaptcha function to use safe wrapper |
| driver_chinese.go | Updates GenerateIdQuestionAnswer function and removes unused import |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
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.
Pull Request Overview
Copilot reviewed 9 out of 10 changed files in this pull request and generated 4 comments.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| //y1 := float64(randIntn(y)+y); | ||
|
|
Copilot
AI
Sep 29, 2025
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.
[nitpick] The commented code on line 48 shows inconsistent usage - it adds y to randIntn(y) which could exceed bounds, while the new code would return 0 for invalid parameters. Consider removing this outdated comment or updating it to reflect the current safe behavior.
| //y1 := float64(randIntn(y)+y); |
| x2 := float64(rand.Intn(first) + end) | ||
| x2 := float64(randIntn(first) + end) | ||
|
|
||
| multiple := float64(rand.Intn(5)+3) / float64(5) |
Copilot
AI
Sep 29, 2025
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.
This line still uses the unsafe rand.Intn() directly instead of the safe randIntn() wrapper that was introduced to prevent panics. This should be updated to randIntn(5) for consistency with the rest of the changes.
| multiple := float64(rand.Intn(5)+3) / float64(5) | |
| multiple := float64(randIntn(5)+3) / float64(5) |
| rh := rand.Intn(item.height) | ||
| rw := randIntn(item.width) | ||
| rh := randIntn(item.height) | ||
| fontSize := rawFontSize/2 + float64(rand.Intn(5)) |
Copilot
AI
Sep 29, 2025
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.
This line still uses the unsafe rand.Intn() directly instead of the safe randIntn() wrapper. This should be updated to randIntn(5) for consistency with the rest of the changes.
| fontSize := rawFontSize/2 + float64(rand.Intn(5)) | |
| fontSize := rawFontSize/2 + float64(randIntn(5)) |
| func (a *ItemAudio) makeBackgroundSound(length int) []byte { | ||
| b := a.makeWhiteNoise(length, 4) | ||
| for i := 0; i < length/(sampleRate/10); i++ { | ||
| snd := reversedSound(a.digitSounds[rand.Intn(10)]) |
Copilot
AI
Sep 29, 2025
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.
This line still uses the unsafe rand.Intn() directly instead of the safe randIntn() wrapper. This should be updated to randIntn(10) for consistency with the rest of the changes.
| snd := reversedSound(a.digitSounds[rand.Intn(10)]) | |
| snd := reversedSound(a.digitSounds[randIntn(10)]) |
#109
大量的 rand.Intn 調用在傳入非法參數時將引發 painc。但通常這是 rand.Intn 返回 0 而非 painc 可能會使 base64Captcha 庫更容易使用。這份 pull 將對 rand.Intn 的調用替換爲了下述代碼以避免 painc