monoev3 icon indicating copy to clipboard operation
monoev3 copied to clipboard

Crash with invalid LCD pixel checks (fix included)

Open freds72 opened this issue 8 years ago • 0 comments

LCD screen size is 178x128 pixels, valid values MUST be 0-177/0-127, not 0-178/0-128! Drawing a line/rectangle beyond screen limits no longer crashes. MonoBrickFirmware.Display.EV3Lcd.cs

protected bool IsPixelInLcd(Point pixel)
{
  return (pixel.X >= 0) && (pixel.Y >= 0) && (pixel.X < width) && (pixel.Y < height);
}
protected bool IsPixelInLcd(int x, int y)
{
 return (x >= 0) && (y >= 0) && (x < width) && (y < height);
}

Same EV3 simulator: EV3MonoBrickSimulator.Stub.LcdStub.cs

// new help method
bool IsPixelInLcd(int x, int y)
{
 return (x >= 0) && (y >= 0) && (x < Width) && (y < Height);
}
public bool IsPixelSet(int x, int y)
{
 if (!IsPixelInLcd(x, y)) return false; // boundary check
 int index = GetIndex(x, y);
 return (Marshal.ReadInt32( IntPtr.Add (lcdBuffer.Pixels, index)) & 0x00ffffff) == 0x000000;
}
public void SetPixel(int x, int y)
{
 if (!IsPixelInLcd(x, y)) return; // boundary check

 int index = GetIndex(x, y);
 Int32 oldValue = Marshal.ReadInt32( IntPtr.Add (lcdBuffer.Pixels, index));
 Int32 newValue = (int)(oldValue & 0xff000000);
 Marshal.WriteInt32 (IntPtr.Add (lcdBuffer.Pixels, index), newValue);
}
public void ClearPixel(int x, int y)
{
 if (!IsPixelInLcd(x, y)) return; // boundary check
 int index = GetIndex(x, y);
 int backGroundValue = Marshal.ReadInt32( IntPtr.Add (backGroundPixBuffer.Pixels, index));
 int oldValue = Marshal.ReadInt32( IntPtr.Add (lcdBuffer.Pixels, index));
 int newValue = (int)(oldValue & 0xff000000) | (int)(backGroundValue & 0x00ffffff);
 Marshal.WriteInt32 (IntPtr.Add (lcdBuffer.Pixels, index), newValue);
}

freds72 avatar Nov 30 '15 20:11 freds72