Skip to content

Commit cfd7d81

Browse files
committed
[flang] Fix bad dereference of NULLIFY pointer object
When we have a subprogram that has been determined to contain errors, we do not perform name resolution on its execution part. In this case, if the subprogram contains a NULLIFY statement, the parser::Name of a pointer object in a NULLIFY statement will not have had name resolution performed on it. Thus, its symbol will not have been set. Later, however, we do semantic checking on the NULLIFY statement. The code that did this assumed that the parser::Name of the pointer object was non-null. I fixed this by just removing the null pointer check for the "symbol" member of the "parser::Name" of the pointer object when doing semantic checking for NULLIFY statements. I also added a test that will make the compiler crash without this change. Differential Revision: https://reviews.llvm.org/D98184
1 parent 535a419 commit cfd7d81

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

flang/lib/Semantics/check-nullify.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ void NullifyChecker::Leave(const parser::NullifyStmt &nullifyStmt) {
2626
std::visit(
2727
common::visitors{
2828
[&](const parser::Name &name) {
29-
const Symbol &symbol{DEREF(name.symbol)};
30-
if (context_.HasError(&symbol)) {
29+
const Symbol *symbol{name.symbol};
30+
if (context_.HasError(symbol)) {
3131
// already reported an error
32-
} else if (!IsVariableName(symbol) && !IsProcName(symbol)) {
32+
} else if (!IsVariableName(*symbol) && !IsProcName(*symbol)) {
3333
messages.Say(name.source,
3434
"name in NULLIFY statement must be a variable or procedure pointer name"_err_en_US);
35-
} else if (!IsPointer(symbol)) { // C951
35+
} else if (!IsPointer(*symbol)) { // C951
3636
messages.Say(name.source,
3737
"name in NULLIFY statement must have the POINTER attribute"_err_en_US);
3838
} else if (pure) {
39-
CheckDefinabilityInPureScope(messages, symbol, scope, *pure);
39+
CheckDefinabilityInPureScope(messages, *symbol, scope, *pure);
4040
}
4141
},
4242
[&](const parser::StructureComponent &structureComponent) {

flang/test/Semantics/nullify02.f90

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,21 @@
2929
Nullify(maxvalue)
3030

3131
End Program
32+
33+
! Make sure that the compiler doesn't crash when NULLIFY is used in a context
34+
! that has reported errors
35+
module badNullify
36+
interface
37+
module function ptrFun()
38+
integer, pointer :: ptrFun
39+
end function
40+
end interface
41+
contains
42+
!ERROR: 'ptrfun' was not declared a separate module procedure
43+
module function ptrFun()
44+
integer, pointer :: ptrFun
45+
real :: realVar
46+
nullify(ptrFun)
47+
nullify(realVar)
48+
end function
49+
end module

0 commit comments

Comments
 (0)