Silverfrost Forums

Welcome to our forums

Compiler crash attempting substring of character function

3 Mar 2008 3:44 (Edited: 3 Mar 2008 3:46) #2874

FTN95 crashes when attempting to compile the following code, the offending statement being, WRITE (,) Buff_toStr(buffer)(1:32768).

! ---------------------------------------------------------------
! MODULE WX_Buffer
!
! This module implements a fixed size string buffer.
!
! As currently implemented the limits of this buffer are as follows.
!
!   Maximum size of buffer   =  1 Mbyte = 1048576 bytes (characters)
!
!

MODULE WX_Buffer

  IMPLICIT NONE
  PRIVATE

  INTEGER(KIND=SELECTED_INT_KIND(9)), &
                      PARAMETER :: long   = SELECTED_INT_KIND(9)

  INTEGER(KIND=long), PARAMETER :: MAX_BUFF_SIZE = 1048576

  TYPE :: buffer_t
    PRIVATE
    INTEGER(KIND=long)           :: size = 0
    CHARACTER(LEN=MAX_BUFF_SIZE) :: str  = ''
  END TYPE

  INTERFACE Buff_Add
    MODULE PROCEDURE Buff_AddStr
  END INTERFACE

  PUBLIC buffer_t,   &
         Buff_Reset, &
         Buff_Add,   &
         Buff_Len,   &
         Buff_toStr

  PUBLIC OPERATOR (.EQUAL.)

CONTAINS

!----------------------------------------------------------------
! Resets the buffer to empty.
!
  SUBROUTINE Buff_Reset (buffer)
    TYPE(buffer_t), INTENT(INOUT) :: buffer

    buffer%size = 0
    buffer%str  = ''
  END SUBROUTINE


!----------------------------------------------------------------
! Adds character string to buffer.  Used for defining generic Buff_Add
! routine.  Sets fOver to true if adding the string would overflow the
! buffer.
!
  SUBROUTINE Buff_AddStr (buffer, s, fOver)            !PRIVATE
    TYPE(buffer_t),   INTENT(INOUT) :: buffer
    CHARACTER(LEN=*), INTENT(IN)    :: s
    LOGICAL,          INTENT(out)   :: fOver

    INTEGER(KIND=long) :: nextSize

    nextSize = buffer%size + LEN(s)
    fOver    = (nextSize > MAX_BUFF_SIZE)
    IF (fOver) THEN
      buffer%size = MAX_BUFF_SIZE
    ELSE
      buffer%str(buffer%size+1: nextSize) = s
      buffer%size = nextSize
    ENDIF

  END SUBROUTINE

!----------------------------------------------------------------
! Returns current contents of buffer as a string.
!
  FUNCTION Buff_toStr (buffer) result(str)
    TYPE(buffer_t), INTENT(IN) :: buffer
    CHARACTER(LEN=buffer%size) :: str

    str = buffer%str(1: buffer%size)

  END FUNCTION

!----------------------------------------------------------------
! Returns current length of buffer.
!
  FUNCTION Buff_Len (buffer)
    INTEGER(KIND=long)         :: Buff_Len
    TYPE(buffer_t), INTENT(IN) :: buffer

    Buff_Len = buffer%size

  END FUNCTION

!----------------------------------------------------------------


END MODULE


  PROGRAM Buffy

    USE WX_Buffer

    LOGICAL :: fOver

    TYPE(buffer_t) :: buffer

    DO i = 1, 1000
      CALL Buff_Add (buffer, &
        'If you are out to describe the truth, leave elegance to the tailor.', fOver)

    END DO

    WRITE (*,*) Buff_toStr(buffer)(1:32768)

  END

! =================================================================

Keith

3 Mar 2008 3:45 #2875
Runtime error from program:c:\\program files\\silverfrost\\ftn95\\ftn95.exe
Access Violation
The instruction at address 00522b96 attempted to read from location 00000218

 00522b26 determine_index_register(<ptr>structÄscoped_entity,enumÄlogical) [+0070]
 00522dfb memory_reference(structÄtree_ptr,enumÄop_type) [+0167]
 0051d0e2 code_generator(<ref>structÄtree_ptr) [+46b3]
 004760a1 do_function_rec(<ptr>structÄtree_record) [+03f1]
 0051d0e2 code_generator(<ref>structÄtree_ptr) [+46b3] [recur=  1] 
 00479f25 do_function_top(<ptr>structÄtree_record) [+126f]
 0051d0e2 code_generator(<ref>structÄtree_ptr) [+46b3]
 00416981 end_function(int) [+10f0]

eax=00000210   ebx=04a5033c   ecx=00000000
edx=00000000   esi=00000001   edi=04a50140
ebp=0383ef08   esp=0383eedc   IOPL=2
ds=0023   es=0023   fs=003b
gs=0000   cs=001b   ss=0023
flgs=00210246 [NC EP ZR SN DN NV]

 00522b96  mov      ecx,[eax+0x8] 
 00522b99  mov      [ebp-0x14],ecx 
 00522b9c  mov      edi,[ebx+0x58] 
5 Mar 2008 5:05 #2887

Thank you for the bug report. The compiler ought to report a programming error on the line:

WRITE (,) Buff_toStr(buffer)(1:32768)

You cannot take a substring of a function returning a character variable.

The compiler has missed the error and then tried to produce executable code.

5 Apr 2008 3:19 #2987

FTN95 now reports this as an error.

16 Sep 2025 6:51 #32346

I don't know whether this is a related problem, but if I try to read a line from an external file into a character that is declared as Len=32678, then Len_Trim seems to return the same as Len regardless of the length of the input line.

16 Sep 2025 7:21 #32347

Simon

Please provide sample code and the content of a file that illustrates the problem.

Please login to reply.