Skip to contentSkip to conversation

ds2760: fix integer overflow for time_to_empty_now

PATCH In review Daniel Mack 2 participants 5 messages Jul 24, 2009 – Jul 30, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ds2760: fix integer overflow for time_to_empty_now

On the device we're currently developing, battery sizes of ~2.8Ah and current flow of ~600mA are typical.

With that values, the life_sec computation overflows due to the multiplication by 3600.

Cc Szabolcs Gyurko <[email protected]>
Cc Matt Reimer <[email protected]>
Cc Anton Vorontsov <[email protected]>
---
 drivers/power/ds2760_battery.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
drivers/power/ds2760_battery.c +2 -2
index d545fee..5d30dc0 100644
@@ -212,8 +212,8 @@ static int ds2760_battery_read_status(struct ds2760_device_info *di)
 		di->rem_capacity = 100;
 
 	if (di->current_uA)
-		di->life_sec = -((di->accum_current_uAh - di->empty_uAh) *
-				 3600L) / di->current_uA;
+		di->life_sec = -((di->accum_current_uAh - di->empty_uAh) * 36L)
+					/ (di->current_uA / 100L);
 	else
 		di->life_sec = 0;
 
Signature · 1 line
1.6.3.1
#1 lore raw
Quoted 21 lines · Daniel Mack

On the device we're currently developing, battery sizes of ~2.8Ah and current flow of ~600mA are typical.

With that values, the life_sec computation overflows due to the multiplication by 3600.

Cc Szabolcs Gyurko <[email protected]>
Cc Matt Reimer <[email protected]>
Cc Anton Vorontsov <[email protected]>
---
 drivers/power/ds2760_battery.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
drivers/power/ds2760_battery.c +0 -0
index d545fee..5d30dc0 100644

drivers/power/ds2760_battery.c static int ds2760_battery_read_status(struct ds2760_device_info *di) · line 216

Quoted 7 lines
drivers/power/ds2760_battery.c +2 -2
@@ -212,8 +212,8 @@ static int ds2760_battery_read_status(struct ds2760_device_info *di)
 		di->rem_capacity = 100;
 
 	if (di->current_uA)
-		di->life_sec = -((di->accum_current_uAh - di->empty_uAh) *
-				 3600L) / di->current_uA;
+		di->life_sec = -((di->accum_current_uAh - di->empty_uAh) * 36L)
+					/ (di->current_uA / 100L);

Hm... "(di->current_uA / 100L)" might result to 0, so you'll get div by zero. Removing the parenthesis will help.

Signature · 3 lines
Anton Vorontsov
email: [email protected]
irc://irc.freenode.net/bd2
#2 lore raw
Quoted 24 lines · Anton Vorontsov
Quoted 23 lines · Daniel Mack

On the device we're currently developing, battery sizes of ~2.8Ah and current flow of ~600mA are typical.

With that values, the life_sec computation overflows due to the multiplication by 3600.

Cc Szabolcs Gyurko <[email protected]>
Cc Matt Reimer <[email protected]>
Cc Anton Vorontsov <[email protected]>
---
 drivers/power/ds2760_battery.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
drivers/power/ds2760_battery.c +0 -0
index d545fee..5d30dc0 100644
@@ -212,8 +212,8 @@ static int ds2760_battery_read_status(struct ds2760_device_info *di)
 		di->rem_capacity = 100;
 
Quoted 8 lines

drivers/power/ds2760_battery.c static int ds2760_battery_read_status(struct ds2760_device_info *di) · line 216

Quoted 5 lines
drivers/power/ds2760_battery.c +2 -2
 	if (di->current_uA)
-		di->life_sec = -((di->accum_current_uAh - di->empty_uAh) *
-				 3600L) / di->current_uA;
+		di->life_sec = -((di->accum_current_uAh - di->empty_uAh) * 36L)
+					/ (di->current_uA / 100L);

Hm... "(di->current_uA / 100L)" might result to 0, so you'll get div by zero. Removing the parenthesis will help.

Hmm, no, then it gets the precedence wrong. But checking for values >= 100 of current_uA helps. Thanks for spotting this.

Daniel

Quoted 1 line

From c60e23e2ba6667bec882338bfe3cb12304ee1ab6 Mon Sep 17 00:00:00 2001

From: Daniel Mack <[email protected]>
Date: Fri, 24 Jul 2009 15:08:11 +0200
Subject: [PATCH] ds2760: fix integer overflow for time_to_empty_now

On the device we're currently developing, battery sizes of ~2.8Ah and current flow of ~600mA are typical.

With that values, the life_sec computation overflows due to the multiplication by 3600.

Cc Szabolcs Gyurko <[email protected]>
Cc Matt Reimer <[email protected]>
Cc Anton Vorontsov <[email protected]>
---
 drivers/power/ds2760_battery.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
drivers/power/ds2760_battery.c +3 -3
index d545fee..2ef29dd 100644
@@ -211,9 +211,9 @@ static int ds2760_battery_read_status(struct ds2760_device_info *di)
 	if (di->rem_capacity > 100)
 		di->rem_capacity = 100;
 
-	if (di->current_uA)
-		di->life_sec = -((di->accum_current_uAh - di->empty_uAh) *
-				 3600L) / di->current_uA;
+	if (di->current_uA >= 100L)
+		di->life_sec = -((di->accum_current_uAh - di->empty_uAh) * 36L)
+					/ (di->current_uA / 100L);
 	else
 		di->life_sec = 0;
 
