Skip to content

Displayio Bitmap arg validation #7548

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

Merged
merged 9 commits into from
Mar 28, 2023

Conversation

FoamyGuy
Copy link
Collaborator

@FoamyGuy FoamyGuy commented Feb 4, 2023

This change includes validation for arguments to Bitmap constructor, fill(), __get_item__ and __set_item__ to raise errors if users pass in negative values for things that can't really be negative.

This resolves: #7540 as well as a few similar issues on other functions and properties in Bitmap class

Copy link

@gneverov gneverov left a comment

Choose a reason for hiding this comment

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

In displayio_bitmap_obj_blit, the use of int16_t truncates the high bits of the input. So if the caller passed 65,537, it would actually get interpreted as 1. You should probably replace all these occurences with int32_t or mp_int_t.

@FoamyGuy
Copy link
Collaborator Author

FoamyGuy commented Feb 6, 2023

In displayio_bitmap_obj_blit, the use of int16_t truncates the high bits of the input. So if the caller passed 65,537, it would actually get interpreted as 1. You should probably replace all these occurences with int32_t or mp_int_t.

I wonder if we could use the arg_check_max function to limit the valid values that can be passed rather than using a larger int size to accommodate the larger values. I think it's possible a that it wouldn't be able to successfully operate on a Bitmap of that size for other reasons as well.

@gneverov
Copy link

gneverov commented Feb 6, 2023

I wonder if we could use the arg_check_max function to limit the valid values that can be passed rather than using a larger int size to accommodate the larger values. I think it's possible a that it wouldn't be able to successfully operate on a Bitmap of that size for other reasons as well.

There's already range checking code on lines 242-52.

Copy link

@jepler jepler left a comment

Choose a reason for hiding this comment

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

Happy to see someone work on this. I think it can be better still, though.

@FoamyGuy
Copy link
Collaborator Author

There's already range checking code on lines 242-52.

the latest commits contain a change that uses the arg validation functions and removes the seperate logic check. This limits the bounds to a smaller number to get rid of the issue of it going over the max and wrapping around as well as consolidates the check into a single place when it first saves the argument.

@tannewt tannewt requested a review from jepler February 13, 2023 18:26
Copy link

@jepler jepler left a comment

Choose a reason for hiding this comment

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

Thanks. Sadly, we're going to have to do one more iteration of this.

I was wrong when I stated that this was a correct way to get the maximum value, including for 32-bit bitmaps:

(1u << common_hal_displayio_bitmap_get_bits_per_value(self)) - 1

we'll need to define a function to return the maximum bitmap value and then call it in these cases. It'll apparently need a special case for 32-bit bitmaps.

:-/

Copy link

@jepler jepler left a comment

Choose a reason for hiding this comment

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

(see previous comment, I suggested something that was not correct)

@jepler
Copy link

jepler commented Feb 17, 2023

This seems to be correct without any special cases: (UINT32_MAX >> (32-n))

@FoamyGuy
Copy link
Collaborator Author

Thank you! The latest commit changes the maximum value computation to use the new one that you mentioned.

I tested it successfully with this change on a Feather ESP32S2 TFT with both upper and lower bounds for the value input.

@dhalbert dhalbert requested a review from jepler February 21, 2023 14:57
# Conflicts:
#	shared-bindings/displayio/Palette.c
@FoamyGuy
Copy link
Collaborator Author

FoamyGuy commented Mar 4, 2023

This had some conflicts in Palette.c, I merged from main and resolved them.

Copy link
Collaborator

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

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

It looks to me like the remaining points have been addressed. Thanks @FoamyGuy and reviewers.

@dhalbert
Copy link
Collaborator

@jepler Do you have time to re-review?

Copy link

@jepler jepler left a comment

Choose a reason for hiding this comment

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

thanks

@dhalbert dhalbert merged commit 29b3580 into adafruit:main Mar 28, 2023
@tannewt
Copy link
Member

tannewt commented Apr 18, 2023

FYI this breaks turtle because it changes subscr errors from IndexError to ValueError. I'll look at fixing 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.

displayio.Bitmap creation does not validate fo negative values
5 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy