FIX: Concatenation of Substring Gives Incorrect Results

Last reviewed: September 11, 1997
Article ID: Q70243
4.00 4.01 4.10 5.00 5.10 | 4.10 5.00 5.10
MS-DOS                   | OS/2
kbtool kbfixlist kbbuglist

The information in this article applies to:

  • Microsoft FORTRAN for MS-DOS, versions 4.0, 4.01, 4.1, 5.0, and 5.1
  • Microsoft FORTRAN for OS/2, versions 4.1, 5.0, and 5.1

SYMPTOMS

Programs compiled with Microsoft FORTRAN versions 4.0, 4.01, 4.1, and 5.0 that concatenate substrings having a variable for a substring index, can give incorrect results or hang the machine when executed under MS-DOS, or result in a protection violation when executed under OS/2.

CAUSE

The problem is usually observed when the concatenation occurs inside a function call, or inside OPEN or IF statements.

RESOLUTION

To avoid this problem, assign the concatenated expression to a temporary character variable and use the temporary variable in the program.

STATUS

Microsoft has confirmed this to be a problem in FORTRAN versions 4.0, 4.01, 4.1, 5.0, and 5.1. This problem was corrected in FORTRAN PowerStation.

MORE INFORMATION

The following sample programs illustrate the problem:

In an IF Statement

      character r*2 /'AA'/
      n=0

      if ('BBB' .gt. r(1:n+2)//'A') then  ! The IF statement should
                                          ! cause 'YES' to be printed,
                                          ! however 'NO' is printed
                                          ! instead.
         write (*,*) 'YES'
      else
         write (*,*) 'NO'
      endif

      if ('BBB' .gt. r(1:2)//'A') then
         write (*,*) 'YES'
      else
         write (*,*) 'NO'
      endif

      end


In a Function

      character*4  a
      character*1  b

      a = 'cdef'
      b = 'd'
      i = len(a)
      print *, i
      j = index('ab'//a(1:i),b)  ! The INDEX function should cause '4'
                                 ! to be printed, however other values
                                 ! are generated instead.
      print *, j
      end

Assigning the concatenated expression to a temporary character variable and using the temporary variable in the IF statement or function call will prevent the problem from occurring, as illustrated by the following sample programs:

In an IF Statement

      character r*2 /'AA'/

      character str*13             ! Declare a temporary variable and
      str = r(1:n+2)//'A'          ! assign the concatenated expression
                                   ! to it.

      n = 0

      if ('BBB' .gt. str) then     ! Use the temporary variable in
                                   ! the IF statement.

         write (*,*) 'YES'
      else
         write (*,*) 'NO'
      endif

      if ('BBB' .gt. r(1:2)//'A') then
         write (*,*) 'YES'
      else
         write (*,*) 'NO'
      endif

      end


In a Function

      character*4  a
      character*1  b
      character*12 str    ! Declare a temporary variable.

      a = 'cdef'
      b = 'd'
      i = len(a)
      print *, i
      str = 'ab'//a(1:i)  ! Assign the concatenated expression
                          ! to the temporary variable.

      j = index(str,b)    ! Use the temporary variable in the
                          ! index function.
      print *, j
      end


Additional reference words: 5.00
KBCategory: kbtool kbfixlist kbbuglist
KBSubcategory: FLIss
Solution Type : kbfix


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 11, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.