This is an archive of the discontinued LLVM Phabricator instance.

[flang] Add runtime API to catch unit number out of range
ClosedPublic

Authored by jeanPerier on Apr 5 2022, 2:07 PM.

Details

Summary

Unit numbers must fit on a default integer. It is however possible that
the user provides the unit number in UNIT with a wider integer type.
In such case, lowering was previously silently narrowing
the value and passing the result to the BeginXXX runtime entry points.
Cases where the conversion caused overflow were not reported/caught.
Most existing compilers catch these errors and raise an IO error.
Add a CheckUnitNumberInRange runtime API to do the same in f18.

This runtime API has its own error management interface (i.e., does not
use GetIoMsg, EndIo, and EnableHandlers) because the usual error
management requires BeginXXX to be called to set up the error
management. But in this case, the BeginXXX cannot be called since
the bad unit number that would be provided to it overflew (and in the worst
case scenario, the narrowed value could point to a different valid unit
already in use). Hence I decided to make an API that must be called
before the BeginXXX and should trigger the whole BeginXXX/.../EndIoStatement
to be skipped in case the unit number is too big and the user enabled
error recovery.

Note that CheckUnitNumberInRange accepts negative numbers (as long as
they can fit on a default integer), because unit numbers may be negative
if they were created by NEWUNIT.

Diff Detail

Event Timeline

jeanPerier created this revision.Apr 5 2022, 2:07 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 5 2022, 2:07 PM
Herald added a subscriber: jdoerfert. · View Herald Transcript
jeanPerier requested review of this revision.Apr 5 2022, 2:07 PM
klausler accepted this revision.Apr 5 2022, 2:14 PM
klausler added inline comments.
flang/include/flang/Runtime/iostat.h
70

IostatErrorString() in iostat.cpp will need a case for this new enumeration (for completeness).

flang/runtime/io-api.cpp
1299

This string argument can be omitted once you add it to IostatErrorString in iostat.cpp.

This revision is now accepted and ready to land.Apr 5 2022, 2:14 PM
jeanPerier updated this revision to Diff 420740.Apr 6 2022, 1:46 AM

Add case for IostatUnitOverflow in IostatErrorString().

jeanPerier marked 2 inline comments as done.Apr 6 2022, 1:46 AM