fix rand.Intn painc#118
Conversation
There was a problem hiding this comment.
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.
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); | ||
|
|
There was a problem hiding this comment.
[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) |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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)) |
| @@ -81,7 +81,7 @@ func (a *ItemAudio) makeBackgroundSound(length int) []byte { | |||
| for i := 0; i < length/(sampleRate/10); i++ { | |||
| snd := reversedSound(a.digitSounds[rand.Intn(10)]) | |||
There was a problem hiding this comment.
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