Skip to content

Add cross-dimension shape validation for diff prepend/append#1152

Open
srinjoy933 wants to merge 3 commits intofortran-lang:masterfrom
srinjoy933:fix-diff2-bounds-check
Open

Add cross-dimension shape validation for diff prepend/append#1152
srinjoy933 wants to merge 3 commits intofortran-lang:masterfrom
srinjoy933:fix-diff2-bounds-check

Conversation

@srinjoy933
Copy link

This PR introduces explicit cross-dimension shape validation for the prepend and append arguments in the diff_2 routine. Previously, passing an array with a mismatched non-differencing dimension bypassed shape checks, leading to potential bounds-check faults or silent memory corruption during the internal work array assignment. The update verifies size(array, 3 - dim_) == size(x, 3 - dim_) prior to workspace allocation, ensuring safe execution.
Solves #1151

@srinjoy933
Copy link
Author

@jvdp1 @jalvesz please review this pr once, kindly give me your suggestions whether any changes are required or not. Thank You!

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds explicit cross-dimension (non-differencing dimension) shape validation for diff_2’s optional prepend/append arguments to prevent out-of-bounds assignment/memory corruption scenarios described in #1151.

Changes:

  • Validate size(prepend, 3-dim_) == size(x, 3-dim_) before using prepend in the internal workspace.
  • Validate size(append, 3-dim_) == size(x, 3-dim_) before using append in the internal workspace.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +90 to +97
error stop "stdlib_math_diff: non-differencing dimension of 'prepend' must match 'x'"
end if
size_prepend = size(prepend, dim_)
end if

if (present(append)) then
if (size(append, 3 - dim_) /= size(x, 3 - dim_)) then
error stop "stdlib_math_diff: non-differencing dimension of 'append' must match 'x'"
Copy link

Copilot AI Mar 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error stop is an image control statement and is not permitted inside a pure procedure by the Fortran standard. Since diff_2_* is declared pure, these new error stop statements can make the code non-conforming (and may fail to compile with stricter compilers). Consider replacing this with a pure-friendly error reporting mechanism (e.g., return a zero-sized result and/or add an optional status/state argument), or drop pure consistently (including the public interface in stdlib_math.fypp) if terminating is required.

Suggested change
error stop "stdlib_math_diff: non-differencing dimension of 'prepend' must match 'x'"
end if
size_prepend = size(prepend, dim_)
end if
if (present(append)) then
if (size(append, 3 - dim_) /= size(x, 3 - dim_)) then
error stop "stdlib_math_diff: non-differencing dimension of 'append' must match 'x'"
allocate(y(0, 0))
return
end if
size_prepend = size(prepend, dim_)
end if
if (present(append)) then
if (size(append, 3 - dim_) /= size(x, 3 - dim_)) then
allocate(y(0, 0))
return

Copilot uses AI. Check for mistakes.
Comment on lines +95 to +99
if (present(append)) then
if (size(append, 3 - dim_) /= size(x, 3 - dim_)) then
error stop "stdlib_math_diff: non-differencing dimension of 'append' must match 'x'"
end if
size_append = size(append, dim_)
Copy link

Copilot AI Mar 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same concern as above: introducing error stop inside pure diff_2_* is non-conforming and can break compilation. If the intent is to hard-fail on invalid append shapes, the procedure (and its public interface) likely needs to be made non-pure, or the API should be adjusted to report shape errors without image control statements.

Copilot uses AI. Check for mistakes.
Comment on lines +88 to +99
if (present(prepend)) then
if (size(prepend, 3 - dim_) /= size(x, 3 - dim_)) then
error stop "stdlib_math_diff: non-differencing dimension of 'prepend' must match 'x'"
end if
size_prepend = size(prepend, dim_)
end if

if (present(append)) then
if (size(append, 3 - dim_) /= size(x, 3 - dim_)) then
error stop "stdlib_math_diff: non-differencing dimension of 'append' must match 'x'"
end if
size_append = size(append, dim_)
Copy link

Copilot AI Mar 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds new shape-validation behavior for diff_2 when prepend/append are present, but there doesn't appear to be a regression test covering the mismatched cross-dimension case that previously caused bounds issues. Consider adding a dedicated test that exercises a mismatched prepend/append shape and asserts failure (e.g., via a CTest WILL_FAIL test) so the bug doesn’t regress.

Copilot uses AI. Check for mistakes.
@srinjoy933
Copy link
Author

srinjoy933 commented Mar 23, 2026

@jvdp1 @jalvesz please review this pr, I have made some changes according to what the copilot has suggested . Waiting for your feedback.

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