Skip to content
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

xpm: Fix various parser panics #7

Merged
merged 11 commits into from
Jan 21, 2024
Merged

xpm: Fix various parser panics #7

merged 11 commits into from
Jan 21, 2024

Conversation

gnoack
Copy link
Contributor

@gnoack gnoack commented Jan 19, 2024

This series of patches adds subtests to test input files against golden PNGs, a fuzzer, and then various changes to fix the detected panics in the parser.

@gnoack gnoack force-pushed the picons2 branch 2 times, most recently from 8bda740 to 89f34d3 Compare January 19, 2024 21:04
Copy link
Member

@Jacalz Jacalz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. A very nice change. Added some notes inline. Mostly about error handling.

go.mod Outdated
@@ -1,8 +1,14 @@
module github.com/fyne-io/image

go 1.14
go 1.18
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The upcoming version of Fyne is targeting 1.19 as baseline. You might as well bump it once more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, will add a commit for that which you can squash with 5bdced5

xpm/xpm.go Outdated
if len(data) < charSize {
return
return "", nil, fmt.Errorf("invalid format: missing color specification")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are not using any formatting. Consider using errors.New() instead. You might also want to bring it out to a global error variable given that the same error is used in multiple places.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I introduced an ErrInvalidFormat and return fmt.Errorf("%w: ...") in the error cases, so that callers can tell it apart.

xpm/xpm.go Outdated
return
}
if ncolors <= 0 {
err = fmt.Errorf("invalid format: ncolors <= 0: missing color palette")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using errors.New().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using fmt.Errorf("%w: ...", ErrInvalidFormat) (see comment above)

xpm/xpm.go Outdated
if err != nil {
return
}
j, err = strconv.Atoi(parts[3])
if cpp <= 0 {
err = fmt.Errorf("invalid format: characters per pixel <= 0")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using errors.New().

xpm/xpm.go Outdated
func parsePixels(row string, charSize int, pixRow int, colors map[string]color.Color, img *image.NRGBA) {
func parsePixels(row string, charSize int, pixRow int, colors map[string]color.Color, img *image.NRGBA) error {
if len(row) < charSize*(img.Stride/4) {
return fmt.Errorf("invalid format: missing pixel data")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using errors.New().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using fmt.Errorf("%w: ...", ErrInvalidFormat) (see comment above)

xpm/xpm.go Outdated
off := pixRow * img.Stride
if len(img.Pix) < off+img.Stride {
return fmt.Errorf("invalid format: too much pixel data")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using errors.New().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using fmt.Errorf("%w: ...", ErrInvalidFormat) (see comment above)

// parser. To run, use:
//
// go test -fuzz=FuzzParsePanic
func FuzzParsePanic(f *testing.F) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be ok to run fuzz automatically everyday in a github action. I usually pick 4am as that's when the activity on fyne repository are at their lowest. If you do not mind adding a new action, I think that will be useful to run this fuzzing regularly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fuzzing runs indefinitely if you let it, and I'm not sure whether Github actions are the best fit there. (TBH, I'm not terribly familiar with Github actions either.) I suspect that these considerations would inflate the scope of this change too much. I'd rather leave it up to you to configure this, if that's OK with you. :)

Fixes fyne-io#7 (comment)

(Should be a fixup on commit 5bdced5.)
We return fmt.Errorf("%w: ...") in the error cases,
so callers can tell the error apart using `errors.Is()` and
`errors.As()`.
@gnoack gnoack requested a review from Jacalz January 20, 2024 14:47
@gnoack
Copy link
Contributor Author

gnoack commented Jan 20, 2024

Thanks. A very nice change. Added some notes inline. Mostly about error handling.

Thanks for the review. I added some commits on top to address these; you might want to squash the go.mod change onto 5bdced5 before merging. Please take another look.

Copy link
Member

@andydotxyz andydotxyz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice addition of safety thanks :)

@Jacalz Jacalz merged commit c3c798e into fyne-io:main Jan 21, 2024
3 checks passed
@Jacalz
Copy link
Member

Jacalz commented Jan 21, 2024

Thanks for working on this :)

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.

4 participants