[flang-commits] [flang] cfd7d81 - [flang] Fix bad dereference of NULLIFY pointer object

Peter Steinfeld via flang-commits flang-commits at lists.llvm.org
Mon Mar 8 15:01:55 PST 2021


Author: Peter Steinfeld
Date: 2021-03-08T15:01:36-08:00
New Revision: cfd7d8123a3b1e77343763ac2b62124bfcffe6d6

URL: https://github.com/llvm/llvm-project/commit/cfd7d8123a3b1e77343763ac2b62124bfcffe6d6
DIFF: https://github.com/llvm/llvm-project/commit/cfd7d8123a3b1e77343763ac2b62124bfcffe6d6.diff

LOG: [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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/flang/lib/Semantics/check-nullify.cpp b/flang/lib/Semantics/check-nullify.cpp
index 5c3396e8128d..ff49a661e206 100644
--- a/flang/lib/Semantics/check-nullify.cpp
+++ b/flang/lib/Semantics/check-nullify.cpp
@@ -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) {

diff  --git a/flang/test/Semantics/nullify02.f90 b/flang/test/Semantics/nullify02.f90
index fccb618e376f..b889c9b20c6c 100644
--- a/flang/test/Semantics/nullify02.f90
+++ b/flang/test/Semantics/nullify02.f90
@@ -29,3 +29,21 @@
 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


        


More information about the flang-commits mailing list