-
Notifications
You must be signed in to change notification settings - Fork 310
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -5,9 +5,6 @@ import ( | |||||
| "encoding/base64" | ||||||
| "errors" | ||||||
| "fmt" | ||||||
| "github.com/golang/freetype" | ||||||
| "github.com/golang/freetype/truetype" | ||||||
| "golang.org/x/image/font" | ||||||
| "image" | ||||||
| "image/color" | ||||||
| "image/draw" | ||||||
|
|
@@ -16,17 +13,21 @@ import ( | |||||
| "log" | ||||||
| "math" | ||||||
| "math/rand" | ||||||
|
|
||||||
| "github.com/golang/freetype" | ||||||
| "github.com/golang/freetype/truetype" | ||||||
| "golang.org/x/image/font" | ||||||
|
mojocn marked this conversation as resolved.
|
||||||
| ) | ||||||
|
|
||||||
| //ItemChar captcha item of unicode characters | ||||||
| // ItemChar captcha item of unicode characters | ||||||
| type ItemChar struct { | ||||||
| bgColor color.Color | ||||||
| width int | ||||||
| height int | ||||||
| nrgba *image.NRGBA | ||||||
| } | ||||||
|
|
||||||
| //NewItemChar creates a captcha item of characters | ||||||
| // NewItemChar creates a captcha item of characters | ||||||
| func NewItemChar(w int, h int, bgColor color.RGBA) *ItemChar { | ||||||
| d := ItemChar{width: w, height: h} | ||||||
| m := image.NewNRGBA(image.Rect(0, 0, w, h)) | ||||||
|
|
@@ -35,18 +36,18 @@ func NewItemChar(w int, h int, bgColor color.RGBA) *ItemChar { | |||||
| return &d | ||||||
| } | ||||||
|
|
||||||
| //drawHollowLine draw strong and bold white line. | ||||||
| // drawHollowLine draw strong and bold white line. | ||||||
| func (item *ItemChar) drawHollowLine() *ItemChar { | ||||||
|
|
||||||
| first := item.width / 20 | ||||||
| end := first * 19 | ||||||
|
|
||||||
| lineColor := RandLightColor() | ||||||
|
|
||||||
| x1 := float64(rand.Intn(first)) | ||||||
| //y1 := float64(rand.Intn(y)+y); | ||||||
| x1 := float64(randIntn(first)) | ||||||
| //y1 := float64(randIntn(y)+y); | ||||||
|
mojocn marked this conversation as resolved.
|
||||||
|
|
||||||
|
Comment on lines
+48
to
49
|
||||||
| //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.
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) |
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)) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package base64Captcha | ||
|
|
||
| import "math/rand" | ||
|
|
||
| func randIntn(n int) int { | ||
| if n > 0 { | ||
| return rand.Intn(n) | ||
| } | ||
| return 0 | ||
| } |
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 saferandIntn()wrapper. This should be updated torandIntn(10)for consistency with the rest of the changes.