Skip to contentSkip to conversation

Fix potential division by zero in shaper_reset()

PATCH In review Gert Doering 2 participants 3 messages Nov 8, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix potential division by zero in shaper_reset()

shaper_reset() is only ever called with "bytes_per_second" set to a non-zero value - so the whole check "is it zero? if not, use constrain_int() to make sure it is within bounds" is not needed -> reduce check to just constrain_int() so even if somebody would call shaper_reset(..., 0) it would not lead to a div-by-zero.

Found by Coverity.

---
 src/openvpn/shaper.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/openvpn/shaper.h +1 -1
index afeb9c3..fa132c4 100644
@@ -75,7 +75,7 @@ bool shaper_soonest_event (struct timeval *tv, int delay);
 static inline void
 shaper_reset (struct shaper *s, int bytes_per_second)
 {
-  s->bytes_per_second = bytes_per_second ? constrain_int (bytes_per_second, SHAPER_MIN, SHAPER_MAX) : 0;
+  s->bytes_per_second = constrain_int (bytes_per_second, SHAPER_MIN, SHAPER_MAX);
 
 #ifdef SHAPER_USE_FP
   s->factor = 1000000.0 / (double)s->bytes_per_second;
Signature · 1 line
2.7.3
#1 lore raw
Quoted 18 lines · Gert Doering

shaper_reset() is only ever called with "bytes_per_second" set to a non-zero value - so the whole check "is it zero? if not, use constrain_int() to make sure it is within bounds" is not needed -> reduce check to just constrain_int() so even if somebody would call shaper_reset(..., 0) it would not lead to a div-by-zero.

Found by Coverity.

---
 src/openvpn/shaper.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/openvpn/shaper.h +0 -0
index afeb9c3..fa132c4 100644

src/openvpn/shaper.h bool shaper_soonest_event (struct timeval *tv, int delay); · line 81

Quoted 8 lines
src/openvpn/shaper.h +1 -1
@@ -75,7 +75,7 @@ bool shaper_soonest_event (struct timeval *tv, int delay);
 static inline void
 shaper_reset (struct shaper *s, int bytes_per_second)
 {
-  s->bytes_per_second = bytes_per_second ? constrain_int (bytes_per_second, SHAPER_MIN, SHAPER_MAX) : 0;
+  s->bytes_per_second = constrain_int (bytes_per_second, SHAPER_MIN, SHAPER_MAX);
 
 #ifdef SHAPER_USE_FP
   s->factor = 1000000.0 / (double)s->bytes_per_second;

ACK

-Steffan

#2 lore raw
In reply to Gert Doering

Patch has been applied to the master branch.

commit 9c3a9335ee77d8447bf47e464f4ab15964fb6f1b

Author: Gert Doering
Date:   Tue Nov 8 10:44:02 2016 +0100
     Fix potential division by zero in shaper_reset()

     Signed-off-by: Gert Doering <[email protected]...>
     Acked-by: Steffan Karger <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg12942.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering