Prevent division by zero crash in QMK OpenRGB controllers

master
Daniel Lamphere 4 months ago committed by Adam Honse
parent d5db3d6428
commit 9fd352efc4

@ -525,6 +525,15 @@ unsigned int RGBController_QMKOpenRGBRev9::CalculateDivisor
counts[i]++; counts[i]++;
} }
/*---------------------------------------------------------*\
| Guard against empty distances (malformed LED data) |
\*---------------------------------------------------------*/
if(distances.empty())
{
LOG_WARNING("[%s] No valid LED distances found, using default divisor of 1", name.c_str());
return 1;
}
unsigned int divisor = distances[0]; unsigned int divisor = distances[0];
for(const std::pair<const int, int> &i : counts) for(const std::pair<const int, int> &i : counts)
{ {
@ -533,6 +542,13 @@ unsigned int RGBController_QMKOpenRGBRev9::CalculateDivisor
divisor = i.first; divisor = i.first;
} }
} }
if(divisor == 0)
{
LOG_WARNING("[%s] Calculated divisor is 0, using default of 1. This may indicate malformed LED position data.", name.c_str());
return 1;
}
return divisor; return divisor;
} }

@ -560,6 +560,16 @@ unsigned int RGBController_QMKOpenRGBRevB::CalculateDivisor
last_pos = pt.x; last_pos = pt.x;
}); });
} }
/*---------------------------------------------------------*\
| Guard against empty distances (malformed LED data) |
\*---------------------------------------------------------*/
if(distances.empty())
{
LOG_WARNING("[%s] No valid LED distances found, using default divisor of 1", name.c_str());
return 1;
}
std::map<int, int> counts; std::map<int, int> counts;
for(const int &i : distances) for(const int &i : distances)
{ {
@ -574,6 +584,16 @@ unsigned int RGBController_QMKOpenRGBRevB::CalculateDivisor
divisor = i.first; divisor = i.first;
} }
} }
/*---------------------------------------------------------*\
| Guard against zero divisor (prevents division by zero) |
\*---------------------------------------------------------*/
if(divisor == 0)
{
LOG_WARNING("[%s] Calculated divisor is 0, using default of 1. This may indicate malformed LED position data.", name.c_str());
return 1;
}
return divisor; return divisor;
} }

@ -567,6 +567,15 @@ unsigned int RGBController_QMKOpenRGBRevD::CalculateDivisor
counts[i]++; counts[i]++;
} }
/*---------------------------------------------------------*\
| Guard against empty distances (malformed LED data) |
\*---------------------------------------------------------*/
if(distances.empty())
{
LOG_WARNING("[%s] No valid LED distances found, using default divisor of 1", name.c_str());
return 1;
}
unsigned int divisor = distances[0]; unsigned int divisor = distances[0];
for(const std::pair<const int, int> &i : counts) for(const std::pair<const int, int> &i : counts)
{ {
@ -575,6 +584,13 @@ unsigned int RGBController_QMKOpenRGBRevD::CalculateDivisor
divisor = i.first; divisor = i.first;
} }
} }
if(divisor == 0)
{
LOG_WARNING("[%s] Calculated divisor is 0, using default of 1. This may indicate malformed LED position data.", name.c_str());
return 1;
}
return divisor; return divisor;
} }

@ -583,6 +583,15 @@ unsigned int RGBController_QMKOpenRGBRevE::CalculateDivisor
counts[i]++; counts[i]++;
} }
/*---------------------------------------------------------*\
| Guard against empty distances (malformed LED data) |
\*---------------------------------------------------------*/
if(distances.empty())
{
LOG_WARNING("[%s] No valid LED distances found, using default divisor of 1", name.c_str());
return 1;
}
unsigned int divisor = distances[0]; unsigned int divisor = distances[0];
for(const std::pair<const int, int> &i : counts) for(const std::pair<const int, int> &i : counts)
{ {
@ -591,6 +600,13 @@ unsigned int RGBController_QMKOpenRGBRevE::CalculateDivisor
divisor = i.first; divisor = i.first;
} }
} }
if(divisor == 0)
{
LOG_WARNING("[%s] Calculated divisor is 0, using default of 1. This may indicate malformed LED position data.", name.c_str());
return 1;
}
return divisor; return divisor;
} }

Loading…
Cancel
Save