Prevent division by zero crash in QMK OpenRGB controllers

This commit is contained in:
Daniel Lamphere
2026-01-04 18:51:34 +00:00
committed by Adam Honse
parent d5db3d6428
commit 9fd352efc4
4 changed files with 68 additions and 0 deletions

View File

@@ -525,6 +525,15 @@ unsigned int RGBController_QMKOpenRGBRev9::CalculateDivisor
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];
for(const std::pair<const int, int> &i : counts)
{
@@ -533,6 +542,13 @@ unsigned int RGBController_QMKOpenRGBRev9::CalculateDivisor
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;
}

View File

@@ -560,6 +560,16 @@ unsigned int RGBController_QMKOpenRGBRevB::CalculateDivisor
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;
for(const int &i : distances)
{
@@ -574,6 +584,16 @@ unsigned int RGBController_QMKOpenRGBRevB::CalculateDivisor
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;
}

View File

@@ -567,6 +567,15 @@ unsigned int RGBController_QMKOpenRGBRevD::CalculateDivisor
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];
for(const std::pair<const int, int> &i : counts)
{
@@ -575,6 +584,13 @@ unsigned int RGBController_QMKOpenRGBRevD::CalculateDivisor
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;
}

View File

@@ -583,6 +583,15 @@ unsigned int RGBController_QMKOpenRGBRevE::CalculateDivisor
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];
for(const std::pair<const int, int> &i : counts)
{
@@ -591,6 +600,13 @@ unsigned int RGBController_QMKOpenRGBRevE::CalculateDivisor
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;
}