From 99afeeb66f11beaeaf72b9e7620b18a2e38e5524 Mon Sep 17 00:00:00 2001 From: srinjoy933 Date: Sun, 22 Mar 2026 18:49:33 +0530 Subject: [PATCH 1/3] fix(math): add cross-dimension shape validation for diff prepend/append --- src/math/stdlib_math_diff.fypp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/math/stdlib_math_diff.fypp b/src/math/stdlib_math_diff.fypp index eb8cb0bc2..50fd6602f 100644 --- a/src/math/stdlib_math_diff.fypp +++ b/src/math/stdlib_math_diff.fypp @@ -85,8 +85,20 @@ contains dim_ = 1 end if - if (present(prepend)) size_prepend = size(prepend, dim_) - if (present(append)) size_append = size(append, dim_) + 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_) + end if + size_x = size(x, dim_) size_work = size_x + size_prepend + size_append From dc5a0a652eceda1f9e154bbeef45209adc883b10 Mon Sep 17 00:00:00 2001 From: srinjoy933 Date: Mon, 23 Mar 2026 17:52:23 +0530 Subject: [PATCH 2/3] fix(math): resolve pure constraint in diff_2 and add regression test --- src/math/stdlib_math_diff.fypp | 6 ++++-- test/math/test_stdlib_math.fypp | 14 +++++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/math/stdlib_math_diff.fypp b/src/math/stdlib_math_diff.fypp index 50fd6602f..084375e6f 100644 --- a/src/math/stdlib_math_diff.fypp +++ b/src/math/stdlib_math_diff.fypp @@ -87,14 +87,16 @@ contains 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'" + 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 - error stop "stdlib_math_diff: non-differencing dimension of 'append' must match 'x'" + allocate(y(0, 0)) + return end if size_append = size(append, dim_) end if diff --git a/test/math/test_stdlib_math.fypp b/test/math/test_stdlib_math.fypp index ea355cd02..4b9dbfc67 100644 --- a/test/math/test_stdlib_math.fypp +++ b/test/math/test_stdlib_math.fypp @@ -583,12 +583,13 @@ contains end subroutine test_all_close_cmplx_${k1}$ #:endfor - #:for k1, t1 in REAL_KINDS_TYPES + #:for k1, t1 in REAL_KINDS_TYPES subroutine test_diff_real_${k1}$(error) type(error_type), allocatable, intent(out) :: error ${t1}$ :: x(6) = [${t1}$ :: 0, 5, 15, 30, 50, 75] ${t1}$ :: A(1, 3) = reshape([${t1}$ :: 1, 3, 5], [1, 3]) ${t1}$ :: B(2) = [${t1}$ :: 1, 2] + ${t1}$ :: p_bad(1, 4) = reshape([${t1}$ :: 2, 2, 2, 2], [1, 4]) !> rank-1 diff call check(error, all_close(diff(x), [${t1}$ :: 5, 10, 15, 20, 25]), & @@ -626,6 +627,17 @@ contains call check(error, size(diff(B, 2)), 0, "size(diff(B, 2)) in test_diff_real_${k1}$ failed") if (allocated(error)) return call check(error, size(diff(B, 3)), 0, "size(diff(B, 3)) in test_diff_real_${k1}$ failed") + if (allocated(error)) return + + ! --- REGRESSION TEST START --- + !> Mismatched prepend/append cross-dimensions should return zero-sized array + call check(error, size(diff(A, dim=1, prepend=p_bad)), 0, & + "prepend shape mismatch test in test_diff_real_${k1}$ failed") + if (allocated(error)) return + call check(error, size(diff(A, dim=1, append=p_bad)), 0, & + "append shape mismatch test in test_diff_real_${k1}$ failed") + if (allocated(error)) return + ! --- REGRESSION TEST END --- end subroutine test_diff_real_${k1}$ #:endfor From f054da68801a6f2a18628118abd576dbf5544b72 Mon Sep 17 00:00:00 2001 From: srinjoy933 Date: Tue, 24 Mar 2026 23:56:35 +0530 Subject: [PATCH 3/3] updated 3 --- src/math/stdlib_math_diff.fypp | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/math/stdlib_math_diff.fypp b/src/math/stdlib_math_diff.fypp index 6bd5b4a70..bad2e7e2c 100644 --- a/src/math/stdlib_math_diff.fypp +++ b/src/math/stdlib_math_diff.fypp @@ -75,28 +75,22 @@ contains size_prepend = 0 size_append = 0 + + dim_ = 1 if (present(dim)) then - if (dim == 1 .or. dim == 2) then - dim_ = dim - else - dim_ = 1 - end if - else - dim_ = 1 + if (dim == 1 .or. dim == 2) dim_ = dim end if - + if (present(prepend)) then if (size(prepend, 3 - dim_) /= size(x, 3 - dim_)) then - allocate(y(0, 0)) - return + 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 - allocate(y(0, 0)) - return + error stop "stdlib_math_diff: non-differencing dimension of 'append' must match 'x'" end if size_append = size(append, dim_) end if @@ -110,8 +104,6 @@ contains allocate(y(0, size(x, 2))) case (2) allocate(y(size(x, 1), 0)) - case default - error stop "diff_2: internal error: invalid dimension (dim_ must be 1 or 2)" end select return end if