From fb00e64a6f65dede8d897d07a28d7d02f74ae742 Mon Sep 17 00:00:00 2001 From: Me No Dev Date: Thu, 18 Aug 2016 10:50:59 +0300 Subject: [PATCH] detect division by zero in map() to prevent exceptions (#2397) Will return -1 like AVR would --- cores/esp8266/WMath.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cores/esp8266/WMath.cpp b/cores/esp8266/WMath.cpp index 13c2452dc..d9d87da5a 100644 --- a/cores/esp8266/WMath.cpp +++ b/cores/esp8266/WMath.cpp @@ -70,7 +70,11 @@ long secureRandom(long howsmall, long howbig) { } long map(long x, long in_min, long in_max, long out_min, long out_max) { - return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; + long divisor = (in_max - in_min) + out_min; + if(divisor == 0){ + return -1; //AVR returns -1, SAM returns 0 + } + return (x - in_min) * (out_max - out_min) / divisor; } unsigned int makeWord(unsigned int w) {