mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Brushes: keep the 512 MiB .abr cap on 32-bit targets
On wasm32 a near-1 GiB .abr arrives as base64 and is copied several times before decoding; with a 1 GiB sample budget the import could pass 4 GiB and abort the tab instead of failing cleanly. Pick the decoder budget by pointer width (like MAX_DECODED_BYTES) and derive the engine's file cap from it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
4992a4305e
commit
d53101c9a1
3 files changed
+12
-6
No files matched your search
@@ -28,7 +28,9 @@ fn always(_: &Session) -> std::result::Result<(), String> {
|
||||
/// Largest accepted file (Photoshop's biggest bundled sets are well under this).
|
||||
pub const MAX_FILE_BYTES: u64 = 512 << 20;
|
||||
/// Largest accepted `.abr` file: brush libraries of large sampled tips outgrow [`MAX_FILE_BYTES`].
|
||||
pub const MAX_ABR_FILE_BYTES: u64 = 1 << 30;
|
||||
/// Tied to the decoder's sample budget (1 GiB, 512 MiB on 32-bit targets) so it never refuses
|
||||
/// what this accepts.
|
||||
pub const MAX_ABR_FILE_BYTES: u64 = photocraft_psd::abr::MAX_TOTAL_BYTES as u64;
|
||||
|
||||
/// File bytes from `data` (base64) or `path`, plus a default group name.
|
||||
pub(crate) fn file_bytes(p: &Value, cmd: &str, what: &str) -> Result<(Vec<u8>, String)> {
|
||||
@@ -597,8 +599,9 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn abr_files_get_a_larger_size_cap() {
|
||||
// The decoder must not refuse what the reader accepts.
|
||||
const _: () = assert!(MAX_ABR_FILE_BYTES == 1 << 30 && photocraft_psd::abr::MAX_TOTAL_BYTES as u64 == MAX_ABR_FILE_BYTES);
|
||||
// 1 GiB on 64-bit; 32-bit (wasm) keeps 512 MiB so a big file fails cleanly instead of aborting.
|
||||
const WANT: u64 = if cfg!(target_pointer_width = "64") { 1 << 30 } else { 512 << 20 };
|
||||
const _: () = assert!(MAX_ABR_FILE_BYTES == WANT && MAX_ABR_FILE_BYTES >= MAX_FILE_BYTES);
|
||||
let dir = std::env::temp_dir().join(format!("pc-abr-cap-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join("cap.abr");
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
//!
|
||||
//! Options (defaults in brackets): `--brushes N` [600], `--edge PX` largest tip edge [2000],
|
||||
//! `--min-edge PX` smallest tip edge [800], `--depth 8|16` [16], `--per-file N` presets per file [100],
|
||||
//! `--raw` store tips uncompressed (bigger files). Each file stays under the reader's 1 GiB decoded cap.
|
||||
//! `--raw` store tips uncompressed (bigger files). Each file stays under the reader's decoded cap (1 GiB; 512 MiB on 32-bit).
|
||||
#![allow(clippy::unwrap_used, clippy::expect_used)]
|
||||
|
||||
use photocraft_paint::procedural::*;
|
||||
|
||||
@@ -43,8 +43,11 @@ use crate::patterns::{PatternBudget, PsdPattern, parse_pattern_block_with, write
|
||||
pub const MAX_EDGE: u32 = 8192;
|
||||
/// Most brushes read from one file.
|
||||
pub const MAX_BRUSHES: usize = 20_000;
|
||||
/// Cap on decoded sample bytes over the whole file.
|
||||
pub const MAX_TOTAL_BYTES: usize = 1 << 30;
|
||||
/// Cap on decoded sample bytes over the whole file. 32-bit targets (wasm) keep 512 MiB: the file,
|
||||
/// its base64 copies and the decoded tips must all fit a 4 GiB address space, and running out
|
||||
/// there aborts instead of returning an error.
|
||||
pub const MAX_TOTAL_BYTES: usize = if cfg!(target_pointer_width = "64") { 1 << 30 } else { 512 << 20 };
|
||||
const _: () = assert!(cfg!(target_pointer_width = "64") || MAX_TOTAL_BYTES == 512 << 20);
|
||||
|
||||
/// A sampled tip: one grayscale plane where the maximum value is full paint.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
|
||||
Reference in new issue
Block a user