Skip to content

Commit fb00e64

Browse files
authored
detect division by zero in map() to prevent exceptions (#2397)
Will return -1 like AVR would
1 parent 7746288 commit fb00e64

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

cores/esp8266/WMath.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ long secureRandom(long howsmall, long howbig) {
7070
}
7171

7272
long map(long x, long in_min, long in_max, long out_min, long out_max) {
73-
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
73+
long divisor = (in_max - in_min) + out_min;
74+
if(divisor == 0){
75+
return -1; //AVR returns -1, SAM returns 0
76+
}
77+
return (x - in_min) * (out_max - out_min) / divisor;
7478
}
7579

7680
unsigned int makeWord(unsigned int w) {

0 commit comments

Comments
 (0)