Always validate parameters to ReadBit/EnableBit/DisableBit before using them.

This commit is contained in:
the_fiddler 2010-10-28 08:14:28 +00:00
parent 76db3e188f
commit 821a8e1117
2 changed files with 72 additions and 15 deletions

View file

@ -39,7 +39,8 @@ namespace OpenTK.Input
#region Fields
// Allocate enough ints to store all keyboard keys
const int NumInts = ((int)Key.LastKey + 31) / 32;
const int IntSize = sizeof(int);
const int NumInts = ((int)Key.LastKey + IntSize - 1) / IntSize;
// The following line triggers bogus CS0214 in gmcs 2.0.1, sigh...
unsafe fixed int Keys[NumInts];
@ -47,6 +48,24 @@ namespace OpenTK.Input
#region Public Members
/// <summary>
/// Gets a <see cref="System.Boolean"/> indicating whether the specified
/// <see cref="OpenTK.Input.Key"/> is pressed.
/// </summary>
/// <param name="key">The <see cref="OpenTK.Input.Key"/> to check.</param>
/// <returns>True if key is pressed; false otherwise.</returns>
public bool this[Key key]
{
get { return IsKeyDown(key); }
internal set
{
if (value)
EnableBit((int)key);
else
DisableBit((int)key);
}
}
/// <summary>
/// Gets a <see cref="System.Boolean"/> indicating whether this key is down.
/// </summary>
@ -71,6 +90,8 @@ namespace OpenTK.Input
internal bool ReadBit(int offset)
{
ValidateOffset(offset);
int int_offset = offset / 32;
int bit_offset = offset % 32;
unsafe
@ -84,6 +105,8 @@ namespace OpenTK.Input
internal void EnableBit(int offset)
{
ValidateOffset(offset);
int int_offset = offset / 32;
int bit_offset = offset % 32;
unsafe
@ -97,6 +120,8 @@ namespace OpenTK.Input
internal void DisableBit(int offset)
{
ValidateOffset(offset);
int int_offset = offset / 32;
int bit_offset = offset % 32;
unsafe
@ -110,6 +135,16 @@ namespace OpenTK.Input
#endregion
#region Private Members
static void ValidateOffset(int offset)
{
if (offset < 0 || offset >= NumInts * IntSize)
throw new ArgumentOutOfRangeException("offset");
}
#endregion
#region IEquatable<KeyboardState> Members
/// <summary>

View file

@ -39,7 +39,8 @@ namespace OpenTK.Input
#region Fields
// Allocate enough ints to store all mouse buttons
const int NumInts = ((int)MouseButton.LastButton + 31) / 32;
const int IntSize = sizeof(int);
const int NumInts = ((int)MouseButton.LastButton + IntSize - 1) / IntSize;
// The following line triggers bogus CS0214 in gmcs 2.0.1, sigh...
unsafe fixed int Buttons[NumInts];
int x, y;
@ -49,6 +50,24 @@ namespace OpenTK.Input
#region Public Members
/// <summary>
/// Gets a <see cref="System.Boolean"/> indicating whether the specified
/// <see cref="OpenTK.Input.MouseButton"/> is pressed.
/// </summary>
/// <param name="button">The <see cref="OpenTK.Input.MouseButton"/> to check.</param>
/// <returns>True if key is pressed; false otherwise.</returns>
public bool this[MouseButton button]
{
get { return IsButtonDown(button); }
internal set
{
if (value)
EnableBit((int)button);
else
DisableBit((int)button);
}
}
/// <summary>
/// Gets a <see cref="System.Boolean"/> indicating whether this button is down.
/// </summary>
@ -106,19 +125,6 @@ namespace OpenTK.Input
internal set { y = value; }
}
/// <summary>
/// Gets a System.Boolean indicating the state of the specified MouseButton.
/// </summary>
/// <param name="button">The MouseButton to check.</param>
/// <returns>True if the MouseButton is pressed, false otherwise.</returns>
public bool this[MouseButton button]
{
get
{
return IsButtonDown(button);
}
}
/// <summary>
/// Gets a <see cref="System.Boolean" indicating whether the left mouse button is pressed.
/// This property is intended for XNA compatibility.
@ -251,6 +257,8 @@ namespace OpenTK.Input
internal bool ReadBit(int offset)
{
ValidateOffset(offset);
int int_offset = offset / 32;
int bit_offset = offset % 32;
unsafe
@ -264,6 +272,8 @@ namespace OpenTK.Input
internal void EnableBit(int offset)
{
ValidateOffset(offset);
int int_offset = offset / 32;
int bit_offset = offset % 32;
unsafe
@ -277,6 +287,8 @@ namespace OpenTK.Input
internal void DisableBit(int offset)
{
ValidateOffset(offset);
int int_offset = offset / 32;
int bit_offset = offset % 32;
unsafe
@ -290,6 +302,16 @@ namespace OpenTK.Input
#endregion
#region Private Members
static void ValidateOffset(int offset)
{
if (offset < 0 || offset >= NumInts * IntSize)
throw new ArgumentOutOfRangeException("offset");
}
#endregion
#region IEquatable<MouseState> Members
/// <summary>