aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeffrey Armstrong <jeff@approximatrix.com>2020-08-08 12:18:08 -0400
committerJeffrey Armstrong <jeff@approximatrix.com>2020-08-08 12:18:08 -0400
commite965762450457f8033397289c5f120475da3a039 (patch)
tree90fd3b10b0724aeedf2fa3801385c2973c5182d5
parentf833fc391d3192733c799dcdaf1c4df4c9919f8b (diff)
downloadLR-87-e965762450457f8033397289c5f120475da3a039.zip
LR-87-e965762450457f8033397289c5f120475da3a039.tar.gz
Fixed array overrun due to conversion of string from c pointer to Fortran pointer
-rw-r--r--network.F9022
1 files changed, 13 insertions, 9 deletions
diff --git a/network.F90 b/network.F90
index 637d092..8e77b9d 100644
--- a/network.F90
+++ b/network.F90
@@ -264,6 +264,15 @@ implicit none
! To process h_name
character(kind=c_char), dimension(:), pointer::h_name
+ integer::hnamelen
+ interface
+ function strlen_c(cstr) bind(c, name="strlen")
+ use iso_c_binding
+ type(c_ptr), value::cstr
+ integer(kind=c_size_t)::strlen_c
+ end function strlen_c
+ end interface
+
! To process h_addr
type(c_ptr), dimension(:), pointer::addrptr
@@ -282,16 +291,11 @@ implicit none
call c_f_pointer(callres, cres)
! Extract the name
- call c_f_pointer(cres%h_name, h_name, [1])
- i = 1
- do while(h_name(i) /= c_null_char)
- i = i + 1
- end do
- allocate(character(len=i-1) :: res%h_name)
- i = 1
- do while(h_name(i) /= c_null_char)
+ hnamelen = strlen_c(cres%h_name)
+ call c_f_pointer(cres%h_name, h_name, [hnamelen])
+ allocate(character(len=i) :: res%h_name)
+ do i = 1, hnamelen
res%h_name(i:i) = h_name(i)
- i = i + 1
end do
! And address