Program: 111595
Project: 111595
Buggy source:
/** 
 * Get current selection.
 * @param useScreenCoords use screen coordinates if true, or framebuffercoordinates if false. This makes difference when scaling factor is not 100.
 * @return The selection as a {@link Rectangle}.
 */
private synchronized Rectangle getSelection(boolean useScreenCoords){
  int x=selectionStart.x;
  int y=selectionStart.y;
  int w=selectionEnd.x - selectionStart.x;
  int h=selectionEnd.y - selectionStart.y;
  boolean horizSwap=false;
  boolean vertSwap=false;
  if (w < 0) {
    w=-w;
    x=x - w;
    horizSwap=true;
  }
  if (h < 0) {
    h=-h;
    y=y - h;
    vertSwap=true;
  }
  if (w > 0 && h > 0) {
    w+=1;
    h+=1;
  }
  if (rfb.framebufferWidth != scaledWidth) {
    x=(x * 100 + scalingFactor / 2) / scalingFactor;
    y=(y * 100 + scalingFactor / 2) / scalingFactor;
    w=(w * 100 + scalingFactor / 2) / scalingFactor;
    h=(h * 100 + scalingFactor / 2) / scalingFactor;
  }
  int widthCorrection=w % 16;
  if (widthCorrection >= 8) {
    widthCorrection-=16;
  }
  w-=widthCorrection;
  if (horizSwap) {
    x+=widthCorrection;
  }
  int heightCorrection=h % 8;
  if (heightCorrection >= 4) {
    heightCorrection-=8;
  }
  h-=heightCorrection;
  if (vertSwap) {
    y+=heightCorrection;
  }
  int clipWidth=rfb.framebufferWidth;
  int clipHeight=rfb.framebufferHeight;
  if (useScreenCoords && rfb.framebufferWidth != scaledWidth) {
    x=(x * scalingFactor + 50) / 100;
    y=(y * scalingFactor + 50) / 100;
    w=(w * scalingFactor + 50) / 100;
    h=(h * scalingFactor + 50) / 100;
    clipWidth=scaledWidth;
    clipHeight=scaledHeight;
  }
  Rectangle selection=new Rectangle(x,y,w,h);
  Rectangle clip=new Rectangle(0,0,clipWidth,clipHeight);
  return selection.intersection(clip);
}

Fixed version (ground truth):
/** 
 * Get current selection.
 * @param useScreenCoords use screen coordinates if true, or framebuffercoordinates if false. This makes difference when scaling factor is not 100.
 * @return The selection as a {@link Rectangle}.
 */
private synchronized Rectangle getSelection(boolean useScreenCoords){
  int x=selectionStart.x;
  int y=selectionStart.y;
  int w=selectionEnd.x - selectionStart.x;
  int h=selectionEnd.y - selectionStart.y;
  boolean horizSwap=false;
  boolean vertSwap=false;
  if (w < 0) {
    w=-w;
    x=x - w;
    horizSwap=true;
  }
  if (h < 0) {
    h=-h;
    y=y - h;
    vertSwap=true;
  }
  if (w > 0 && h > 0) {
    w+=1;
    h+=1;
  }
  if (rfb.framebufferWidth != scaledWidth) {
    x=(x * 100 + scalingFactor / 2) / scalingFactor;
    y=(y * 100 + scalingFactor / 2) / scalingFactor;
    w=(w * 100 + scalingFactor / 2) / scalingFactor;
    h=(h * 100 + scalingFactor / 2) / scalingFactor;
  }
  if (x < 0)   x=0;
  if (y < 0)   y=0;
  if (x + w > rfb.framebufferWidth)   w=rfb.framebufferWidth - x;
  if (y + h > rfb.framebufferHeight)   h=rfb.framebufferHeight - y;
  int widthCorrection=w % 16;
  if (widthCorrection >= 8 && x + (w / 16 + 1) * 16 <= rfb.framebufferWidth) {
    widthCorrection-=16;
  }
  w-=widthCorrection;
  if (horizSwap) {
    x+=widthCorrection;
  }
  int heightCorrection=h % 8;
  if (heightCorrection >= 4 && y + (h / 8 + 1) * 8 <= rfb.framebufferHeight) {
    heightCorrection-=8;
  }
  h-=heightCorrection;
  if (vertSwap) {
    y+=heightCorrection;
  }
  if (useScreenCoords && rfb.framebufferWidth != scaledWidth) {
    x=(x * scalingFactor + 50) / 100;
    y=(y * scalingFactor + 50) / 100;
    w=(w * scalingFactor + 50) / 100;
    h=(h * scalingFactor + 50) / 100;
  }
  return new Rectangle(x,y,w,h);
}