Skip to content

Commit 6141d9e

Browse files
committed
Prevent out-of-bounds array access in TCommandSet
Fixes #216
1 parent eae7683 commit 6141d9e

2 files changed

Lines changed: 10 additions & 6 deletions

File tree

include/tvision/views.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ class TCommandSet
238238

239239
private:
240240

241-
int loc( int ) noexcept;
241+
uint loc( int ) noexcept;
242242
int mask( int ) noexcept;
243243

244244
static int _NEAR masks[8];
@@ -272,9 +272,9 @@ inline int operator != ( const TCommandSet& tc1, const TCommandSet& tc2 ) noexce
272272
return !operator == ( tc1, tc2 );
273273
}
274274

275-
inline int TCommandSet::loc( int cmd ) noexcept
275+
inline uint TCommandSet::loc( int cmd ) noexcept
276276
{
277-
return cmd / 8;
277+
return (uint) cmd / 8;
278278
}
279279

280280
inline int TCommandSet::mask( int cmd ) noexcept

source/tvision/tcmdset.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,15 @@ TCommandSet& TCommandSet::operator = ( const TCommandSet& tc ) noexcept
4747

4848
Boolean TCommandSet::has( int cmd ) noexcept
4949
{
50-
return Boolean( (cmds[ loc( cmd ) ] & mask( cmd )) != 0 );
50+
if( loc( cmd ) < 32 )
51+
return Boolean( (cmds[ loc( cmd ) ] & mask( cmd )) != 0 );
52+
return False;
5153
}
5254

5355
void TCommandSet::disableCmd( int cmd ) noexcept
5456
{
55-
cmds[ loc( cmd ) ] &= ~mask( cmd );
57+
if( loc( cmd ) < 32 )
58+
cmds[ loc( cmd ) ] &= ~mask( cmd );
5659
}
5760

5861
void TCommandSet::enableCmd( const TCommandSet& tc ) noexcept
@@ -69,7 +72,8 @@ void TCommandSet::disableCmd( const TCommandSet& tc ) noexcept
6972

7073
void TCommandSet::enableCmd( int cmd ) noexcept
7174
{
72-
cmds[ loc( cmd ) ] |= mask( cmd );
75+
if( loc( cmd ) < 32 )
76+
cmds[ loc( cmd ) ] |= mask( cmd );
7377
}
7478

7579
TCommandSet& TCommandSet::operator &= ( const TCommandSet& tc ) noexcept

0 commit comments

Comments
 (0)