mirror of https://github.com/ipxe/ipxe.git
[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>pull/1/head
parent
02a6f46c09
commit
5590faf14a
|
@ -62,6 +62,8 @@ int monojob_wait ( const char *string ) {
|
||||||
int rc;
|
int rc;
|
||||||
unsigned long last_progress;
|
unsigned long last_progress;
|
||||||
unsigned long elapsed;
|
unsigned long elapsed;
|
||||||
|
unsigned long completed;
|
||||||
|
unsigned long total;
|
||||||
unsigned int percentage;
|
unsigned int percentage;
|
||||||
int shown_percentage = 0;
|
int shown_percentage = 0;
|
||||||
|
|
||||||
|
@ -85,9 +87,11 @@ int monojob_wait ( const char *string ) {
|
||||||
if ( shown_percentage )
|
if ( shown_percentage )
|
||||||
printf ( "\b\b\b\b \b\b\b\b" );
|
printf ( "\b\b\b\b \b\b\b\b" );
|
||||||
job_progress ( &monojob, &progress );
|
job_progress ( &monojob, &progress );
|
||||||
if ( progress.total ) {
|
/* Normalise progress figures to avoid overflow */
|
||||||
percentage = ( ( 100 * progress.completed ) /
|
completed = ( progress.completed / 128 );
|
||||||
progress.total );
|
total = ( progress.total / 128 );
|
||||||
|
if ( total ) {
|
||||||
|
percentage = ( ( 100 * completed ) / total );
|
||||||
printf ( "%3d%%", percentage );
|
printf ( "%3d%%", percentage );
|
||||||
shown_percentage = 1;
|
shown_percentage = 1;
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue