NCOF: secure possible index overflow by 1 due to float to int conversion

This commit is contained in:
f4exb 2018-02-15 23:58:01 +01:00
parent ae8eddbfda
commit 34081dd50b
2 changed files with 12 additions and 6 deletions

View File

@ -23,16 +23,19 @@
#undef M_PI #undef M_PI
#define M_PI 3.14159265358979323846 #define M_PI 3.14159265358979323846
Real NCOF::m_table[NCOF::TableSize]; Real NCOF::m_table[NCOF::TableSize+1];
bool NCOF::m_tableInitialized = false; bool NCOF::m_tableInitialized = false;
float NCOF::m_tableSizeLimit = (float) NCOF::TableSize;
void NCOF::initTable() void NCOF::initTable()
{ {
if(m_tableInitialized) if(m_tableInitialized) {
return; return;
}
for(int i = 0; i < TableSize; i++) for(int i = 0; i <= TableSize; i++) {
m_table[i] = cos((2.0 * M_PI * i) / TableSize); m_table[i] = cos((2.0 * M_PI * i) / TableSize);
}
m_tableInitialized = true; m_tableInitialized = true;
} }

View File

@ -25,8 +25,9 @@ private:
enum { enum {
TableSize = (1 << 12), TableSize = (1 << 12),
}; };
static Real m_table[TableSize]; static Real m_table[TableSize+1];
static bool m_tableInitialized; static bool m_tableInitialized;
static float m_tableSizeLimit;
static void initTable(); static void initTable();
@ -42,10 +43,12 @@ public:
int nextPhase() //!< Increment phase and return its integer value int nextPhase() //!< Increment phase and return its integer value
{ {
m_phase += m_phaseIncrement; m_phase += m_phaseIncrement;
while(m_phase >= TableSize) while(m_phase >= m_tableSizeLimit) {
m_phase -= TableSize; m_phase -= TableSize;
while(m_phase < 0) }
while(m_phase < 0.0) {
m_phase += TableSize; m_phase += TableSize;
}
return (int) m_phase; return (int) m_phase;
} }