Program: 2919
Project: 2919
Buggy source:
@Override protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec){
  if (mDirtyHierarchy) {
    mDirtyHierarchy=false;
    sortChildren();
  }
  int myWidth=-1;
  int myHeight=-1;
  int width=0;
  int height=0;
  final int widthMode=MeasureSpec.getMode(widthMeasureSpec);
  final int heightMode=MeasureSpec.getMode(heightMeasureSpec);
  final int widthSize=MeasureSpec.getSize(widthMeasureSpec);
  final int heightSize=MeasureSpec.getSize(heightMeasureSpec);
  if (widthMode != MeasureSpec.UNSPECIFIED) {
    myWidth=widthSize;
  }
  if (heightMode != MeasureSpec.UNSPECIFIED) {
    myHeight=heightSize;
  }
  if (widthMode == MeasureSpec.EXACTLY) {
    width=myWidth;
  }
  if (heightMode == MeasureSpec.EXACTLY) {
    height=myHeight;
  }
  mHasBaselineAlignedChild=false;
  View ignore=null;
  int gravity=mGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK;
  final boolean horizontalGravity=gravity != Gravity.START && gravity != 0;
  gravity=mGravity & Gravity.VERTICAL_GRAVITY_MASK;
  final boolean verticalGravity=gravity != Gravity.TOP && gravity != 0;
  int left=Integer.MAX_VALUE;
  int top=Integer.MAX_VALUE;
  int right=Integer.MIN_VALUE;
  int bottom=Integer.MIN_VALUE;
  boolean offsetHorizontalAxis=false;
  boolean offsetVerticalAxis=false;
  if ((horizontalGravity || verticalGravity) && mIgnoreGravity != View.NO_ID) {
    ignore=findViewById(mIgnoreGravity);
  }
  final boolean isWrapContentWidth=widthMode != MeasureSpec.EXACTLY;
  final boolean isWrapContentHeight=heightMode != MeasureSpec.EXACTLY;
  View[] views=mSortedHorizontalChildren;
  int count=views.length;
  if (isLayoutRtl() && (myWidth == -1 || isWrapContentWidth)) {
    int w=getPaddingStart() + getPaddingEnd();
    for (int i=0; i < count; i++) {
      View child=views[i];
      if (child.getVisibility() != GONE) {
        LayoutParams params=(LayoutParams)child.getLayoutParams();
        int[] rules=params.getRules(View.LAYOUT_DIRECTION_LTR);
        applyHorizontalSizeRules(params,myWidth,rules);
        measureChildHorizontal(child,params,-1,myHeight);
        w+=child.getMeasuredWidth();
        w+=params.leftMargin + params.rightMargin;
      }
    }
    if (myWidth == -1) {
      myWidth=w;
    }
 else {
      if (w > 0) {
        myWidth=Math.min(myWidth,w);
      }
    }
  }
  final int layoutDirection=getLayoutDirection();
  for (int i=0; i < count; i++) {
    View child=views[i];
    if (child.getVisibility() != GONE) {
      LayoutParams params=(LayoutParams)child.getLayoutParams();
      int[] rules=params.getRules(layoutDirection);
      applyHorizontalSizeRules(params,myWidth,rules);
      measureChildHorizontal(child,params,myWidth,myHeight);
      if (positionChildHorizontal(child,params,myWidth,isWrapContentWidth)) {
        offsetHorizontalAxis=true;
      }
    }
  }
  views=mSortedVerticalChildren;
  count=views.length;
  for (int i=0; i < count; i++) {
    View child=views[i];
    if (child.getVisibility() != GONE) {
      LayoutParams params=(LayoutParams)child.getLayoutParams();
      applyVerticalSizeRules(params,myHeight);
      measureChild(child,params,myWidth,myHeight);
      if (positionChildVertical(child,params,myHeight,isWrapContentHeight)) {
        offsetVerticalAxis=true;
      }
      if (isWrapContentWidth) {
        width=Math.max(width,params.mRight);
      }
      if (isWrapContentHeight) {
        height=Math.max(height,params.mBottom);
      }
      if (child != ignore || verticalGravity) {
        left=Math.min(left,params.mLeft - params.leftMargin);
        top=Math.min(top,params.mTop - params.topMargin);
      }
      if (child != ignore || horizontalGravity) {
        right=Math.max(right,params.mRight + params.rightMargin);
        bottom=Math.max(bottom,params.mBottom + params.bottomMargin);
      }
    }
  }
  if (mHasBaselineAlignedChild) {
    for (int i=0; i < count; i++) {
      View child=getChildAt(i);
      if (child.getVisibility() != GONE) {
        LayoutParams params=(LayoutParams)child.getLayoutParams();
        alignBaseline(child,params);
        if (child != ignore || verticalGravity) {
          left=Math.min(left,params.mLeft - params.leftMargin);
          top=Math.min(top,params.mTop - params.topMargin);
        }
        if (child != ignore || horizontalGravity) {
          right=Math.max(right,params.mRight + params.rightMargin);
          bottom=Math.max(bottom,params.mBottom + params.bottomMargin);
        }
      }
    }
  }
  if (isWrapContentWidth) {
    width+=mPaddingRight;
    if (mLayoutParams.width >= 0) {
      width=Math.max(width,mLayoutParams.width);
    }
    width=Math.max(width,getSuggestedMinimumWidth());
    width=resolveSize(width,widthMeasureSpec);
    if (offsetHorizontalAxis) {
      for (int i=0; i < count; i++) {
        View child=getChildAt(i);
        if (child.getVisibility() != GONE) {
          LayoutParams params=(LayoutParams)child.getLayoutParams();
          final int[] rules=params.getRules(layoutDirection);
          if (rules[CENTER_IN_PARENT] != 0 || rules[CENTER_HORIZONTAL] != 0) {
            centerHorizontal(child,params,width);
          }
 else           if (rules[ALIGN_PARENT_RIGHT] != 0) {
            final int childWidth=child.getMeasuredWidth();
            params.mLeft=width - mPaddingRight - childWidth;
            params.mRight=params.mLeft + childWidth;
          }
        }
      }
    }
  }
  if (isWrapContentHeight) {
    height+=mPaddingBottom;
    if (mLayoutParams.height >= 0) {
      height=Math.max(height,mLayoutParams.height);
    }
    height=Math.max(height,getSuggestedMinimumHeight());
    height=resolveSize(height,heightMeasureSpec);
    if (offsetVerticalAxis) {
      for (int i=0; i < count; i++) {
        View child=getChildAt(i);
        if (child.getVisibility() != GONE) {
          LayoutParams params=(LayoutParams)child.getLayoutParams();
          final int[] rules=params.getRules(layoutDirection);
          if (rules[CENTER_IN_PARENT] != 0 || rules[CENTER_VERTICAL] != 0) {
            centerVertical(child,params,height);
          }
 else           if (rules[ALIGN_PARENT_BOTTOM] != 0) {
            final int childHeight=child.getMeasuredHeight();
            params.mTop=height - mPaddingBottom - childHeight;
            params.mBottom=params.mTop + childHeight;
          }
        }
      }
    }
  }
  if (horizontalGravity || verticalGravity) {
    final Rect selfBounds=mSelfBounds;
    selfBounds.set(mPaddingLeft,mPaddingTop,width - mPaddingRight,height - mPaddingBottom);
    final Rect contentBounds=mContentBounds;
    Gravity.apply(mGravity,right - left,bottom - top,selfBounds,contentBounds,layoutDirection);
    final int horizontalOffset=contentBounds.left - left;
    final int verticalOffset=contentBounds.top - top;
    if (horizontalOffset != 0 || verticalOffset != 0) {
      for (int i=0; i < count; i++) {
        View child=getChildAt(i);
        if (child.getVisibility() != GONE && child != ignore) {
          LayoutParams params=(LayoutParams)child.getLayoutParams();
          if (horizontalGravity) {
            params.mLeft+=horizontalOffset;
            params.mRight+=horizontalOffset;
          }
          if (verticalGravity) {
            params.mTop+=verticalOffset;
            params.mBottom+=verticalOffset;
          }
        }
      }
    }
  }
  setMeasuredDimension(width,height);
}

