plotters-rs / plotters

A rust drawing library for high quality data plotting for both WASM and native, statically and realtimely 🦀 📈🚀
https://plotters-rs.github.io/home/
MIT License
3.87k stars 281 forks source link

[BUG] "attempt to add with overflow" in plotters code #304

Closed osa1 closed 2 years ago

osa1 commented 2 years ago

Repro (slightly modified version of the GTK example in the repo):

use std::env::args;

use gio::prelude::*;
use gtk::prelude::*;
use gtk::DrawingArea;

use cairo::Context;
use plotters::prelude::*;
use plotters_cairo::CairoBackend;

fn build_ui(app: &gtk::Application) {
    drawable(app, 500, 500, |_, cr| {
        let root = CairoBackend::new(cr, (500, 500))
            .unwrap()
            .into_drawing_area();

        root.fill(&WHITE).unwrap();
        let root = root.margin(25, 25, 25, 25);

        let mut chart = ChartBuilder::on(&root)
            .caption("This is a test", ("monospace", 20))
            .set_label_area_size(LabelAreaPosition::Left, 40)
            .set_label_area_size(LabelAreaPosition::Bottom, 40)
            .build_cartesian_2d(0..3000, 0u32..u32::MAX)
            .unwrap();

        chart.configure_mesh().draw().unwrap();

        Inhibit(false)
    })
}

fn main() {
    let application = gtk::Application::new(
        Some("io.github.plotters-rs.plotters-gtk-test"),
        Default::default(),
    )
    .expect("Initialization failed...");

    application.connect_activate(|app| {
        build_ui(app);
    });

    application.run(&args().collect::<Vec<_>>());
}

pub fn drawable<F>(application: &gtk::Application, width: i32, height: i32, draw_fn: F)
where
    F: Fn(&DrawingArea, &Context) -> Inhibit + 'static,
{
    let window = gtk::ApplicationWindow::new(application);
    let drawing_area = Box::new(DrawingArea::new)();

    drawing_area.connect_draw(draw_fn);

    window.set_default_size(width, height);

    window.add(&drawing_area);
    window.show_all();
}

Output:

$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.04s
     Running `target/debug/gtk-demo`
thread 'main' panicked at 'attempt to add with overflow', /home/omer/rust/plotters/src/coord/ranged1d/types/numeric.rs:209:1
stack backtrace:
   0: rust_begin_unwind
             at /rustc/59eed8a2aac0230a8b53e89d4e99d55912ba6b35/library/std/src/panicking.rs:517:5
   1: core::panicking::panic_fmt
             at /rustc/59eed8a2aac0230a8b53e89d4e99d55912ba6b35/library/core/src/panicking.rs:101:14
   2: core::panicking::panic
             at /rustc/59eed8a2aac0230a8b53e89d4e99d55912ba6b35/library/core/src/panicking.rs:50:5
   3: plotters::coord::ranged1d::types::numeric::compute_u32_key_points
             at /home/omer/rust/plotters/src/coord/ranged1d/types/numeric.rs:177:28
   4: <plotters::coord::ranged1d::types::numeric::RangedCoordu32 as plotters::coord::ranged1d::Ranged>::key_points
             at /home/omer/rust/plotters/src/coord/ranged1d/types/numeric.rs:93:17
   5: plotters::coord::ranged2d::cartesian::Cartesian2d<X,Y>::draw_mesh
             at /home/omer/rust/plotters/src/coord/ranged2d/cartesian.rs:58:13
   6: plotters::drawing::area::DrawingArea<DB,plotters::coord::ranged2d::cartesian::Cartesian2d<X,Y>>::draw_mesh::{{closure}}
             at /home/omer/rust/plotters/src/drawing/area.rs:199:13
   7: plotters::drawing::area::DrawingArea<DB,CT>::backend_ops
             at /home/omer/rust/plotters/src/drawing/area.rs:277:13
   8: plotters::drawing::area::DrawingArea<DB,plotters::coord::ranged2d::cartesian::Cartesian2d<X,Y>>::draw_mesh
             at /home/omer/rust/plotters/src/drawing/area.rs:198:9
   9: plotters::chart::context::cartesian2d::draw_impl::<impl plotters::chart::context::ChartContext<DB,plotters::coord::ranged2d::cartesian::Cartesian2d<X,Y>>>::draw_mesh_lines
             at /home/omer/rust/plotters/src/chart/context/cartesian2d/draw_impl.rs:36:9
  10: plotters::chart::context::cartesian2d::draw_impl::<impl plotters::chart::context::ChartContext<DB,plotters::coord::ranged2d::cartesian::Cartesian2d<X,Y>>>::draw_mesh
             at /home/omer/rust/plotters/src/chart/context/cartesian2d/draw_impl.rs:346:13
  11: plotters::chart::mesh::MeshStyle<X,Y,DB>::draw
             at /home/omer/rust/plotters/src/chart/mesh.rs:442:9
  12: gtk_demo::build_ui::{{closure}}
             at ./src/main.rs:27:9
  13: <O as gtk::auto::widget::WidgetExt>::connect_draw::draw_trampoline
             at /home/omer/.cargo/registry/src/github.com-1ecc6299db9ec823/gtk-0.9.2/src/auto/widget.rs:3747:13
  14: <unknown>
  15: <unknown>
  16: <unknown>
  17: g_signal_emit_valist
  18: g_signal_emit
  19: <unknown>
  20: gtk_container_propagate_draw
  21: <unknown>
  22: <unknown>
  23: <unknown>
  24: <unknown>
  25: gtk_main_do_event
  26: <unknown>
  27: <unknown>
  28: <unknown>
  29: <unknown>
  30: g_closure_invoke
  31: <unknown>
  32: g_signal_emit_valist
  33: g_signal_emit
  34: <unknown>
  35: <unknown>
  36: <unknown>
  37: g_main_context_dispatch
  38: <unknown>
  39: g_main_context_iteration
  40: g_application_run
  41: <O as gio::application::ApplicationExtManual>::run
             at /home/omer/.cargo/registry/src/github.com-1ecc6299db9ec823/gio-0.9.1/src/application.rs:23:13
  42: gtk_demo::main
             at ./src/main.rs:44:5
  43: core::ops::function::FnOnce::call_once
             at /rustc/59eed8a2aac0230a8b53e89d4e99d55912ba6b35/library/core/src/ops/function.rs:227:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
kaimast commented 2 years ago

I noticed this too. It seems like the range build_cartesian_2d cannot be MAX or MIN.

38 commented 2 years ago

Hi there, I believe this is fixed by the latest commit. Feel free to reopen it if anything is not right.