Remove potential division by 0 in gfxmenu.

This commit is contained in:
Vladimir Serbinenko 2015-01-21 15:56:53 +01:00
parent 44b38e4988
commit 6866f2ba37
4 changed files with 61 additions and 46 deletions

View file

@ -1,3 +1,7 @@
2015-01-20 Vladimir Serbinenko <phcoder@gmail.com>
Remove potential division by 0 in gfxmenu.
2015-01-20 Vladimir Serbinenko <phcoder@gmail.com> 2015-01-20 Vladimir Serbinenko <phcoder@gmail.com>
* grub-core/normal/menu_text.c (grub_menu_init_page): Avoid * grub-core/normal/menu_text.c (grub_menu_init_page): Avoid

View file

@ -138,51 +138,53 @@ circprog_paint (void *vself, const grub_video_rect_t *region)
(height - center_height) / 2, 0, 0, (height - center_height) / 2, 0, 0,
center_width, center_height); center_width, center_height);
int radius = grub_min (height, width) / 2 - grub_max (tick_height, tick_width) / 2 - 1; if (self->num_ticks)
unsigned nticks;
unsigned tick_begin;
unsigned tick_end;
if (self->end <= self->start
|| self->value <= self->start)
nticks = 0;
else
nticks = ((unsigned) (self->num_ticks
* (self->value - self->start)))
/ ((unsigned) (self->end - self->start));
/* Do ticks appear or disappear as the value approached the end? */
if (self->ticks_disappear)
{ {
tick_begin = nticks; int radius = grub_min (height, width) / 2 - grub_max (tick_height, tick_width) / 2 - 1;
tick_end = self->num_ticks; unsigned nticks;
unsigned tick_begin;
unsigned tick_end;
if (self->end <= self->start
|| self->value <= self->start)
nticks = 0;
else
nticks = ((unsigned) (self->num_ticks
* (self->value - self->start)))
/ ((unsigned) (self->end - self->start));
/* Do ticks appear or disappear as the value approached the end? */
if (self->ticks_disappear)
{
tick_begin = nticks;
tick_end = self->num_ticks;
}
else
{
tick_begin = 0;
tick_end = nticks;
}
unsigned i;
for (i = tick_begin; i < tick_end; i++)
{
int x;
int y;
int angle;
/* Calculate the location of the tick. */
angle = self->start_angle
+ i * GRUB_TRIG_ANGLE_MAX / self->num_ticks;
x = width / 2 + (grub_cos (angle) * radius / GRUB_TRIG_FRACTION_SCALE);
y = height / 2 + (grub_sin (angle) * radius / GRUB_TRIG_FRACTION_SCALE);
/* Adjust (x,y) so the tick is centered. */
x -= tick_width / 2;
y -= tick_height / 2;
/* Draw the tick. */
grub_video_blit_bitmap (self->tick_bitmap, GRUB_VIDEO_BLIT_BLEND,
x, y, 0, 0, tick_width, tick_height);
}
} }
else
{
tick_begin = 0;
tick_end = nticks;
}
unsigned i;
for (i = tick_begin; i < tick_end; i++)
{
int x;
int y;
int angle;
/* Calculate the location of the tick. */
angle = self->start_angle
+ i * GRUB_TRIG_ANGLE_MAX / self->num_ticks;
x = width / 2 + (grub_cos (angle) * radius / GRUB_TRIG_FRACTION_SCALE);
y = height / 2 + (grub_sin (angle) * radius / GRUB_TRIG_FRACTION_SCALE);
/* Adjust (x,y) so the tick is centered. */
x -= tick_width / 2;
y -= tick_height / 2;
/* Draw the tick. */
grub_video_blit_bitmap (self->tick_bitmap, GRUB_VIDEO_BLIT_BLEND,
x, y, 0, 0, tick_width, tick_height);
}
grub_gui_restore_viewport (&vpsave); grub_gui_restore_viewport (&vpsave);
} }

View file

@ -131,6 +131,9 @@ get_num_shown_items (list_impl_t self)
int max_top_pad = grub_max (item_top_pad, sel_top_pad); int max_top_pad = grub_max (item_top_pad, sel_top_pad);
int max_bottom_pad = grub_max (item_bottom_pad, sel_bottom_pad); int max_bottom_pad = grub_max (item_bottom_pad, sel_bottom_pad);
if (item_height + item_vspace <= 0)
return 1;
return (self->bounds.height + item_vspace - 2 * boxpad return (self->bounds.height + item_vspace - 2 * boxpad
- max_top_pad - max_bottom_pad - max_top_pad - max_bottom_pad
- box_top_pad - box_bottom_pad) / (item_height + item_vspace); - box_top_pad - box_bottom_pad) / (item_height + item_vspace);

View file

@ -118,9 +118,15 @@ draw_filled_rect_bar (grub_gui_progress_bar_t self)
f.width + 2, f.height + 2); f.width + 2, f.height + 2);
/* Bar background. */ /* Bar background. */
int barwidth = (f.width unsigned barwidth;
* (self->value - self->start)
/ (self->end - self->start)); if (self->end <= self->start
|| self->value <= self->start)
barwidth = 0;
else
barwidth = (f.width
* (self->value - self->start)
/ (self->end - self->start));
grub_video_fill_rect (grub_video_map_rgba_color (self->bg_color), grub_video_fill_rect (grub_video_map_rgba_color (self->bg_color),
f.x + barwidth, f.y, f.x + barwidth, f.y,
f.width - barwidth, f.height); f.width - barwidth, f.height);