Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array lengths should be explicit in fortran interface #344

Open
jhp-lanl opened this issue Feb 14, 2024 · 0 comments
Open

Array lengths should be explicit in fortran interface #344

jhp-lanl opened this issue Feb 14, 2024 · 0 comments
Assignees
Labels
bug Something isn't working enhancement New feature or request interface Library interface work

Comments

@jhp-lanl
Copy link
Collaborator

Much (if not all) of the fortran interface currently uses function signatures that look like

integer function fun_f(val, array_vals) result(err)
    integer, value, intent(in) :: val
    integer, dimension(:), target, intent(inout) :: array_vals

    err = fun(val, c_loc(array_vals))

where the dimension of the dummy variable is not explicitly defined. Since all that's passed over the C++ side is the pointer to the array, it is very easy for the array to be under-allocated and go out of bounds.

In particular, if we choose to change the length of array_vals on the C++ side, we have no way of alerting the Fortran host codes that the length has changed and they need to allocate more space.

I think it would be better if our interface dummy arguments use explicit lengths so that fortran can do length checking at compile time. Then the interface will look like

integer function fun_f(val, array_vals) result(err)
    integer, value, intent(in) :: val
    integer, dimension(4), target, intent(inout) :: array_vals

    err = fun(val, c_loc(array_vals))

This will produce a compile-time error if the host code tries to pass an array in that doesn't comply with the API we've defined:

Error: Actual argument contains too few elements for dummy argument 'array_vals' (2/4) at (1)

@dholladay00 this would fix some UB I saw in the xRAGE interface

@jhp-lanl jhp-lanl added bug Something isn't working enhancement New feature or request interface Library interface work labels Feb 14, 2024
@jhp-lanl jhp-lanl self-assigned this Feb 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request interface Library interface work
Projects
None yet
Development

No branches or pull requests

1 participant