diff options
author | Michael Brown | 2011-03-26 14:13:45 +0100 |
---|---|---|
committer | Michael Brown | 2011-03-26 14:14:53 +0100 |
commit | 5590faf14af29421281f40fdd93f8770dd436ce3 (patch) | |
tree | 99adc5d86c18c0f644026e0a2e8597c06490a562 /src/core/monojob.c | |
parent | [settings] Match terminology in online documentation (diff) | |
download | ipxe-5590faf14af29421281f40fdd93f8770dd436ce3.tar.gz ipxe-5590faf14af29421281f40fdd93f8770dd436ce3.tar.xz ipxe-5590faf14af29421281f40fdd93f8770dd436ce3.zip |
[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>
Diffstat (limited to 'src/core/monojob.c')
-rw-r--r-- | src/core/monojob.c | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/src/core/monojob.c b/src/core/monojob.c index 62e672a4..7431f88a 100644 --- a/src/core/monojob.c +++ b/src/core/monojob.c @@ -62,6 +62,8 @@ int monojob_wait ( const char *string ) { int rc; unsigned long last_progress; unsigned long elapsed; + unsigned long completed; + unsigned long total; unsigned int percentage; int shown_percentage = 0; @@ -85,9 +87,11 @@ int monojob_wait ( const char *string ) { if ( shown_percentage ) printf ( "\b\b\b\b \b\b\b\b" ); job_progress ( &monojob, &progress ); - if ( progress.total ) { - percentage = ( ( 100 * progress.completed ) / - progress.total ); + /* Normalise progress figures to avoid overflow */ + completed = ( progress.completed / 128 ); + total = ( progress.total / 128 ); + if ( total ) { + percentage = ( ( 100 * completed ) / total ); printf ( "%3d%%", percentage ); shown_percentage = 1; } else { |