Browse Source

[monojob] Avoid overflow when calculating percentage progress

Normalise the progress figures to ensure that multiplication by 100
(to produce a percentage) cannot result in integer overflow.

Reported-by: Sven Dreyer <sven@dreyer-net.de>
Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 13 years ago
parent
commit
5590faf14a
1 changed files with 7 additions and 3 deletions
  1. 7
    3
      src/core/monojob.c

+ 7
- 3
src/core/monojob.c View File

@@ -62,6 +62,8 @@ int monojob_wait ( const char *string ) {
62 62
 	int rc;
63 63
 	unsigned long last_progress;
64 64
 	unsigned long elapsed;
65
+	unsigned long completed;
66
+	unsigned long total;
65 67
 	unsigned int percentage;
66 68
 	int shown_percentage = 0;
67 69
 
@@ -85,9 +87,11 @@ int monojob_wait ( const char *string ) {
85 87
 			if ( shown_percentage )
86 88
 				printf ( "\b\b\b\b    \b\b\b\b" );
87 89
 			job_progress ( &monojob, &progress );
88
-			if ( progress.total ) {
89
-				percentage = ( ( 100 * progress.completed ) /
90
-					       progress.total );
90
+			/* Normalise progress figures to avoid overflow */
91
+			completed = ( progress.completed / 128 );
92
+			total = ( progress.total / 128 );
93
+			if ( total ) {
94
+				percentage = ( ( 100 * completed ) / total );
91 95
 				printf ( "%3d%%", percentage );
92 96
 				shown_percentage = 1;
93 97
 			} else {

Loading…
Cancel
Save