Skip to content

Conditional order of precedence bug in ScrollableControl.SetDisplayRectLocation #1955

Closed

Description

@hughbe

From the code in

if (xDelta != 0 || yDelta != 0 && IsHandleCreated)
{
RECT rcClip = ClientRectangle;
RECT rcUpdate = ClientRectangle;
SafeNativeMethods.ScrollWindowEx(new HandleRef(this, Handle), xDelta, yDelta,
null,
ref rcClip,
NativeMethods.NullHandleRef,
ref rcUpdate,
NativeMethods.SW_INVALIDATE
| NativeMethods.SW_ERASE
| NativeMethods.SW_SCROLLCHILDREN);
}

The conditional check if (xDelta != 0 || yDelta != 0 && IsHandleCreated) has incorrect bracketing and order of precedence.

It should be if ((xDelta != 0 || yDelta != 0) && IsHandleCreated).

This has observable differences in behaviour - see #1920 (https://dotnetfiddle.net/EVSyDY)

Fixing this bug would have the following implications

  • We don't create a Handle if xDelta != 0 and IsHandleCreated == true

Activity

  1. RussKie commented on Sep 24, 2019

    @RussKie
    Contributor

    @sharwell any thoughts?

  2. sharwell commented on Sep 24, 2019

    @sharwell
    Contributor

    @RussKie SA1408 should report this, yes. Is the rule disabled?

  3. sharwell commented on Sep 24, 2019

    @sharwell
    Contributor

    <Rule Id="SA1408" Action="None" />

    I believe you can also get this behavior from a built-in rule:

    dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity:warning
    dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:warning
    dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity:warning
    
  4. RussKie commented on Sep 24, 2019

    @RussKie
    Contributor

    Thank you Sam.
    Any other rules we should enable?

  5. sharwell commented on Sep 25, 2019

    @sharwell
    Contributor

    @RussKie That's entirely up to the individual project. If you have any questions about the applicability of specific rules I'm happy to help 👍

  6. self-assigned this
    on Sep 25, 2019
  7. RussKie commented on Sep 26, 2019

    @RussKie
    Contributor

    It is easy to break SetDisplayRectLocation_InvokeWithoutHandle_Success test by adding an Debug.Assert(IsHandleCreated, "Handle is not created") in the condition.
    So it is a 🐛

  8. hughbe commented on Sep 26, 2019

    @hughbe
    ContributorAuthor

    Aye.. I can fix this once #1920 is merged

  9. RussKie commented on Sep 26, 2019

    @RussKie
    Contributor

    I'm looking at all violations of SA1408, so leave this with me.

  10. added
    🪲 bug
    on Sep 26, 2019
  11. RussKie commented on Sep 26, 2019

    @RussKie
    Contributor

    Looking further at the code I'm wondering - would a child control have its handle created if its parent control doesn't have a handle? Is it a valid scenario?
    If not, we can optimise SetDisplayRectLocation method to fast exit if IsHandleCreated isn't created:

    if (xDelta != 0 || yDelta != 0 && IsHandleCreated)
    {
    RECT rcClip = ClientRectangle;
    RECT rcUpdate = ClientRectangle;
    SafeNativeMethods.ScrollWindowEx(new HandleRef(this, Handle), xDelta, yDelta,
    null,
    ref rcClip,
    NativeMethods.NullHandleRef,
    ref rcUpdate,
    NativeMethods.SW_INVALIDATE
    | NativeMethods.SW_ERASE
    | NativeMethods.SW_SCROLLCHILDREN);
    }
    // Force child controls to update bounds.
    for (int i = 0; i < Controls.Count; i++)
    {
    Control ctl = Controls[i];
    if (ctl != null && ctl.IsHandleCreated)
    {
    ctl.UpdateBounds();
    }
    }

    @JeremyKuhne @Tanya-Solyanik @dreddy-work what do you think?

  12. weltkante commented on Sep 26, 2019

    @weltkante
    Contributor

    would a child control have its handle created if its parent control doesn't have a handle? Is it a valid scenario?

    In Desktop Framework this apparently was possible and there is code to deal with that, so I'd assume this is still possible in Core.

    Basically, if some user code forces the child window handle to be created it will be created hidden and parented to a "parking window". Later when the real parent is actually created the child will be moved over. I don't know if the comment (or my interpretation thereof) is correct but thats how I read it.

    However, considering that a child would be parented to the parking window, I don't know how critical it is to call its UpdateBounds in the first place, maybe its safe to skip when the parent is not created.

  13. RussKie commented on Sep 27, 2019

    @RussKie
    Contributor

    Thank you Tobias.

  14. ghost added
    🚧 work in progress
    on Sep 27, 2019
  15. modified the milestones: Future, 5.0 on Oct 3, 2019
  16. added a commit that references this issue on Oct 10, 2019
    cff38a4
  17. removed this from the 5.0 Preview 1- 4 milestone on Apr 20, 2020
  18. removed
    🚧 work in progress
    on Apr 20, 2020
  19. ghost locked as resolved and limited conversation to collaborators on Feb 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

🪲 bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions