Skip to content

Commit 9fd352e

Browse files
dantheman4700CalcProgrammer1
authored andcommitted
Prevent division by zero crash in QMK OpenRGB controllers
1 parent d5db3d6 commit 9fd352e

4 files changed

Lines changed: 68 additions & 0 deletions

File tree

Controllers/QMKController/QMKOpenRGBController/QMKOpenRGBRev9Controller/RGBController_QMKOpenRGBRev9.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,15 @@ unsigned int RGBController_QMKOpenRGBRev9::CalculateDivisor
525525
counts[i]++;
526526
}
527527

528+
/*---------------------------------------------------------*\
529+
| Guard against empty distances (malformed LED data) |
530+
\*---------------------------------------------------------*/
531+
if(distances.empty())
532+
{
533+
LOG_WARNING("[%s] No valid LED distances found, using default divisor of 1", name.c_str());
534+
return 1;
535+
}
536+
528537
unsigned int divisor = distances[0];
529538
for(const std::pair<const int, int> &i : counts)
530539
{
@@ -533,6 +542,13 @@ unsigned int RGBController_QMKOpenRGBRev9::CalculateDivisor
533542
divisor = i.first;
534543
}
535544
}
545+
546+
if(divisor == 0)
547+
{
548+
LOG_WARNING("[%s] Calculated divisor is 0, using default of 1. This may indicate malformed LED position data.", name.c_str());
549+
return 1;
550+
}
551+
536552
return divisor;
537553
}
538554

Controllers/QMKController/QMKOpenRGBController/QMKOpenRGBRevBController/RGBController_QMKOpenRGBRevB.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,16 @@ unsigned int RGBController_QMKOpenRGBRevB::CalculateDivisor
560560
last_pos = pt.x;
561561
});
562562
}
563+
564+
/*---------------------------------------------------------*\
565+
| Guard against empty distances (malformed LED data) |
566+
\*---------------------------------------------------------*/
567+
if(distances.empty())
568+
{
569+
LOG_WARNING("[%s] No valid LED distances found, using default divisor of 1", name.c_str());
570+
return 1;
571+
}
572+
563573
std::map<int, int> counts;
564574
for(const int &i : distances)
565575
{
@@ -574,6 +584,16 @@ unsigned int RGBController_QMKOpenRGBRevB::CalculateDivisor
574584
divisor = i.first;
575585
}
576586
}
587+
588+
/*---------------------------------------------------------*\
589+
| Guard against zero divisor (prevents division by zero) |
590+
\*---------------------------------------------------------*/
591+
if(divisor == 0)
592+
{
593+
LOG_WARNING("[%s] Calculated divisor is 0, using default of 1. This may indicate malformed LED position data.", name.c_str());
594+
return 1;
595+
}
596+
577597
return divisor;
578598
}
579599

Controllers/QMKController/QMKOpenRGBController/QMKOpenRGBRevDController/RGBController_QMKOpenRGBRevD.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,15 @@ unsigned int RGBController_QMKOpenRGBRevD::CalculateDivisor
567567
counts[i]++;
568568
}
569569

570+
/*---------------------------------------------------------*\
571+
| Guard against empty distances (malformed LED data) |
572+
\*---------------------------------------------------------*/
573+
if(distances.empty())
574+
{
575+
LOG_WARNING("[%s] No valid LED distances found, using default divisor of 1", name.c_str());
576+
return 1;
577+
}
578+
570579
unsigned int divisor = distances[0];
571580
for(const std::pair<const int, int> &i : counts)
572581
{
@@ -575,6 +584,13 @@ unsigned int RGBController_QMKOpenRGBRevD::CalculateDivisor
575584
divisor = i.first;
576585
}
577586
}
587+
588+
if(divisor == 0)
589+
{
590+
LOG_WARNING("[%s] Calculated divisor is 0, using default of 1. This may indicate malformed LED position data.", name.c_str());
591+
return 1;
592+
}
593+
578594
return divisor;
579595
}
580596

Controllers/QMKController/QMKOpenRGBController/QMKOpenRGBRevEController/RGBController_QMKOpenRGBRevE.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,15 @@ unsigned int RGBController_QMKOpenRGBRevE::CalculateDivisor
583583
counts[i]++;
584584
}
585585

586+
/*---------------------------------------------------------*\
587+
| Guard against empty distances (malformed LED data) |
588+
\*---------------------------------------------------------*/
589+
if(distances.empty())
590+
{
591+
LOG_WARNING("[%s] No valid LED distances found, using default divisor of 1", name.c_str());
592+
return 1;
593+
}
594+
586595
unsigned int divisor = distances[0];
587596
for(const std::pair<const int, int> &i : counts)
588597
{
@@ -591,6 +600,13 @@ unsigned int RGBController_QMKOpenRGBRevE::CalculateDivisor
591600
divisor = i.first;
592601
}
593602
}
603+
604+
if(divisor == 0)
605+
{
606+
LOG_WARNING("[%s] Calculated divisor is 0, using default of 1. This may indicate malformed LED position data.", name.c_str());
607+
return 1;
608+
}
609+
594610
return divisor;
595611
}
596612

0 commit comments

Comments
 (0)