font-kit icon indicating copy to clipboard operation
font-kit copied to clipboard

Bail out from rendering when bitmap_buffer is null

Open saethlin opened this issue 3 years ago • 0 comments

I don't really understand what's going on here. All I really know is that I ran into one of the standard library's debug assertions when running some of the examples in plotters. I trimmed one down, but not a whole lot.

use plotters::prelude::*;
fn main() {
    let mut buffer = vec![0u8; 1024 * 768 * 3];
    let area = BitMapBackend::with_buffer(&mut buffer[..], (1024, 768))
        .into_drawing_area()
        .split_evenly((1, 2));
    let _chart = ChartBuilder::on(&area[0])
        .caption("Incremental Example", ("sans-serif", 20))
        .build_cartesian_2d(0..10, 0..10)
        .expect("Unable to build ChartContext");
}

Running this example without this patch using cargo +nightly run -Zbuild-std --target=x86_64-unknown-linux-gnu will hit a SIGILL, with a backtrace that starts with this:

#0  core::slice::raw::from_raw_parts::{closure#0}<u8> () at src/intrinsics.rs:2009
#1  0x00005594d1bb7d65 in core::ops::function::FnOnce::call_once<core::slice::raw::from_raw_parts::{closure_env#0}<u8>, ()> ()
    at src/ops/function.rs:248
#2  0x00005594d1bccd07 in core::intrinsics::const_eval_select<(), fn(), core::slice::raw::from_raw_parts::{closure_env#0}<u8>, ()> (arg=(), _called_in_const=0x0, called_at_rt=...) at src/intrinsics.rs:2375
#3  0x00005594d1bc8002 in core::slice::raw::from_raw_parts<u8> (data=0x0, len=0) at src/slice/raw.rs:93
#4  0x00005594d19e351d in font_kit::loaders::freetype::Font::rasterize_glyph (self=0x7ffc7ad033a0, canvas=0x7ffc7ad01528, 
    glyph_id=3, point_size=16.1290321, transform=..., hinting_options=..., 
    rasterization_options=font_kit::canvas::RasterizationOptions::GrayscaleAa) at src/loaders/freetype.rs:847
#5  0x00005594d199977d in plotters::style::font::ttf::{impl#5}::draw<plotters_backend::DrawingErrorKind<plotters_bitmap::bitmap::BitMapBackendError>, plotters::style::text::{impl#15}::draw::{closure_env#0}<plotters_backend::DrawingErrorKind<plotters_bitmap::bitmap::BitMapBackendError>, plotters_backend::DrawingBackend::draw_text::{closure_env#1}<plotters_bitmap::bitmap::BitMapBackend<plotters_bitmap::bitmap::RGBPixel>, plotters::style::text::TextStyle>>> (self=0x7ffc7ad033a0, size=20, text=..., draw=...)
    at /home/ben/.cargo/registry/src/github.com-1ecc6299db9ec823/plotters-0.3.1/src/style/font/ttf.rs:264

The data=0x0 is what trips the assertion.

saethlin avatar Jun 18 '22 23:06 saethlin