Skip to content

Commit

Permalink
Handle failure to allocate QR code on panic (#693)
Browse files Browse the repository at this point in the history
If the allocation for the working space and the result of the QR code
fails, then we don't want to panic but just not render the QR code.

- [x] no changelog update needed
  • Loading branch information
gwilymk committed May 14, 2024
2 parents f80b180 + ae2eb5b commit 30ff065
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions agb/src/panics_render.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::{fmt::Write, panic::PanicInfo};

use alloc::{format, vec};
use alloc::{format, vec::Vec};

use crate::{
backtrace,
Expand Down Expand Up @@ -66,9 +66,15 @@ pub fn render_backtrace(trace: &backtrace::Frames, info: &PanicInfo) -> ! {
/// Returns the width / height of the QR code + padding in pixels
fn draw_qr_code(gfx: &mut Bitmap3<'_>, qrcode_string_data: &str) -> i32 {
const MAX_VERSION: qrcodegen_no_heap::Version = qrcodegen_no_heap::Version::new(6);

let mut temp_buffer = vec![0; MAX_VERSION.buffer_len()];
let mut out_buffer = vec![0; MAX_VERSION.buffer_len()];
const PADDING: i32 = 8;

let (Ok(mut temp_buffer), Ok(mut out_buffer)) = (
Vec::try_with_capacity_in(MAX_VERSION.buffer_len(), crate::ExternalAllocator),
Vec::try_with_capacity_in(MAX_VERSION.buffer_len(), crate::ExternalAllocator),
) else {
crate::println!("Failed to allocate memory to generate QR code");
return PADDING;
};

let qr_code = match qrcodegen_no_heap::QrCode::encode_text(
qrcode_string_data,
Expand All @@ -83,7 +89,7 @@ fn draw_qr_code(gfx: &mut Bitmap3<'_>, qrcode_string_data: &str) -> i32 {
Ok(qr_code) => qr_code,
Err(e) => {
crate::println!("Error generating qr code: {e:?}");
return 8;
return PADDING;
}
};

Expand All @@ -98,5 +104,5 @@ fn draw_qr_code(gfx: &mut Bitmap3<'_>, qrcode_string_data: &str) -> i32 {
}
}

qr_code.size() * 2 + 8 * 2
qr_code.size() * 2 + PADDING * 2
}

0 comments on commit 30ff065

Please sign in to comment.