Skip to content

Commit

Permalink
Merge pull request #582 from jacobwilliams/581-iostat-checks
Browse files Browse the repository at this point in the history
added some iostat checks to write statements to avoid potential crashes
  • Loading branch information
jacobwilliams authored Feb 22, 2025
2 parents 1147599 + fb00a48 commit a25b1dd
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/json_value_module.F90
Original file line number Diff line number Diff line change
Expand Up @@ -6120,6 +6120,7 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&

s = s_indent//start_object
call write_it()
if (json%exception_thrown) return

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

s = s_indent//start_array
call write_it( advance=(.not. is_vector) )
if (json%exception_thrown) return

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

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

end if

if (json%exception_thrown) return

contains

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

if (present(comma)) then
add_comma = comma
Expand Down Expand Up @@ -6368,9 +6373,14 @@ subroutine write_it(advance,comma,space_after_comma)
if (write_file) then

if (add_line_break) then
write(iunit,fmt='(A)') s
write(iunit,fmt='(A)',iostat=istat) s
else
write(iunit,fmt='(A)',advance='NO') s
write(iunit,fmt='(A)',advance='NO',iostat=istat) s
end if
if (istat/=0) then
call integer_to_string(iunit,int_fmt,tmp)
call json%throw_exception('Error in json_value_print: '//&
'could not write to file unit: '//trim(tmp))
end if

else !write string
Expand Down Expand Up @@ -11577,16 +11587,24 @@ subroutine json_print_error_message(json,io_unit)

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

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

!print it if there is one:
if (.not. status_ok) then
if (present(io_unit)) then
write(io_unit,'(A)') error_msg
write(io_unit,'(A)',iostat=istat) error_msg
if (istat/=0) then
! in this case, just try to write to the error_unit
call integer_to_string(io_unit,int_fmt,tmp)
write(error_unit,'(A)',iostat=istat) 'Error writing to unit '//trim(tmp)
write(error_unit,'(A)',iostat=istat) error_msg
end if
else
write(output_unit,'(A)') error_msg
write(output_unit,'(A)',iostat=istat) error_msg
end if
deallocate(error_msg)
call json%clear_exceptions()
Expand Down

0 comments on commit a25b1dd

Please sign in to comment.