Skip to content

Commit 9405a81

Browse files
authored
[flang] Fix crash exposed by fuzzing (#122187)
An integer overflowed in an erroneous test. Fix, and improve the error messages a bit. Fixes #121979.
1 parent 6f55c80 commit 9405a81

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

flang/lib/Evaluate/intrinsics.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2094,7 +2094,7 @@ std::optional<SpecificCall> IntrinsicInterface::Match(
20942094
const ActualArgument *arrayArg{nullptr};
20952095
const char *arrayArgName{nullptr};
20962096
const ActualArgument *knownArg{nullptr};
2097-
std::optional<int> shapeArgSize;
2097+
std::optional<std::int64_t> shapeArgSize;
20982098
int elementalRank{0};
20992099
for (std::size_t j{0}; j < dummies; ++j) {
21002100
const IntrinsicDummyArgument &d{dummy[std::min(j, dummyArgPatterns - 1)]};
@@ -2133,14 +2133,20 @@ std::optional<SpecificCall> IntrinsicInterface::Match(
21332133
if (auto shape{GetShape(context, *arg)}) {
21342134
if (auto constShape{AsConstantShape(context, *shape)}) {
21352135
shapeArgSize = constShape->At(ConstantSubscripts{1}).ToInt64();
2136-
CHECK(*shapeArgSize >= 0);
2137-
argOk = true;
2136+
CHECK(shapeArgSize.value() >= 0);
2137+
argOk = *shapeArgSize <= common::maxRank;
21382138
}
21392139
}
21402140
}
21412141
if (!argOk) {
2142-
messages.Say(arg->sourceLocation(),
2143-
"'shape=' argument must be a vector of known size"_err_en_US);
2142+
if (shapeArgSize.value_or(0) > common::maxRank) {
2143+
messages.Say(arg->sourceLocation(),
2144+
"'shape=' argument must be a vector of at most %d elements (has %jd)"_err_en_US,
2145+
common::maxRank, std::intmax_t{*shapeArgSize});
2146+
} else {
2147+
messages.Say(arg->sourceLocation(),
2148+
"'shape=' argument must be a vector of known size"_err_en_US);
2149+
}
21442150
return std::nullopt;
21452151
}
21462152
break;

flang/test/Semantics/reshape.f90

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,9 @@ program reshaper
5353
!ERROR: 'shape=' argument has too many elements
5454
integer :: array23(I64_MAX, I64_MAX) = RESHAPE([1, 2, 3], huge_shape)
5555

56-
!ERROR: Size of 'shape=' argument must not be greater than 15
5756
CALL ext_sub(RESHAPE([(n, n=1,20)], &
57+
!ERROR: 'shape=' argument must be a vector of at most 15 elements (has 16)
5858
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]))
59-
!ERROR: Reference to the procedure 'ext_sub' has an implicit interface that is distinct from another reference: incompatible dummy argument #1: incompatible dummy data object shapes
6059
!ERROR: 'shape=' argument must not have a negative extent
6160
CALL ext_sub(RESHAPE([(n, n=1,20)], [1, -5, 3]))
6261
!ERROR: 'order=' argument has unacceptable rank 2

0 commit comments

Comments
 (0)