Signature · 1 line
1.6.3.3
#3 lore raw
Quoted 34 lines · Daniel Mack
Quoted 24 lines · Anton Vorontsov
Quoted 23 lines · Daniel Mack

On the device we're currently developing, battery sizes of ~2.8Ah and current flow of ~600mA are typical.

With that values, the life_sec computation overflows due to the multiplication by 3600.

Cc Szabolcs Gyurko <[email protected]>
Cc Matt Reimer <[email protected]>
Cc Anton Vorontsov <[email protected]>
---
 drivers/power/ds2760_battery.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
drivers/power/ds2760_battery.c +0 -0
index d545fee..5d30dc0 100644
@@ -212,8 +212,8 @@ static int ds2760_battery_read_status(struct ds2760_device_info *di)
 		di->rem_capacity = 100;
 
Quoted 8 lines

drivers/power/ds2760_battery.c static int ds2760_battery_read_status(struct ds2760_device_info *di) · line 216

Quoted 5 lines
drivers/power/ds2760_battery.c +2 -2
 	if (di->current_uA)
-		di->life_sec = -((di->accum_current_uAh - di->empty_uAh) *
-				 3600L) / di->current_uA;
+		di->life_sec = -((di->accum_current_uAh - di->empty_uAh) * 36L)
+					/ (di->current_uA / 100L);

Hm... "(di->current_uA / 100L)" might result to 0, so you'll get div by zero. Removing the parenthesis will help.

Quoted 1 line

Hmm, no, then it gets the precedence wrong.

Ah.

Quoted 2 lines

But checking for values >=
100 of current_uA helps. Thanks for spotting this.

Yeah, that'll be fine indeed.

Quoted 44 lines

Daniel

Quoted 1 line

From c60e23e2ba6667bec882338bfe3cb12304ee1ab6 Mon Sep 17 00:00:00 2001

From: Daniel Mack <[email protected]>
Date: Fri, 24 Jul 2009 15:08:11 +0200
Subject: [PATCH] ds2760: fix integer overflow for time_to_empty_now

On the device we're currently developing, battery sizes of ~2.8Ah and current flow of ~600mA are typical.

With that values, the life_sec computation overflows due to the multiplication by 3600.

Cc Szabolcs Gyurko <[email protected]>
Cc Matt Reimer <[email protected]>
Cc Anton Vorontsov <[email protected]>
---
 drivers/power/ds2760_battery.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
drivers/power/ds2760_battery.c +3 -3
index d545fee..2ef29dd 100644
@@ -211,9 +211,9 @@ static int ds2760_battery_read_status(struct ds2760_device_info *di)
 	if (di->rem_capacity > 100)
 		di->rem_capacity = 100;
 
-	if (di->current_uA)
-		di->life_sec = -((di->accum_current_uAh - di->empty_uAh) *
-				 3600L) / di->current_uA;
+	if (di->current_uA >= 100L)
+		di->life_sec = -((di->accum_current_uAh - di->empty_uAh) * 36L)
+					/ (di->current_uA / 100L);
 	else
 		di->life_sec = 0;
 
Signature · 1 line
1.6.3.3
Signature · 3 lines
Anton Vorontsov
email: [email protected]
irc://irc.freenode.net/bd2
#4 lore raw
Quoted 11 lines · Anton Vorontsov
Quoted 4 lines
Quoted 2 lines

Hm... "(di->current_uA / 100L)" might result to 0, so you'll get div by zero. Removing the parenthesis will help.

Hmm, no, then it gets the precedence wrong.

Ah.

Quoted 2 lines

But checking for values >=
100 of current_uA helps. Thanks for spotting this.

Yeah, that'll be fine indeed.

Just wanted to make sure you noticed that there was a new patch attached to this mail :) I copied it again here.

Daniel

Quoted 1 line

From c60e23e2ba6667bec882338bfe3cb12304ee1ab6 Mon Sep 17 00:00:00 2001

From: Daniel Mack <[email protected]>
Date: Fri, 24 Jul 2009 15:08:11 +0200
Subject: [PATCH] ds2760: fix integer overflow for time_to_empty_now

On the device we're currently developing, battery sizes of ~2.8Ah and current flow of ~600mA are typical.

With that values, the life_sec computation overflows due to the multiplication by 3600.

Cc Szabolcs Gyurko <[email protected]>
Cc Matt Reimer <[email protected]>
Cc Anton Vorontsov <[email protected]>
---
 drivers/power/ds2760_battery.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
drivers/power/ds2760_battery.c +3 -3
index d545fee..2ef29dd 100644
@@ -211,9 +211,9 @@ static int ds2760_battery_read_status(struct ds2760_device_info *di)
 	if (di->rem_capacity > 100)
 		di->rem_capacity = 100;
 
-	if (di->current_uA)
-		di->life_sec = -((di->accum_current_uAh - di->empty_uAh) *
-				 3600L) / di->current_uA;
+	if (di->current_uA >= 100L)
+		di->life_sec = -((di->accum_current_uAh - di->empty_uAh) * 36L)
+					/ (di->current_uA / 100L);
 	else
 		di->life_sec = 0;
 
Signature · 1 line
1.6.3.3