Fixed version (ground truth):
@Override protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec){
  if (mDirtyHierarchy) {
    mDirtyHierarchy=false;
    sortChildren();
  }
  int myWidth=-1;
  int myHeight=-1;
  int width=0;
  int height=0;
  final int widthMode=MeasureSpec.getMode(widthMeasureSpec);
  final int heightMode=MeasureSpec.getMode(heightMeasureSpec);
  final int widthSize=MeasureSpec.getSize(widthMeasureSpec);
  final int heightSize=MeasureSpec.getSize(heightMeasureSpec);
  if (widthMode != MeasureSpec.UNSPECIFIED) {
    myWidth=widthSize;
  }
  if (heightMode != MeasureSpec.UNSPECIFIED) {
    myHeight=heightSize;
  }
  if (widthMode == MeasureSpec.EXACTLY) {
    width=myWidth;
  }
  if (heightMode == MeasureSpec.EXACTLY) {
    height=myHeight;
  }
  mHasBaselineAlignedChild=false;
  View ignore=null;
  int gravity=mGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK;
  final boolean horizontalGravity=gravity != Gravity.START && gravity != 0;
  gravity=mGravity & Gravity.VERTICAL_GRAVITY_MASK;
  final boolean verticalGravity=gravity != Gravity.TOP && gravity != 0;
  int left=Integer.MAX_VALUE;
  int top=Integer.MAX_VALUE;
  int right=Integer.MIN_VALUE;
  int bottom=Integer.MIN_VALUE;
  boolean offsetHorizontalAxis=false;
  boolean offsetVerticalAxis=false;
  if ((horizontalGravity || verticalGravity) && mIgnoreGravity != View.NO_ID) {
    ignore=findViewById(mIgnoreGravity);
  }
  final boolean isWrapContentWidth=widthMode != MeasureSpec.EXACTLY;
  final boolean isWrapContentHeight=heightMode != MeasureSpec.EXACTLY;
  final int layoutDirection=getLayoutDirection();
  if (isLayoutRtl() && myWidth == -1) {
    myWidth=mDisplayWidth;
  }
  View[] views=mSortedHorizontalChildren;
  int count=views.length;
  for (int i=0; i < count; i++) {
    View child=views[i];
    if (child.getVisibility() != GONE) {
      LayoutParams params=(LayoutParams)child.getLayoutParams();
      int[] rules=params.getRules(layoutDirection);
      applyHorizontalSizeRules(params,myWidth,rules);
      measureChildHorizontal(child,params,myWidth,myHeight);
      if (positionChildHorizontal(child,params,myWidth,isWrapContentWidth)) {
        offsetHorizontalAxis=true;
      }
    }
  }
  views=mSortedVerticalChildren;
  count=views.length;
  for (int i=0; i < count; i++) {
    View child=views[i];
    if (child.getVisibility() != GONE) {
      LayoutParams params=(LayoutParams)child.getLayoutParams();
      applyVerticalSizeRules(params,myHeight);
      measureChild(child,params,myWidth,myHeight);
      if (positionChildVertical(child,params,myHeight,isWrapContentHeight)) {
        offsetVerticalAxis=true;
      }
      if (isWrapContentWidth) {
        if (isLayoutRtl()) {
          width=Math.max(width,myWidth - params.mLeft);
        }
 else {
          width=Math.max(width,params.mRight);
        }
      }
      if (isWrapContentHeight) {
        height=Math.max(height,params.mBottom);
      }
      if (child != ignore || verticalGravity) {
        left=Math.min(left,params.mLeft - params.leftMargin);
        top=Math.min(top,params.mTop - params.topMargin);
      }
      if (child != ignore || horizontalGravity) {
        right=Math.max(right,params.mRight + params.rightMargin);
        bottom=Math.max(bottom,params.mBottom + params.bottomMargin);
      }
    }
  }
  if (mHasBaselineAlignedChild) {
    for (int i=0; i < count; i++) {
      View child=getChildAt(i);
      if (child.getVisibility() != GONE) {
        LayoutParams params=(LayoutParams)child.getLayoutParams();
        alignBaseline(child,params);
        if (child != ignore || verticalGravity) {
          left=Math.min(left,params.mLeft - params.leftMargin);
          top=Math.min(top,params.mTop - params.topMargin);
        }
        if (child != ignore || horizontalGravity) {
          right=Math.max(right,params.mRight + params.rightMargin);
          bottom=Math.max(bottom,params.mBottom + params.bottomMargin);
        }
      }
    }
  }
  if (isWrapContentWidth) {
    width+=mPaddingRight;
    if (mLayoutParams.width >= 0) {
      width=Math.max(width,mLayoutParams.width);
    }
    width=Math.max(width,getSuggestedMinimumWidth());
    width=resolveSize(width,widthMeasureSpec);
    if (offsetHorizontalAxis) {
      for (int i=0; i < count; i++) {
        View child=getChildAt(i);
        if (child.getVisibility() != GONE) {
          LayoutParams params=(LayoutParams)child.getLayoutParams();
          final int[] rules=params.getRules(layoutDirection);
          if (rules[CENTER_IN_PARENT] != 0 || rules[CENTER_HORIZONTAL] != 0) {
            centerHorizontal(child,params,width);
          }
 else           if (rules[ALIGN_PARENT_RIGHT] != 0) {
            final int childWidth=child.getMeasuredWidth();
            params.mLeft=width - mPaddingRight - childWidth;
            params.mRight=params.mLeft + childWidth;
          }
        }
      }
    }
  }
  if (isWrapContentHeight) {
    height+=mPaddingBottom;
    if (mLayoutParams.height >= 0) {
      height=Math.max(height,mLayoutParams.height);
    }
    height=Math.max(height,getSuggestedMinimumHeight());
    height=resolveSize(height,heightMeasureSpec);
    if (offsetVerticalAxis) {
      for (int i=0; i < count; i++) {
        View child=getChildAt(i);
        if (child.getVisibility() != GONE) {
          LayoutParams params=(LayoutParams)child.getLayoutParams();
          final int[] rules=params.getRules(layoutDirection);
          if (rules[CENTER_IN_PARENT] != 0 || rules[CENTER_VERTICAL] != 0) {
            centerVertical(child,params,height);
          }
 else           if (rules[ALIGN_PARENT_BOTTOM] != 0) {
            final int childHeight=child.getMeasuredHeight();
            params.mTop=height - mPaddingBottom - childHeight;
            params.mBottom=params.mTop + childHeight;
          }
        }
      }
    }
  }
  if (horizontalGravity || verticalGravity) {
    final Rect selfBounds=mSelfBounds;
    selfBounds.set(mPaddingLeft,mPaddingTop,width - mPaddingRight,height - mPaddingBottom);
    final Rect contentBounds=mContentBounds;
    Gravity.apply(mGravity,right - left,bottom - top,selfBounds,contentBounds,layoutDirection);
    final int horizontalOffset=contentBounds.left - left;
    final int verticalOffset=contentBounds.top - top;
    if (horizontalOffset != 0 || verticalOffset != 0) {
      for (int i=0; i < count; i++) {
        View child=getChildAt(i);
        if (child.getVisibility() != GONE && child != ignore) {
          LayoutParams params=(LayoutParams)child.getLayoutParams();
          if (horizontalGravity) {
            params.mLeft+=horizontalOffset;
            params.mRight+=horizontalOffset;
          }
          if (verticalGravity) {
            params.mTop+=verticalOffset;
            params.mBottom+=verticalOffset;
          }
        }
      }
    }
  }
  if (isLayoutRtl()) {
    final int offsetWidth=myWidth - width;
    for (int i=0; i < count; i++) {
      View child=getChildAt(i);
      if (child.getVisibility() != GONE) {
        LayoutParams params=(LayoutParams)child.getLayoutParams();
        params.mLeft-=offsetWidth;
        params.mRight-=offsetWidth;
      }
    }
  }
  setMeasuredDimension(width,height);
}