Skip to content

Commit

Permalink
Prevent string out of bounds. (#46)
Browse files Browse the repository at this point in the history
* Prevent string out of bounds.

* Update string limit in copy_c_ptr_to_string.

* Increase size of buffer in c_ptr_to_string.
  • Loading branch information
DJDavies2 authored Jul 26, 2024
1 parent df7f157 commit 4d812b9
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/fckit/module/fckit_C_interop.F90
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,11 @@ function c_str_to_string(s) result(string)

subroutine copy_c_str_to_string(s,string)
use, intrinsic :: iso_c_binding
character(kind=c_char,len=1), intent(in) :: s(*)
character(kind=c_char,len=1), intent(in) :: s(:)
character(len=:), allocatable :: string
integer i, nchars
i = 1
do
do i = 1, size(s)
if (s(i) == c_null_char) exit
i = i + 1
enddo
nchars = i - 1 ! Exclude null character from Fortran string
FCKIT_ALLOCATE_CHARACTER(string,nchars)
Expand All @@ -183,7 +181,7 @@ subroutine copy_c_ptr_to_string(cptr,string)
type(c_ptr), intent(in) :: cptr
character(kind=c_char,len=:), allocatable :: string
character(kind=c_char), dimension(:), pointer :: s
integer(c_int), parameter :: MAX_STR_LEN = 255
integer(c_int), parameter :: MAX_STR_LEN = 2550
call c_f_pointer ( cptr , s, (/MAX_STR_LEN/) )
call copy_c_str_to_string( s, string )
end subroutine
Expand All @@ -195,7 +193,7 @@ function c_ptr_to_string(cptr) result(string)
type(c_ptr), intent(in) :: cptr
character(kind=c_char,len=:), allocatable :: string
character(kind=c_char), dimension(:), pointer :: s
integer(c_int), parameter :: MAX_STR_LEN = 255
integer(c_int), parameter :: MAX_STR_LEN = 2550
call c_f_pointer ( cptr , s, (/MAX_STR_LEN/) )
call copy_c_str_to_string( s, string )
end function
Expand Down

0 comments on commit 4d812b9

Please sign in to comment.