Fix an edge case that causes an infinite loop#1
Open
ashak wants to merge 1 commit into
Open
Conversation
If the number of items in your list is less than the number of items that can be displayed on screen AND you have the option for centering the highlighted item on screen enabled it causes an infinite loop as soon as I try to move from item 0 to item 1 When I move from item 0 to item 1, the variables become: para->low.i=1 para->high.i=1 items_per_screen=3 So the conditional works out like this: para->low.i-item_per_screen/2+item_per_screen>para->high.i 1-3/2+3 With integer division in c++ I think that becomes: 1-1+3 = 3 Therefore 3 > 1, and therefore the result is true Because thats true, _first_item then gets set like this: _first_item=para->high.i+1-item_per_screen; 1+1-3, which means _first_item = 255. I couldn't then work out why the following for loop loops infinitely, but some debugging suggests that it does. So I would imagine i'm misunderstanding something around how _first_item+item_per_screen is working in this instance. If the for loop is for (byte i=_first_item;i<_first_item+item_per_screen;i++) i=255 I would expect _first_item+item_per_screen to be 255 + 3, which presumably would equal 2... The test is 255 < 2, which I would expect to fail and for the contents of thw loop never to be executed. But that's not what happens This change ensures that _first_item can't be set in this way if the number of items in the list is less than the number of items that can be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
If the number of items in your list is less than the number of items
that can be displayed on screen AND you have the option for
centering the highlighted item on screen enabled it causes an infinite
loop as soon as I try to move from item 0 to item 1
When I move from item 0 to item 1, the variables become:
para->low.i=1
para->high.i=1
items_per_screen=3
So the conditional works out like this:
para->low.i-item_per_screen/2+item_per_screen>para->high.i
1-3/2+3
With integer division in c++ I think that becomes:
1-1+3 = 3
Therefore 3 > 1, and therefore the result is true
Because thats true, _first_item then gets set like this:
_first_item=para->high.i+1-item_per_screen;
1+1-3, which means _first_item = 255.
I couldn't then work out why the following for loop loops infinitely,
but some debugging suggests that it does. So I would imagine i'm
misunderstanding something around how _first_item+item_per_screen is
working in this instance.
If the for loop is
for (byte i=_first_item;i<_first_item+item_per_screen;i++)
i=255
I would expect _first_item+item_per_screen to be
255 + 3, which presumably would equal 2...
The test is 255 < 2, which I would expect to fail and for the contents
of thw loop never to be executed. But that's not what happens
This change ensures that _first_item can't be set in this way if the
number of items in the list is less than the number of items that
can be displayed.