[flang] Fix bad dereference of NULLIFY pointer object
authorPeter Steinfeld <psteinfeld@nvidia.com>
Mon, 8 Mar 2021 16:21:30 +0000 (08:21 -0800)
committerPeter Steinfeld <psteinfeld@nvidia.com>
Mon, 8 Mar 2021 23:01:36 +0000 (15:01 -0800)
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

flang/lib/Semantics/check-nullify.cpp
flang/test/Semantics/nullify02.f90

index 5c3396e..ff49a66 100644 (file)
@@ -26,17 +26,17 @@ void NullifyChecker::Leave(const parser::NullifyStmt &nullifyStmt) {
     std::visit(
         common::visitors{
             [&](const parser::Name &name) {
-              const Symbol &symbol{DEREF(name.symbol)};
-              if (context_.HasError(&symbol)) {
+              const Symbol *symbol{name.symbol};
+              if (context_.HasError(symbol)) {
                 // already reported an error
-              } else if (!IsVariableName(symbol) && !IsProcName(symbol)) {
+              } else if (!IsVariableName(*symbol) && !IsProcName(*symbol)) {
                 messages.Say(name.source,
                     "name in NULLIFY statement must be a variable or procedure pointer name"_err_en_US);
-              } else if (!IsPointer(symbol)) { // C951
+              } else if (!IsPointer(*symbol)) { // C951
                 messages.Say(name.source,
                     "name in NULLIFY statement must have the POINTER attribute"_err_en_US);
               } else if (pure) {
-                CheckDefinabilityInPureScope(messages, symbol, scope, *pure);
+                CheckDefinabilityInPureScope(messages, *symbol, scope, *pure);
               }
             },
             [&](const parser::StructureComponent &structureComponent) {
index fccb618..b889c9b 100644 (file)
@@ -29,3 +29,21 @@ Nullify(prp)
 Nullify(maxvalue)
 
 End Program
+
+! Make sure that the compiler doesn't crash when NULLIFY is used in a context
+! that has reported errors
+module badNullify
+  interface
+    module function ptrFun()
+      integer, pointer :: ptrFun
+    end function
+  end interface
+contains
+  !ERROR: 'ptrfun' was not declared a separate module procedure
+  module function ptrFun()
+    integer, pointer :: ptrFun
+    real :: realVar
+    nullify(ptrFun)
+    nullify(realVar)
+  end function
+end module