diff options
| author | salmonik <salmonik@tuta.io> | 2026-11-04 01:30:38 +0100 |
|---|---|---|
| committer | salmonik <salmonik@tuta.io> | 2026-11-04 01:30:38 +0100 |
| commit | cb6cdd22a11c45525393aefeb0fc8f9cbc384722 (patch) | |
| tree | d60bcc699bb8f6dd699f9faf5d675eb8de39f4ca | |
| parent | downloads: cap yt-dlp media size (diff) | |
| download | senator-sperma-cb6cdd22a11c45525393aefeb0fc8f9cbc384722.tar.gz senator-sperma-cb6cdd22a11c45525393aefeb0fc8f9cbc384722.zip | |
qr: cap image dimensions and allocation
| -rw-r--r-- | src/commands/qr.rs | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/src/commands/qr.rs b/src/commands/qr.rs index c0b8af0..611f43f 100644 --- a/src/commands/qr.rs +++ b/src/commands/qr.rs | |||
| @@ -61,10 +61,20 @@ pub async fn dekodowanie( | |||
| 61 | .await? | 61 | .await? |
| 62 | .bytes() | 62 | .bytes() |
| 63 | .await?; | 63 | .await?; |
| 64 | // Decoding is CPU-heavy: keep it off the async runtime. | 64 | // Decoding is CPU-heavy: keep it off the async runtime, and cap the |
| 65 | // dimensions/allocation so a small compressed image cannot expand into | ||
| 66 | // gigabytes of pixels. | ||
| 65 | let img_data = img_data.to_vec(); | 67 | let img_data = img_data.to_vec(); |
| 66 | let decoded = tokio::task::spawn_blocking(move || { | 68 | let decoded = tokio::task::spawn_blocking(move || { |
| 67 | let img = image::load_from_memory(&img_data).ok()?; | 69 | let mut reader = image::ImageReader::new(std::io::Cursor::new(&img_data)) |
| 70 | .with_guessed_format() | ||
| 71 | .ok()?; | ||
| 72 | let mut limits = image::Limits::default(); | ||
| 73 | limits.max_image_width = Some(5_000); | ||
| 74 | limits.max_image_height = Some(5_000); | ||
| 75 | limits.max_alloc = Some(128 * 1024 * 1024); | ||
| 76 | reader.limits(limits); | ||
| 77 | let img = reader.decode().ok()?; | ||
| 68 | let gray = img.to_luma8(); | 78 | let gray = img.to_luma8(); |
| 69 | decode_qr_simple(&gray) | 79 | decode_qr_simple(&gray) |
| 70 | }) | 80 | }) |
