Skip to content

Commit 82982d7

Browse files
authored
[flang][intrinsic] restrict kind of get_command(_argument) to >= 2 (#139291)
Previously the following program would have failed with a runtime assertion violation. This PR restricts the type information such that this assertion failure isn't reachable. The example below demonstrates the change. ```bash $ cat error.f90 integer (kind=1) :: i call get_command(length=i) print *, i end $ cat good.f90 integer (kind=2) :: i call get_command(length=i) print *, i end $ prior/flang error.f90 && ./a.out fatal Fortran runtime error(/home/akuhlenschmi/work/lorado/src/llvm-project/t.f90:2): Internal error: RUNTIME_CHECK(IsValidIntDescriptor(length)) failed at /home/akuhlenschmi/work/lorado/src/llvm-project/flang-rt/lib/runtime/command.cpp(154) Aborted (core dumped) $ prior/flang good.f90 && ./a.out 7 $ current/flang error.f90 && ./a.out error: Semantic errors in t.f90 ./t.f90:2:25: error: Actual argument for 'length=' has bad type or kind 'INTEGER(1)' call get_command(length=i) ^ $ current/flang good.f90 && ./a.out 7 ``` Also while making the change, I noticed that "get_command_argument" suffers from the same issue, so I made a similar change for it.
1 parent 12e6622 commit 82982d7

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

flang/lib/Evaluate/intrinsics.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,8 +1587,8 @@ static const IntrinsicInterface intrinsicSubroutine[]{
15871587
{"get_command",
15881588
{{"command", DefaultChar, Rank::scalar, Optionality::optional,
15891589
common::Intent::Out},
1590-
{"length", AnyInt, Rank::scalar, Optionality::optional,
1591-
common::Intent::Out},
1590+
{"length", TypePattern{IntType, KindCode::greaterOrEqualToKind, 2},
1591+
Rank::scalar, Optionality::optional, common::Intent::Out},
15921592
{"status", AnyInt, Rank::scalar, Optionality::optional,
15931593
common::Intent::Out},
15941594
{"errmsg", DefaultChar, Rank::scalar, Optionality::optional,
@@ -1598,8 +1598,8 @@ static const IntrinsicInterface intrinsicSubroutine[]{
15981598
{{"number", AnyInt, Rank::scalar},
15991599
{"value", DefaultChar, Rank::scalar, Optionality::optional,
16001600
common::Intent::Out},
1601-
{"length", AnyInt, Rank::scalar, Optionality::optional,
1602-
common::Intent::Out},
1601+
{"length", TypePattern{IntType, KindCode::greaterOrEqualToKind, 2},
1602+
Rank::scalar, Optionality::optional, common::Intent::Out},
16031603
{"status", AnyInt, Rank::scalar, Optionality::optional,
16041604
common::Intent::Out},
16051605
{"errmsg", DefaultChar, Rank::scalar, Optionality::optional,

flang/test/Semantics/command.f90

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
! RUN: %python %S/test_errors.py %s %flang_fc1
2+
program command
3+
implicit none
4+
Integer(1) :: i1
5+
Integer(2) :: i2
6+
Integer(4) :: i4
7+
Integer(8) :: i8
8+
Integer(16) :: i16
9+
Integer :: a
10+
!ERROR: Actual argument for 'length=' has bad type or kind 'INTEGER(1)'
11+
call get_command(length=i1)
12+
!OK:
13+
call get_command(length=i2)
14+
!OK:
15+
call get_command(length=i4)
16+
!OK:
17+
call get_command(length=i8)
18+
!OK:
19+
call get_command(length=i16)
20+
!ERROR: Actual argument for 'length=' has bad type or kind 'INTEGER(1)'
21+
call get_command_argument(number=a,length=i1)
22+
!OK:
23+
call get_command_argument(number=a,length=i2)
24+
!OK:
25+
call get_command_argument(number=a,length=i4)
26+
!OK:
27+
call get_command_argument(number=a,length=i8)
28+
!OK:
29+
call get_command_argument(number=a,length=i16)
30+
end program

0 commit comments

Comments
 (0)