Skip to content

1x1 matrix segfaults and missing shape mismatch checks #1161

@srinjoy933

Description

@srinjoy933

Hi everyone, while working through stdlib_specialmatrices (specifically the tridiagonal_matrices submodule), I noticed a couple of edge cases that will cause runtime crashes.

First, in the tridiagonal_to_dense_${s1}$ functions, there is an out-of-bounds memory access if a 1x1 matrix (n=1) is passed. The routine unconditionally attempts to assign B(1, 2) = A%du(1) and B(n, n-1) = A%dl(n-1). For a 1x1 matrix, B is allocated as 1x1 and the off-diagonal arrays are size 0, so this will inevitably segfault. We can easily fix this by adding an explicit if (n == 1) guard before the off-diagonal assignments.

Second, the pure arithmetic functions like matrix_add_tridiagonal_${s1}$ and matrix_sub_tridiagonal_${s1}$ do not check if matrices A and B have compatible dimensions (A%n == B%n). If a user accidentally passes matrices of different sizes, the element-wise array operations (like C%dl + B%dl) will fail with a shape mismatch at runtime. Since these are pure functions, we can't use the standard linalg_error_handling routine, so we should probably discuss whether an error stop is appropriate here or if it should be caught at a higher interface level.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions