Skip to content

Conversation

@zuiwuchang
Copy link

#109

大量的 rand.Intn 調用在傳入非法參數時將引發 painc。但通常這是 rand.Intn 返回 0 而非 painc 可能會使 base64Captcha 庫更容易使用。這份 pull 將對 rand.Intn 的調用替換爲了下述代碼以避免 painc

func randIntn(n int) int {
	if n > 0 {
		return rand.Intn(n)
	}
	return 0
}

@mojocn mojocn requested a review from Copilot September 29, 2025 10:08
Copy link

Copilot AI left a 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 randIntn function that returns 0 for non-positive inputs instead of panicking
  • Replaces all rand.Intn calls throughout the codebase with the safer randIntn function
  • Removes unused math/rand imports 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.

@mojocn mojocn assigned mojocn and Copilot and unassigned mojocn Sep 29, 2025
@mojocn mojocn requested a review from Copilot September 29, 2025 10:15
Copy link

Copilot AI left a 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.

Comment on lines +48 to 49
//y1 := float64(randIntn(y)+y);

Copy link

Copilot AI Sep 29, 2025

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.

Suggested change
//y1 := float64(randIntn(y)+y);

Copilot uses AI. Check for mistakes.
x2 := float64(rand.Intn(first) + end)
x2 := float64(randIntn(first) + end)

multiple := float64(rand.Intn(5)+3) / float64(5)
Copy link

Copilot AI Sep 29, 2025

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.

Suggested change
multiple := float64(rand.Intn(5)+3) / float64(5)
multiple := float64(randIntn(5)+3) / float64(5)

Copilot uses AI. Check for mistakes.
rh := rand.Intn(item.height)
rw := randIntn(item.width)
rh := randIntn(item.height)
fontSize := rawFontSize/2 + float64(rand.Intn(5))
Copy link

Copilot AI Sep 29, 2025

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.

Suggested change
fontSize := rawFontSize/2 + float64(rand.Intn(5))
fontSize := rawFontSize/2 + float64(randIntn(5))

Copilot uses AI. Check for mistakes.
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)])
Copy link

Copilot AI Sep 29, 2025

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.

Suggested change
snd := reversedSound(a.digitSounds[rand.Intn(10)])
snd := reversedSound(a.digitSounds[randIntn(10)])

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants