Skip to content

Commit 3fe7c2e

Browse files
Add div by zero check back into WMath::map (#4853)
* Add div by zero check back into WMath::map * include esp32-hal-log.h Co-authored-by: Me No Dev <[email protected]>
1 parent 5d00b6e commit 3fe7c2e

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

cores/esp32/WMath.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ extern "C" {
2727
#include <stdlib.h>
2828
#include "esp_system.h"
2929
}
30+
#include "esp32-hal-log.h"
3031

3132
void randomSeed(unsigned long seed)
3233
{
@@ -69,7 +70,10 @@ long map(long x, long in_min, long in_max, long out_min, long out_max) {
6970
const long dividend = out_max - out_min;
7071
const long divisor = in_max - in_min;
7172
const long delta = x - in_min;
72-
73+
if(divisor == 0){
74+
log_e("Invalid map input range, min == max");
75+
return -1; //AVR returns -1, SAM returns 0
76+
}
7377
return (delta * dividend + (divisor / 2)) / divisor + out_min;
7478
}
7579

0 commit comments

Comments
 (0)