Skip to content

Reject ragged matrices in recomb and affine#4560

Merged
lovell merged 1 commit into
lovell:mainfrom
chatman-media:fix/recomb-affine-ragged-matrix
Jun 25, 2026
Merged

Reject ragged matrices in recomb and affine#4560
lovell merged 1 commit into
lovell:mainfrom
chatman-media:fix/recomb-affine-ragged-matrix

Conversation

@chatman-media

Copy link
Copy Markdown
Contributor

recomb() and affine() flatten the supplied matrix and validate only the total element count. A non-rectangular nested array whose values happen to flatten to a valid cardinality is therefore accepted and applied with the wrong shape instead of being rejected:

sharp(input).recomb([[1, 2, 3, 4, 5], [6, 7], [8, 9]]);   // flat length 9 -> silently treated as 3x3
sharp(input).affine([[1, 2, 3], [4]]);                    // flat length 4 -> silently treated as 2x2

This validates that every row is an array of the expected length before flattening, so malformed matrices throw Invalid parameter as documented ("3x3 or 4x4" / "1x4 or 2x2"). The existing recomb "incorrect sub size" test already established the intent to reject non-rectangular matrices; this closes the gap where the row lengths cancel out.

Added unit tests for both that fail without the change. npm run lint-js (biome) clean.

The recomb and affine matrix validation flattened the input and only
checked the total number of elements, so a non-rectangular nested array
whose values happened to flatten to a valid cardinality (9 or 16 for
recomb, 4 for affine) was silently accepted and applied with the wrong
shape instead of being rejected.

Validate that every row is an array of the expected length before
flattening.
@lovell lovell merged commit 9783122 into lovell:main Jun 25, 2026
32 checks passed
@lovell

lovell commented Jun 25, 2026

Copy link
Copy Markdown
Owner

Thank you!

@lovell lovell added this to the v0.35.3 milestone Jun 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants