Skip to content

Commit

Permalink
Avoid any potential out-of-bounds indexing in neasyf_type
Browse files Browse the repository at this point in the history
Allocate a local variable with the same type as the input, but with
exactly one element. This way we can be sure we can index it correctly
to call the scalar version.

This shouldn't be a problem now that we explicitly disallow zero-sized
dimensions, but it's nice to be safe in case the user created the
dimensions some other way.
  • Loading branch information
ZedThree committed Apr 4, 2023
1 parent 5a7b585 commit 1a14b84
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
28 changes: 21 additions & 7 deletions src/neasyf.f90
Original file line number Diff line number Diff line change
Expand Up @@ -280,43 +280,57 @@ end function neasyf_type_scalar
function neasyf_type_rank1(variable) result(nf_type)
integer(nf_kind) :: nf_type
class(*), dimension(:), intent(in) :: variable
nf_type = neasyf_type(variable(1))
class(*), dimension(:), allocatable :: local
allocate(local(1), mold=variable)
nf_type = neasyf_type(local(1))
end function neasyf_type_rank1

function neasyf_type_rank2(variable) result(nf_type)
integer(nf_kind) :: nf_type
class(*), dimension(:, :), intent(in) :: variable
nf_type = neasyf_type(variable(1, 1))
class(*), dimension(:, :), allocatable :: local
allocate(local(1, 1), mold=variable)
nf_type = neasyf_type(local(1, 1))
end function neasyf_type_rank2

function neasyf_type_rank3(variable) result(nf_type)
integer(nf_kind) :: nf_type
class(*), dimension(:, :, :), intent(in) :: variable
nf_type = neasyf_type(variable(1, 1, 1))
class(*), dimension(:, :, :), allocatable :: local
allocate(local(1, 1, 1), mold=variable)
nf_type = neasyf_type(local(1, 1, 1))
end function neasyf_type_rank3

function neasyf_type_rank4(variable) result(nf_type)
integer(nf_kind) :: nf_type
class(*), dimension(:, :, :, :), intent(in) :: variable
nf_type = neasyf_type(variable(1, 1, 1, 1))
class(*), dimension(:, :, :, :), allocatable :: local
allocate(local(1, 1, 1, 1), mold=variable)
nf_type = neasyf_type(local(1, 1, 1, 1))
end function neasyf_type_rank4

function neasyf_type_rank5(variable) result(nf_type)
integer(nf_kind) :: nf_type
class(*), dimension(:, :, :, :, :), intent(in) :: variable
nf_type = neasyf_type(variable(1, 1, 1, 1, 1))
class(*), dimension(:, :, :, :, :), allocatable :: local
allocate(local(1, 1, 1, 1, 1), mold=variable)
nf_type = neasyf_type(local(1, 1, 1, 1, 1))
end function neasyf_type_rank5

function neasyf_type_rank6(variable) result(nf_type)
integer(nf_kind) :: nf_type
class(*), dimension(:, :, :, :, :, :), intent(in) :: variable
nf_type = neasyf_type(variable(1, 1, 1, 1, 1, 1))
class(*), dimension(:, :, :, :, :, :), allocatable :: local
allocate(local(1, 1, 1, 1, 1, 1), mold=variable)
nf_type = neasyf_type(local(1, 1, 1, 1, 1, 1))
end function neasyf_type_rank6

function neasyf_type_rank7(variable) result(nf_type)
integer(nf_kind) :: nf_type
class(*), dimension(:, :, :, :, :, :, :), intent(in) :: variable
nf_type = neasyf_type(variable(1, 1, 1, 1, 1, 1, 1))
class(*), dimension(:, :, :, :, :, :, :), allocatable :: local
allocate(local(1, 1, 1, 1, 1, 1, 1), mold=variable)
nf_type = neasyf_type(local(1, 1, 1, 1, 1, 1, 1))
end function neasyf_type_rank7


Expand Down
4 changes: 3 additions & 1 deletion src/neasyf.type.in.f90
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
function neasyf_type_rank{n}(variable) result(nf_type)
integer(nf_kind) :: nf_type
class(*), dimension({array(n)}), intent(in) :: variable
nf_type = neasyf_type(variable({slice(n)}))
class(*), dimension({array(n)}), allocatable :: local
allocate(local({slice(n)}), mold=variable)
nf_type = neasyf_type(local({slice(n)}))
end function neasyf_type_rank{n}

0 comments on commit 1a14b84

Please sign in to comment.