fix(widgets/canvas): Add bounds check when drawing line high/low (#283)

* Add bounds check when drawing line high/low
* Add test to ensure codepath doesn't break
pull/287/head
Darrien Glasser 4 years ago committed by GitHub
parent 6ffdede95a
commit 359b7feb8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -64,7 +64,11 @@ fn draw_line_low(painter: &mut Painter, x1: usize, y1: usize, x2: usize, y2: usi
for x in x1..=x2 {
painter.paint(x, y, color);
if d > 0 {
y = if y1 > y2 { y - 1 } else { y + 1 };
y = if y1 > y2 {
y.saturating_sub(1)
} else {
y.saturating_add(1)
};
d -= 2 * dx;
}
d += 2 * dy;
@ -79,7 +83,11 @@ fn draw_line_high(painter: &mut Painter, x1: usize, y1: usize, x2: usize, y2: us
for y in y1..=y2 {
painter.paint(x, y, color);
if d > 0 {
x = if x1 > x2 { x - 1 } else { x + 1 };
x = if x1 > x2 {
x.saturating_sub(1)
} else {
x.saturating_add(1)
};
d -= 2 * dy;
}
d += 2 * dx;

@ -35,3 +35,40 @@ fn zero_axes_ok() {
})
.unwrap();
}
#[test]
fn handles_overflow() {
let backend = TestBackend::new(80, 30);
let mut terminal = Terminal::new(backend).unwrap();
terminal
.draw(|mut f| {
let datasets = [Dataset::default()
.marker(symbols::Marker::Braille)
.style(Style::default().fg(Color::Magenta))
.data(&[
(1588298471.0, 1.0),
(1588298473.0, 0.0),
(1588298496.0, 1.0),
])];
let chart = Chart::default()
.block(Block::default().title("Plot").borders(Borders::ALL))
.x_axis(
Axis::default()
.bounds([1588298471.0, 1588992600.0])
.labels(&["1588298471.0", "1588992600.0"]),
)
.y_axis(Axis::default().bounds([0.0, 1.0]).labels(&["0.0", "1.0"]))
.datasets(&datasets);
f.render_widget(
chart,
Rect {
x: 0,
y: 0,
width: 80,
height: 30,
},
);
})
.unwrap();
}

Loading…
Cancel
Save