Skip to content

Commit

Permalink
Fix SIGSEGV/Keyboard buffer overflow upon multiple arrow key strokes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jehutting committed Feb 9, 2014
1 parent 29827b0 commit b3427cf
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions Keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,22 @@ void Keyboard::RestoreTerm()

KeyConfig::Action Keyboard::GetEvent()
{
int ch[8];
// An arrow key consists of a three character sequence:
// 0x1b (=ESC), 0x5b (='[') and ARROW
// where ARROW = 0x41 (='A') for arrow up
// = 0x42 (='B') for arrow down
// = 0x42 (='C') for arrow right
// = 0x42 (='D') for arrow left
int ch[3]; // Handle max ONE arrow key at a time.
int chnum = 0;

while((ch[chnum] = getchar()) != EOF)
while((chnum < 3) && ((ch[chnum] = getchar()) != EOF))
chnum++;

if (chnum == 0)
return KeyConfig::ACTION_BLANK;

// KeyConfig uses only the last two characters of an arrow key
if (chnum > 1)
ch[0] = ch[chnum - 1] | (ch[chnum - 2] << 8);

Expand Down

0 comments on commit b3427cf

Please sign in to comment.