Skip to content

Commit 5590faf

Browse files
committedMar 26, 2011
[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>
1 parent 02a6f46 commit 5590faf

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed
 

‎src/core/monojob.c

+7-3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ int monojob_wait ( const char *string ) {
6262
int rc;
6363
unsigned long last_progress;
6464
unsigned long elapsed;
65+
unsigned long completed;
66+
unsigned long total;
6567
unsigned int percentage;
6668
int shown_percentage = 0;
6769

@@ -85,9 +87,11 @@ int monojob_wait ( const char *string ) {
8587
if ( shown_percentage )
8688
printf ( "\b\b\b\b \b\b\b\b" );
8789
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 );
9195
printf ( "%3d%%", percentage );
9296
shown_percentage = 1;
9397
} else {

0 commit comments

Comments
 (0)