Closed Bug 549779 Opened 14 years ago Closed 14 years ago

valgrind: nsMenuPopupFrame::AdjustPositionForAnchorAlign creates undefined nsRects

Categories

(Core :: XUL, defect)

defect
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla1.9.3a3

People

(Reporter: jseward, Assigned: jseward)

References

Details

(Keywords: valgrind)

Attachments

(2 files, 2 obsolete files)

There is a path through layout/xul/base/source/nsMenuPopupFrame.cpp
function nsMenuPopupFrame::AdjustPositionForAnchorAlign which causes
it to return an undefined nsPoint (stack-allocated garbage).  It's
actually very simple.  We have:

783:  nsPoint pnt;
784:  switch (popupAnchor) {
785:    case POPUPALIGNMENT_TOPLEFT:
786:      pnt = anchorRect.TopLeft();
787:      break;
788:    case POPUPALIGNMENT_TOPRIGHT:
789:      pnt = anchorRect.TopRight();
790:      break;
791:    case POPUPALIGNMENT_BOTTOMLEFT:
792:      pnt = anchorRect.BottomLeft();
793:      break;
794:    case POPUPALIGNMENT_BOTTOMRIGHT:
795:      pnt = anchorRect.BottomRight();
796:      break;
797:  }

It then goes on to possibly add/subtract various offsets to pnt,
and then returns it.  That's all well and good, but the problem
is the switch doesn't handle the case POPUPALIGNMENT_NONE, in
which case garbage will be returned.  Adding the case, and a
printf, shows this case does indeed occur.

The bogus nsPoint is returned to the caller, nsMenuPopupFrame::SetPopupPosition,
which eventually hands it onwards via a call to nsMenuPopupFrame::FlipOrResize,
and, I think, various other parts of the graphics system.  Valgrind
tracks it as causing an immense number of errors, 49762 errors from 497 contexts
(different stack tracebacks) in this test case.  These are shown in
the attached file.

The flaw is simple once you see it, but tracking it down from the
presented information proved very difficult.


HOW TO REPRO: run layout/base/tests/test_bug420499.xul
on Memcheck as described in bug 549224.
Blocks: 549224
Keywords: valgrind
Component: Graphics → XP Toolkit/Widgets: Menus
QA Contact: thebes → xptoolkit.menus
Note that the origin of the undefined value is erroneously listed
as being from 
nsMenuPopupFrame::SetPopupPosition(nsIFrame*, int) (nsMenuPopupFrame.cpp:908)
whereas it is really from 
nsMenuPopupFrame::AdjustPositionForAnchorAlign(const nsRect& anchorRect,
                                               PRBool& aHFlip, PRBool& aVFlip)
as discussed above.  I think this happens because the latter got inlined
into the former.
Attached patch proposed fix (obsolete) — Splinter Review
Proposed fix.  Obviously the minimal safe thing to do is give pnt
an initial value of (0,0) so its safe regardless of whatever else
happens.  I took the liberty of adding do-nothing cases for
POPUPALIGNMENT_NONE to show that they are deliberately no-ops
rather than were merely forgotten about.
You can request review from enndeakin.
Attachment #429890 - Flags: review?(enndeakin)
Actually this is fixed by the patch in bug 524545, which has some problems with the test on Mac which I've never figured out. Two options here:

- check in the patch in bug 524545 with the test disabled on Mac
- check in just something like the patch in this bug, however POPUPALIGNMENT_NONE should behave the same as POPUPALIGNMENT_TOPLEFT. The patch in bug 524545 uses a switch default for this.
Two thumbs up for always including 'default' cases in switch statements!
Attached patch patch v2 (obsolete) — Splinter Review
patch revised as per comment 5
Attachment #429890 - Attachment is obsolete: true
Attachment #429890 - Flags: review?(enndeakin)
So much for guessing comment numbers.  That should have been "patch
revised as per comment 4."
Attachment #431498 - Flags: review?(enndeakin)
The intent was to just add a 'default' case, and rely on it for the none value. Really, just the first part of https://bug524545.bugzilla.mozilla.org/attachment.cgi?id=408843
Attachment #431498 - Attachment is obsolete: true
Attachment #431710 - Flags: review?(enndeakin)
Attachment #431498 - Flags: review?(enndeakin)
Attachment #431710 - Flags: review?(enndeakin) → review+
Keywords: checkin-needed
Assignee: nobody → jseward
http://hg.mozilla.org/mozilla-central/rev/581260c2e0d7
Status: NEW → RESOLVED
Closed: 14 years ago
Keywords: checkin-needed
Resolution: --- → FIXED
Target Milestone: --- → mozilla1.9.3a3
Component: XP Toolkit/Widgets: Menus → XUL
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: