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

Add length bounds to string conversion function #208

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions interfaces/C/rutherford_boeing.f90
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,30 @@ subroutine convert_string_c2f(cstr, fstr)
end if
end subroutine convert_string_c2f

! WARNING: Assumes cstr points to a sufficiently large buffer.
subroutine convert_string_f2c(fstr, cstr)
! WARNING: Assumes cstr points to a buffer of size of at least `maxlen`.
subroutine convert_string_f2c(fstr, cstr, maxlen)
implicit none
character(len=*), intent(in) :: fstr
type(C_PTR), intent(inout) :: cstr
integer, intent(in) :: maxlen

integer :: len
integer :: i
character(C_CHAR), dimension(:), pointer :: cstrptr

len = len_trim(fstr) + 1
len = min(len, maxlen - 1)

if (len == 0) then
return
endif

if (C_ASSOCIATED(cstr)) then
call c_f_pointer(cstr, cstrptr, shape = (/ len_trim(fstr)+1 /))
do i = 1, len_trim(fstr)
call c_f_pointer(cstr, cstrptr, shape = (/ len /))
do i = 1, len - 1
cstrptr(i) = fstr(i:i)
end do
cstrptr(len_trim(fstr)+1) = C_NULL_CHAR
cstrptr(len) = C_NULL_CHAR
end if
end subroutine convert_string_f2c
end module spral_rutherford_boeing_ciface
Expand Down Expand Up @@ -199,11 +208,11 @@ integer(C_INT) function spral_rb_peek(filename, m, n, nelt, nvar, nval, &
temp_int = fmatrix_type
end if
if (c_associated(type_code)) &
call convert_string_f2c(ftype_code, type_code)
call convert_string_f2c(ftype_code, type_code, LEN(ftype_code))
if (c_associated(title)) &
call convert_string_f2c(ftitle, title)
call convert_string_f2c(ftitle, title, LEN(ftitle))
if (c_associated(identifier)) &
call convert_string_f2c(fidentifier, identifier)
call convert_string_f2c(fidentifier, identifier, LEN(fidentifier))
end function spral_rb_peek

integer(C_INT) function spral_rb_read(filename, handle, matrix_type, m, n, &
Expand Down Expand Up @@ -267,9 +276,9 @@ integer(C_INT) function spral_rb_read(filename, handle, matrix_type, m, n, &
if (allocated(matrix%val)) val = C_LOC(matrix%val)
! Handle optional strings
if (C_ASSOCIATED(title)) &
call convert_string_f2c(ftitle, title)
call convert_string_f2c(ftitle, title, LEN(ftitle))
if (C_ASSOCIATED(identifier)) &
call convert_string_f2c(fidentifier, identifier)
call convert_string_f2c(fidentifier, identifier, LEN(fidentifier))

! Set return code
spral_rb_read = info
Expand Down Expand Up @@ -327,9 +336,9 @@ integer(C_INT) function spral_rb_read_ptr32(filename, handle, matrix_type, &
if (allocated(matrix%val)) val = C_LOC(matrix%val)
! Handle optional strings
if (C_ASSOCIATED(title)) &
call convert_string_f2c(ftitle, title)
call convert_string_f2c(ftitle, title, LEN(ftitle))
if (C_ASSOCIATED(identifier)) &
call convert_string_f2c(fidentifier, identifier)
call convert_string_f2c(fidentifier, identifier, LEN(fidentifier))

! Set return code
spral_rb_read_ptr32 = info
Expand Down
Loading