On macOS, prevent key_event_buffer overflows

This commit is contained in:
Bernhard Liebl 2018-01-19 08:47:30 +01:00
parent 9f479f096c
commit 2419ffd428
2 changed files with 14 additions and 9 deletions

View file

@ -52,10 +52,6 @@
class OS_OSX : public OS_Unix { class OS_OSX : public OS_Unix {
public: public:
enum {
KEY_EVENT_BUFFER_SIZE = 512
};
struct KeyEvent { struct KeyEvent {
unsigned int osx_state; unsigned int osx_state;
bool pressed; bool pressed;
@ -64,7 +60,7 @@ public:
uint32_t unicode; uint32_t unicode;
}; };
KeyEvent key_event_buffer[KEY_EVENT_BUFFER_SIZE]; Vector<KeyEvent> key_event_buffer;
int key_event_pos; int key_event_pos;
bool force_quit; bool force_quit;

View file

@ -88,6 +88,15 @@ static void get_key_modifier_state(unsigned int p_osx_state, Ref<InputEventWithM
state->set_metakey((p_osx_state & NSEventModifierFlagCommand)); state->set_metakey((p_osx_state & NSEventModifierFlagCommand));
} }
static void push_to_key_event_buffer(const OS_OSX::KeyEvent &p_event) {
Vector<OS_OSX::KeyEvent> &buffer = OS_OSX::singleton->key_event_buffer;
if (OS_OSX::singleton->key_event_pos >= buffer.size()) {
buffer.resize(1 + OS_OSX::singleton->key_event_pos);
}
buffer[OS_OSX::singleton->key_event_pos++] = p_event;
}
static int mouse_x = 0; static int mouse_x = 0;
static int mouse_y = 0; static int mouse_y = 0;
static int prev_mouse_x = 0; static int prev_mouse_x = 0;
@ -446,7 +455,7 @@ static const NSRange kEmptyRange = { NSNotFound, 0 };
ke.scancode = 0; ke.scancode = 0;
ke.unicode = codepoint; ke.unicode = codepoint;
OS_OSX::singleton->key_event_buffer[OS_OSX::singleton->key_event_pos++] = ke; push_to_key_event_buffer(ke);
} }
[self cancelComposition]; [self cancelComposition];
} }
@ -805,7 +814,7 @@ static int translateKey(unsigned int key) {
ke.scancode = latin_keyboard_keycode_convert(translateKey([event keyCode])); ke.scancode = latin_keyboard_keycode_convert(translateKey([event keyCode]));
ke.unicode = 0; ke.unicode = 0;
OS_OSX::singleton->key_event_buffer[OS_OSX::singleton->key_event_pos++] = ke; push_to_key_event_buffer(ke);
} }
if ((OS_OSX::singleton->im_position.x != 0) && (OS_OSX::singleton->im_position.y != 0)) if ((OS_OSX::singleton->im_position.x != 0) && (OS_OSX::singleton->im_position.y != 0))
@ -858,7 +867,7 @@ static int translateKey(unsigned int key) {
ke.scancode = latin_keyboard_keycode_convert(translateKey(key)); ke.scancode = latin_keyboard_keycode_convert(translateKey(key));
ke.unicode = 0; ke.unicode = 0;
OS_OSX::singleton->key_event_buffer[OS_OSX::singleton->key_event_pos++] = ke; push_to_key_event_buffer(ke);
} }
} }
@ -874,7 +883,7 @@ static int translateKey(unsigned int key) {
ke.scancode = latin_keyboard_keycode_convert(translateKey([event keyCode])); ke.scancode = latin_keyboard_keycode_convert(translateKey([event keyCode]));
ke.unicode = 0; ke.unicode = 0;
OS_OSX::singleton->key_event_buffer[OS_OSX::singleton->key_event_pos++] = ke; push_to_key_event_buffer(ke);
} }
} }