From 821a8e11175c6f402a476dbc4fef75efc64b169c Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Thu, 28 Oct 2010 08:14:28 +0000 Subject: [PATCH] Always validate parameters to ReadBit/EnableBit/DisableBit before using them. --- Source/OpenTK/Input/KeyboardState.cs | 37 +++++++++++++++++++- Source/OpenTK/Input/MouseState.cs | 50 ++++++++++++++++++++-------- 2 files changed, 72 insertions(+), 15 deletions(-) diff --git a/Source/OpenTK/Input/KeyboardState.cs b/Source/OpenTK/Input/KeyboardState.cs index 91c019a8..612dc963 100644 --- a/Source/OpenTK/Input/KeyboardState.cs +++ b/Source/OpenTK/Input/KeyboardState.cs @@ -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 + /// + /// Gets a indicating whether the specified + /// is pressed. + /// + /// The to check. + /// True if key is pressed; false otherwise. + public bool this[Key key] + { + get { return IsKeyDown(key); } + internal set + { + if (value) + EnableBit((int)key); + else + DisableBit((int)key); + } + } + /// /// Gets a indicating whether this key is down. /// @@ -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 Members /// diff --git a/Source/OpenTK/Input/MouseState.cs b/Source/OpenTK/Input/MouseState.cs index e0b5b458..a4449aad 100644 --- a/Source/OpenTK/Input/MouseState.cs +++ b/Source/OpenTK/Input/MouseState.cs @@ -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 + /// + /// Gets a indicating whether the specified + /// is pressed. + /// + /// The to check. + /// True if key is pressed; false otherwise. + public bool this[MouseButton button] + { + get { return IsButtonDown(button); } + internal set + { + if (value) + EnableBit((int)button); + else + DisableBit((int)button); + } + } + /// /// Gets a indicating whether this button is down. /// @@ -106,19 +125,6 @@ namespace OpenTK.Input internal set { y = value; } } - /// - /// Gets a System.Boolean indicating the state of the specified MouseButton. - /// - /// The MouseButton to check. - /// True if the MouseButton is pressed, false otherwise. - public bool this[MouseButton button] - { - get - { - return IsButtonDown(button); - } - } - /// /// Gets a = NumInts * IntSize) + throw new ArgumentOutOfRangeException("offset"); + } + + #endregion + #region IEquatable Members ///