[flang-commits] [flang] [flang] Fix crash in error recovery (bad binding) (PR #92800)
Peter Klausler via flang-commits
flang-commits at lists.llvm.org
Mon May 20 11:20:02 PDT 2024
https://github.com/klausler created https://github.com/llvm/llvm-project/pull/92800
A type-bound procedure that's bound to a name that isn't a procedure is caught as an error, but can also lead to a crash in compatibility checking later. Make that code more robust to failure.
Fixes https://github.com/llvm/llvm-project/issues/92678.
>From 24c3a32ca0f23e4e0c84a173e064679ce3ba095f Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Mon, 20 May 2024 11:16:45 -0700
Subject: [PATCH] [flang] Fix crash in error recovery (bad binding)
A type-bound procedure that's bound to a name that isn't a
procedure is caught as an error, but can also lead to a crash
in compatibility checking later. Make that code more robust
to failure.
Fixes https://github.com/llvm/llvm-project/issues/92678.
---
.../include/flang/Evaluate/characteristics.h | 2 +-
flang/lib/Evaluate/characteristics.cpp | 17 ++++++++++------
flang/lib/Semantics/check-declarations.cpp | 20 ++++++++++---------
3 files changed, 23 insertions(+), 16 deletions(-)
diff --git a/flang/include/flang/Evaluate/characteristics.h b/flang/include/flang/Evaluate/characteristics.h
index 8aa065b025a4f..9695c665d0cb5 100644
--- a/flang/include/flang/Evaluate/characteristics.h
+++ b/flang/include/flang/Evaluate/characteristics.h
@@ -386,7 +386,7 @@ struct Procedure {
bool HasExplicitInterface() const {
return !attrs.test(Attr::ImplicitInterface);
}
- int FindPassIndex(std::optional<parser::CharBlock>) const;
+ std::optional<int> FindPassIndex(std::optional<parser::CharBlock>) const;
bool CanBeCalledViaImplicitInterface(std::string *whyNot = nullptr) const;
bool CanOverride(const Procedure &, std::optional<int> passIndex) const;
bool IsCompatibleWith(const Procedure &, bool ignoreImplicitVsExplicit,
diff --git a/flang/lib/Evaluate/characteristics.cpp b/flang/lib/Evaluate/characteristics.cpp
index ab03ca5ed2d5a..a0ce190b90e92 100644
--- a/flang/lib/Evaluate/characteristics.cpp
+++ b/flang/lib/Evaluate/characteristics.cpp
@@ -1333,16 +1333,21 @@ bool Procedure::IsCompatibleWith(const Procedure &actual,
return false;
}
-int Procedure::FindPassIndex(std::optional<parser::CharBlock> name) const {
+std::optional<int> Procedure::FindPassIndex(
+ std::optional<parser::CharBlock> name) const {
int argCount{static_cast<int>(dummyArguments.size())};
- int index{0};
if (name) {
- while (index < argCount && *name != dummyArguments[index].name.c_str()) {
- ++index;
+ for (int index{0}; index < argCount; ++index) {
+ if (*name == dummyArguments[index].name.c_str()) {
+ return index;
+ }
}
+ return std::nullopt;
+ } else if (argCount > 0) {
+ return 0;
+ } else {
+ return std::nullopt;
}
- CHECK(index < argCount);
- return index;
}
bool Procedure::CanOverride(
diff --git a/flang/lib/Semantics/check-declarations.cpp b/flang/lib/Semantics/check-declarations.cpp
index f564a0b69671c..9f9b01630f5ae 100644
--- a/flang/lib/Semantics/check-declarations.cpp
+++ b/flang/lib/Semantics/check-declarations.cpp
@@ -2430,16 +2430,18 @@ void CheckHelper::CheckProcBinding(
"A NOPASS type-bound procedure and its override must have identical interfaces"_err_en_US);
}
} else if (!context_.HasError(binding.symbol())) {
- int passIndex{bindingChars->FindPassIndex(binding.passName())};
- int overriddenPassIndex{
+ auto passIndex{bindingChars->FindPassIndex(binding.passName())};
+ auto overriddenPassIndex{
overriddenChars->FindPassIndex(overriddenBinding->passName())};
- if (passIndex != overriddenPassIndex) {
- SayWithDeclaration(*overridden,
- "A type-bound procedure and its override must use the same PASS argument"_err_en_US);
- } else if (!bindingChars->CanOverride(
- *overriddenChars, passIndex)) {
- SayWithDeclaration(*overridden,
- "A type-bound procedure and its override must have compatible interfaces"_err_en_US);
+ if (passIndex && overriddenPassIndex) {
+ if (*passIndex != *overriddenPassIndex) {
+ SayWithDeclaration(*overridden,
+ "A type-bound procedure and its override must use the same PASS argument"_err_en_US);
+ } else if (!bindingChars->CanOverride(
+ *overriddenChars, passIndex)) {
+ SayWithDeclaration(*overridden,
+ "A type-bound procedure and its override must have compatible interfaces"_err_en_US);
+ }
}
}
}
More information about the flang-commits
mailing list