Program: 111596
Project: 111596
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;
  }
  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);
}

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) {
    if (horizSwap) {
      w+=x;
    }
    x=0;
  }
  if (y < 0) {
    if (vertSwap) {
      h+=y;
    }
    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);
}