Skip to content

Commit

Permalink
230517.185647.CST fix "buffer overflow" of ntest
Browse files Browse the repository at this point in the history
  • Loading branch information
zaikunzhang committed May 17, 2023
1 parent 8477aa5 commit 311f2b3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
22 changes: 20 additions & 2 deletions fortran/common/fprint.f90
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module fprint_mod
!
! Started: July 2020
!
! Last Modified: Monday, May 08, 2023 PM06:15:46
! Last Modified: Wednesday, May 17, 2023 PM06:04:21
!--------------------------------------------------------------------------------------------------!

implicit none
Expand Down Expand Up @@ -41,7 +41,10 @@ subroutine fprint(string, funit, fname, faction)
character(len=:), allocatable :: fstat
character(len=:), allocatable :: position
integer :: funit_loc
integer :: i
integer :: iostat
integer :: j
integer :: slen
logical :: fexist

! Preconditions
Expand Down Expand Up @@ -117,7 +120,22 @@ subroutine fprint(string, funit, fname, faction)
end if

! Print the string.
write (funit_loc, '(1A)') string
! `WRITE (FUNIT_LOC, '(A)') STRING` would do what we want, but it causes "Buffer overflow on output"
! if string is long.This did occur with NAG Fortran Compiler R7.1(Hanzomon) Build 7122. To avoid
! this problem, we print the string line by line, separated by NEW_LINE('').
i = 1
slen = len(string)
do while (.true.)
j = index(string(i:slen), new_line(''))
if (j == 0) then ! No more NEW_LINE('') in the string.
exit
end if
write (funit_loc, '(A)') string(i:i + j - 2)
i = i + j
end do
if (string(i:slen) /= '') then
write (funit_loc, '(A)') string(i:slen)
end if

! Close the file if necessary.
if (len(fname_loc) > 0 .and. iostat == 0) then
Expand Down
24 changes: 22 additions & 2 deletions matlab/mex_gateways/fprint.F90
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module fprint_mod
!
! Started: July 2020
!
! Last Modified: Monday, May 08, 2023 PM06:15:37
! Last Modified: Wednesday, May 17, 2023 PM06:55:49
!--------------------------------------------------------------------------------------------------!

! N.B.: INT32_MEX is indeed INT32, i.e., the kind of INTEGER*4. We name it INT32_MEX instead of
Expand Down Expand Up @@ -66,7 +66,10 @@ subroutine fprint(string, funit, fname, faction)
character(len=:), allocatable :: fstat
character(len=:), allocatable :: position
integer :: funit_loc
integer :: i
integer :: iostat
integer :: j
integer :: slen
integer(INT32_MEX) :: k
logical :: fexist

Expand Down Expand Up @@ -154,8 +157,25 @@ subroutine fprint(string, funit, fname, faction)
call warning(srname, 'Failed to open file '//fname_loc)
return
end if

! Print the string.
write (funit_loc, '(1A)') string
! `WRITE (FUNIT_LOC, '(A)') STRING` would do what we want, but it causes "Buffer overflow on
! output" if string is long.This did occur with NAG Fortran Compiler R7.1(Hanzomon) Build 7122.
! To avoid this problem, we print the string line by line, separated by NEW_LINE('').
i = 1
slen = len(string)
do while (.true.)
j = index(string(i:slen), new_line(''))
if (j == 0) then ! No more NEW_LINE('') in the string.
exit
end if
write (funit_loc, '(A)') string(i:i + j - 2)
i = i + j
end do
if (string(i:slen) /= '') then
write (funit_loc, '(A)') string(i:slen)
end if

! Close the file.
close (funit_loc)
end if
Expand Down

1 comment on commit 311f2b3

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@check-spelling-bot Report

🔴 Please review

See the 📜action log or 👼 SARIF report for details.

Unrecognized words (1)

Hanzomon

To accept ✔️ these unrecognized words as correct, run the following commands

... in a clone of the [email protected]:libprima/prima.git repository
on the main branch (ℹ️ how do I use this?):

curl -s -S -L 'https://raw.githubusercontent.com/check-spelling/check-spelling/main/apply.pl' |
perl - 'https://github.com/libprima/prima/actions/runs/5002377024/attempts/1'
If the flagged items are 🤯 false positives

If items relate to a ...

  • binary file (or some other file you wouldn't want to check at all).

    Please add a file path to the excludes.txt file matching the containing file.

    File paths are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your files.

    ^ refers to the file's path from the root of the repository, so ^README\.md$ would exclude README.md (on whichever branch you're using).

  • well-formed pattern.

    If you can write a pattern that would match it,
    try adding it to the patterns.txt file.

    Patterns are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your lines.

    Note that patterns can't match multiline strings.

Please sign in to comment.