mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
fix(engine): check memory before a Type Mask builds any selection over the text's band (#12074)
This commit is contained in:
1 parent
afe22a9615
commit
c3efacb333
2 files changed
+25
-22
No files matched your search
@@ -93,20 +93,17 @@ fn create_selection(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
t.antialias = aa;
|
||||
}
|
||||
let (mask, text_area) = coverage(&doc, &t)?;
|
||||
// With an unusual earlier selection (not 8-bit grey), `combine_band` keeps its format through
|
||||
// buffers spanning the text, made where running out of memory aborts: check first that they
|
||||
// fit, so a text too big for them is an error that changes nothing. Per pixel that is up to:
|
||||
// the combined grey tiles and their float copy (1 + 4), the copy widened to every channel and
|
||||
// its growth while collected (8 per channel), and the earlier selection's own new tiles. An
|
||||
// Intersect reads the earlier selection over the band the same way.
|
||||
if mode != SelectionMode::Replace
|
||||
&& let Some(old) = doc.selection.as_ref().filter(|o| o.format() != photocraft_color::PixelFormat::GRAY8)
|
||||
{
|
||||
let per_pixel = 5 + 8 * old.channels() + old.format().bytes_per_pixel();
|
||||
let bytes = (text_area.width() as usize).checked_mul(text_area.height() as usize).and_then(|n| n.checked_mul(per_pixel));
|
||||
let bytes = bytes.ok_or_else(|| crate::allocation::error(PURPOSE, "requested size overflowed"))?;
|
||||
drop(crate::allocation::capacity::<u8>(bytes, PURPOSE)?);
|
||||
}
|
||||
// `combine_band` builds the new selection over the text's band where running out of memory
|
||||
// aborts, and the band is no longer bounded by the canvas: check first that it fits, so a text
|
||||
// too big for it is an error that changes nothing. Per pixel that is the new 8-bit grey tiles.
|
||||
// With an unusual earlier selection (not 8-bit grey) that the mode reads, it keeps its format
|
||||
// through more: the combined grey tiles and their float copy (1 + 4), the copy widened to every
|
||||
// channel and its growth while collected (8 per channel), and its own new tiles.
|
||||
let unusual = doc.selection.as_ref().filter(|o| mode != SelectionMode::Replace && o.format() != photocraft_color::PixelFormat::GRAY8);
|
||||
let per_pixel = unusual.map_or(1, |old| 5 + 8 * old.channels() + old.format().bytes_per_pixel());
|
||||
let bytes = (text_area.width() as usize).checked_mul(text_area.height() as usize).and_then(|n| n.checked_mul(per_pixel));
|
||||
let bytes = bytes.ok_or_else(|| crate::allocation::error(PURPOSE, "requested size overflowed"))?;
|
||||
drop(crate::allocation::capacity::<u8>(bytes, PURPOSE)?);
|
||||
let selected = s.edit("Type Mask", |doc, active| {
|
||||
// Replace and Intersect leave nothing outside the text; Add and Subtract keep the rest of
|
||||
// the selection as it is, its part past the canvas too, without a buffer spanning both.
|
||||
|
||||
@@ -192,8 +192,9 @@ fn replace_and_intersect_drop_an_off_canvas_previous_selection() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Adding to an unusual earlier selection (not 8-bit grey) keeps its format; when its buffers over
|
||||
/// the text can't be had, that's an error that changes nothing, not an abort.
|
||||
/// Adding to an unusual earlier selection (not 8-bit grey) keeps its format; when the buffers that
|
||||
/// combining makes over the text can't be had, for any selection, that's an error that changes
|
||||
/// nothing, not an abort.
|
||||
#[test]
|
||||
fn an_unusual_earlier_selection_keeps_its_format_or_fails_cleanly() {
|
||||
use photocraft_color::{PixelFormat, SampleType};
|
||||
@@ -218,10 +219,15 @@ fn an_unusual_earlier_selection_keeps_its_format_or_fails_cleanly() {
|
||||
assert_eq!(now.format(), format);
|
||||
assert_eq!(now.read_region(earlier), old.read_region(earlier), "the earlier part stays, past the canvas too");
|
||||
|
||||
// The third attempt is that check: an 8-bit grey earlier selection needs none, so the same
|
||||
// failure point is never reached and the mask goes through.
|
||||
let mut s = session(600, 400);
|
||||
s.execute("select.rect", json!({"x": 10, "y": 50, "width": 100, "height": 70})).unwrap();
|
||||
crate::allocation::fail_after_for_test(2);
|
||||
s.execute(super::CMD, text).unwrap();
|
||||
// An 8-bit grey earlier selection, in every mode, gets the same check for its new tiles.
|
||||
for operation in ["add", "subtract", "new", "intersect"] {
|
||||
let mut s = session(600, 400);
|
||||
s.execute("select.rect", json!({"x": 10, "y": 50, "width": 100, "height": 70})).unwrap();
|
||||
let (before, steps) = (selection(&s), s.active().unwrap().history.entries().len());
|
||||
crate::allocation::fail_after_for_test(2);
|
||||
let err = s.execute(super::CMD, json!({"text": "I", "x": 400, "y": 300, "size": 48, "operation": operation})).unwrap_err().to_string();
|
||||
assert!(err.contains("not enough memory for the Type Mask selection"), "{operation}: {err}");
|
||||
assert_eq!(selection(&s), before, "{operation}: unchanged");
|
||||
assert_eq!(s.active().unwrap().history.entries().len(), steps, "{operation}: no history step");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user