[PATCH] D98184: [flang] Fix bad dereference of NULLIFY pointer object

Pete Steinfeld via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 8 08:23:12 PST 2021


PeteSteinfeld created this revision.
PeteSteinfeld added reviewers: klausler, tskeith.
PeteSteinfeld requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D98184

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


Index: flang/test/Semantics/nullify02.f90
===================================================================
--- flang/test/Semantics/nullify02.f90
+++ 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
Index: flang/lib/Semantics/check-nullify.cpp
===================================================================
--- flang/lib/Semantics/check-nullify.cpp
+++ flang/lib/Semantics/check-nullify.cpp
@@ -26,17 +26,17 @@
     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) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D98184.329026.patch
Type: text/x-patch
Size: 2115 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210308/98a7d706/attachment.bin>


More information about the llvm-commits mailing list