Skip to content

[Flang] reads d* prefixed strings as floating-point values #151465

@marikurz-amd

Description

@marikurz-amd

Trying to read a (non-real) string into a real variable unexpectedly succeeds instead of throwing an error if both

  • the string starts with d or e,
  • and a floating-point format identifier without explicit width is used.

This behavior can be reproduced with

Flang Version: 21.0.0git commit 532facc78e

and the following snippet:

program reproducer
implicit none
character(len=4)  :: str = "dstr"
real              :: test
integer           :: istat
read(str,"(D)", iostat=istat) test
WRITE(*,*) "Is the string real? ", (istat .EQ. 0)
end program reproducer

which yields

Is the string real? T

Thus, flang compiles and succeeds in reading the string "dstr" into a real variable, which should not be allowed. If an explicit width is used, such as "(D4.0)", Flang throws istat != 0 as expected.

In my opinion, the expected behavior would be one of the following:

  1. Flang should throw a compile-time error as no standard-conform, explicit field width in the format "(Dw.d)" is provided. This is how gfortran behaves.
  2. Flang should correctly detect during runtime that the string cannot be transformed into a real in a meaningful way and provide iostat != 0. This is the behavior of Classic-Flang and the Cray compiler.

I would speculate this behavior is somehow caused by d and e are valid components of a floating-point number in exponential format.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions