Skip to content

Commit a25b1dd

Browse files
Merge pull request #582 from jacobwilliams/581-iostat-checks
added some iostat checks to write statements to avoid potential crashes
2 parents 1147599 + fb00a48 commit a25b1dd

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

src/json_value_module.F90

+23-5
Original file line numberDiff line numberDiff line change
@@ -6120,6 +6120,7 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
61206120

61216121
s = s_indent//start_object
61226122
call write_it()
6123+
if (json%exception_thrown) return
61236124

61246125
!if an object is in an array, there is an extra tab:
61256126
if (is_array) then
@@ -6150,6 +6151,7 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
61506151
str_escaped//quotation_mark//colon_char//space
61516152
call write_it(advance=.false.)
61526153
end if
6154+
if (json%exception_thrown) return
61536155
else
61546156
call json%throw_exception('Error in json_value_print:'//&
61556157
' element%name not allocated')
@@ -6195,6 +6197,7 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
61956197

61966198
s = s_indent//start_array
61976199
call write_it( advance=(.not. is_vector) )
6200+
if (json%exception_thrown) return
61986201

61996202
!if an array is in an array, there is an extra tab:
62006203
if (is_array) then
@@ -6223,7 +6226,6 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
62236226
need_comma=i<count, is_array_element=.true., &
62246227
str=str, iloc=iloc)
62256228
end if
6226-
if (json%exception_thrown) return
62276229

62286230
! get the next child the list:
62296231
element => element%next
@@ -6311,6 +6313,8 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
63116313

63126314
end if
63136315

6316+
if (json%exception_thrown) return
6317+
63146318
contains
63156319

63166320
subroutine write_it(advance,comma,space_after_comma)
@@ -6329,6 +6333,7 @@ subroutine write_it(advance,comma,space_after_comma)
63296333
integer(IK) :: n !! length of actual string `s` appended to `str`
63306334
integer(IK) :: room_left !! number of characters left in `str`
63316335
integer(IK) :: n_chunks_to_add !! number of chunks to add to `str` for appending `s`
6336+
integer(IK) :: istat !! `iostat` code for `write` statement
63326337

63336338
if (present(comma)) then
63346339
add_comma = comma
@@ -6368,9 +6373,14 @@ subroutine write_it(advance,comma,space_after_comma)
63686373
if (write_file) then
63696374

63706375
if (add_line_break) then
6371-
write(iunit,fmt='(A)') s
6376+
write(iunit,fmt='(A)',iostat=istat) s
63726377
else
6373-
write(iunit,fmt='(A)',advance='NO') s
6378+
write(iunit,fmt='(A)',advance='NO',iostat=istat) s
6379+
end if
6380+
if (istat/=0) then
6381+
call integer_to_string(iunit,int_fmt,tmp)
6382+
call json%throw_exception('Error in json_value_print: '//&
6383+
'could not write to file unit: '//trim(tmp))
63746384
end if
63756385

63766386
else !write string
@@ -11577,16 +11587,24 @@ subroutine json_print_error_message(json,io_unit)
1157711587

1157811588
character(kind=CK,len=:),allocatable :: error_msg !! error message
1157911589
logical :: status_ok !! false if there were any errors thrown
11590+
integer(IK) :: istat !! for write error checking
11591+
character(kind=CK,len=max_integer_str_len) :: tmp !! for int to string conversions
1158011592

1158111593
!get error message:
1158211594
call json%check_for_errors(status_ok, error_msg)
1158311595

1158411596
!print it if there is one:
1158511597
if (.not. status_ok) then
1158611598
if (present(io_unit)) then
11587-
write(io_unit,'(A)') error_msg
11599+
write(io_unit,'(A)',iostat=istat) error_msg
11600+
if (istat/=0) then
11601+
! in this case, just try to write to the error_unit
11602+
call integer_to_string(io_unit,int_fmt,tmp)
11603+
write(error_unit,'(A)',iostat=istat) 'Error writing to unit '//trim(tmp)
11604+
write(error_unit,'(A)',iostat=istat) error_msg
11605+
end if
1158811606
else
11589-
write(output_unit,'(A)') error_msg
11607+
write(output_unit,'(A)',iostat=istat) error_msg
1159011608
end if
1159111609
deallocate(error_msg)
1159211610
call json%clear_exceptions()

0 commit comments

Comments
 (0)