From 74124c344ce61912663db18d03168a949789ac51 Mon Sep 17 00:00:00 2001 From: Vladimir Serbinenko Date: Mon, 28 Oct 2013 13:37:40 +0100 Subject: [PATCH] * grub-core/lib/progress.c (grub_file_progress_hook_real): Add missing safeguards. Fixes a crash with i386/pc/console.c. --- ChangeLog | 5 ++++ grub-core/lib/progress.c | 57 +++++++++++++++++++++++++++++----------- 2 files changed, 47 insertions(+), 15 deletions(-) diff --git a/ChangeLog b/ChangeLog index 58e5e91f1..6389edc3b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2013-10-28 Vladimir Serbinenko + + * grub-core/lib/progress.c (grub_file_progress_hook_real): Add missing + safeguards. Fixes a crash with i386/pc/console.c. + 2013-10-28 Vladimir Serbinenko * include/grub/emu/hostdisk.h: Add proper declaration for grub_host_init diff --git a/grub-core/lib/progress.c b/grub-core/lib/progress.c index 4a2164502..63a0767d6 100644 --- a/grub-core/lib/progress.c +++ b/grub-core/lib/progress.c @@ -33,23 +33,48 @@ grub_file_progress_hook_real (grub_disk_addr_t sector __attribute__ ((unused)), unsigned offset __attribute__ ((unused)), unsigned length, void *data) { - grub_uint64_t now = grub_get_time_ms (); + static int call_depth = 0; + grub_uint64_t now; static grub_uint64_t last_progress_update_time; grub_file_t file = data; file->progress_offset += length; + if (call_depth) + return; + + call_depth = 1; + now = grub_get_time_ms (); + if (((now - last_progress_update_time > UPDATE_INTERVAL) && (file->progress_offset - file->offset > 0)) || (file->progress_offset == file->size)) { - char buffer[80]; + static char buffer[80]; struct grub_term_output *term; - char *partial_file_name = grub_strrchr (file->name, '/') + 1; + const char *partial_file_name; - grub_uint64_t current_speed = grub_divmod64 ((file->progress_offset - - file->last_progress_offset) - * 100ULL * 1000ULL, - now - file->last_progress_time, 0); + unsigned long long percent; + grub_uint64_t current_speed; + + if (now - file->last_progress_time < 10) + current_speed = 0; + else + current_speed = grub_divmod64 ((file->progress_offset + - file->last_progress_offset) + * 100ULL * 1000ULL, + now - file->last_progress_time, 0); + + if (file->size == 0) + percent = 100; + else + percent = grub_divmod64 (100 * file->progress_offset, + file->size, 0); + + partial_file_name = grub_strrchr (file->name, '/'); + if (partial_file_name) + partial_file_name++; + else + partial_file_name = ""; file->estimated_speed = (file->estimated_speed + current_speed) >> 1; @@ -57,32 +82,33 @@ grub_file_progress_hook_real (grub_disk_addr_t sector __attribute__ ((unused)), partial_file_name, grub_get_human_size (file->progress_offset, GRUB_HUMAN_SIZE_NORMAL), - (unsigned long long) grub_divmod64 (100 * file->progress_offset, - file->size, 0)); + (unsigned long long) percent); char *ptr = buffer + grub_strlen (buffer); grub_snprintf (ptr, sizeof (buffer) - (ptr - buffer), "%s ]", grub_get_human_size (file->estimated_speed, GRUB_HUMAN_SIZE_SPEED)); - grub_uint16_t len = grub_strlen (buffer); + grub_size_t len = grub_strlen (buffer); FOR_ACTIVE_TERM_OUTPUTS (term) { - if (term->progress_update_counter++ > term->progress_update_divisor || - (file->progress_offset == file->size && - term->progress_update_divisor != (unsigned) GRUB_PROGRESS_NO_UPDATE)) + if (term->progress_update_counter++ > term->progress_update_divisor + || (file->progress_offset == file->size + && term->progress_update_divisor + != (unsigned) GRUB_PROGRESS_NO_UPDATE)) { struct grub_term_coordinate old_pos = grub_term_getxy (term); struct grub_term_coordinate new_pos = old_pos; new_pos.x = grub_term_width (term) - len - 1; grub_term_gotoxy (term, new_pos); - grub_puts_terminal (buffer, term); + grub_puts_terminal (buffer, term); grub_term_gotoxy (term, old_pos); term->progress_update_counter = 0; - term->refresh (term); + if (term->refresh) + term->refresh (term); } } @@ -90,6 +116,7 @@ grub_file_progress_hook_real (grub_disk_addr_t sector __attribute__ ((unused)), file->last_progress_time = now; last_progress_update_time = now; } + call_depth = 0; } GRUB_MOD_INIT(progress)