Is this using character string pointers safe?

When implementing the service line function, I came across several expressions of the character pointer, which, I think, may be unsafe. I googled, searched on SO, read my Fortran 95 language guide (Gehrke 1996), as well as various excerpts from Google books. However, I could not find sources discussing this particular use.

Both ifort and gfortran compile the following program without warning:

PROGRAM test_pointer
  IMPLICIT NONE
  CHARACTER(LEN=100), TARGET :: string = "A string variable"
  CHARACTER(LEN=0), TARGET :: empty = ""
  CHARACTER(LEN=:), POINTER :: ptr

  ptr => NULL()
  IF(ptr == "") PRINT *, 'Nullified pointer is equal to ""'

  ptr => string(-2:-3)
  IF(ptr == "") PRINT *, 'ptr equals "", but the (empty) sub string was out of bounds.'

  ptr => empty(1:0)
  IF(ptr == "") PRINT *, 'ptr equals "", it was not possible to specify subarray within bonds'
END PROGRAM

Program Output:

Nullified pointer is equal to ""
ptr equals "", but the (empty) sub string was out of bounds.
ptr equals "", it was not possible to specify subarray within bonds

-, , - , . - , ? ? ?

edit: F , . .

+4
1

segfault? ( C undefined). , , . , ! , !

(sunf90):

 ******  FORTRAN RUN-TIME SYSTEM  ******
 Attempting to use an unassociated POINTER 'PTR'
 Location:  line 8 column 6 of 'charptr.f90'
Aborted

(ifort):

forrtl: severe (408): fort: (7): Attempt to use pointer PTR when it is not associated with a target

Image              PC                Routine            Line        Source             
a.out              0000000000402EB8  Unknown               Unknown  Unknown
a.out              0000000000402DE6  Unknown               Unknown  Unknown
libc.so.6          00007FA0AE123A15  Unknown               Unknown  Unknown
a.out              0000000000402CD9  Unknown               Unknown  Unknown

, 0, , - .

, Fortran (F2008: 6.4.1.3) :

, 1, 2,..., n , point, 0.


, .

+5

Source: https://habr.com/ru/post/1614151/


All Articles