[flang] Fix bad deference in ProgramTree
authorTim Keith <tkeith@nvidia.com>
Fri, 15 Nov 2019 03:22:24 +0000 (19:22 -0800)
committerTim Keith <tkeith@nvidia.com>
Fri, 15 Nov 2019 03:22:24 +0000 (19:22 -0800)
We weren't handling MainProgram with no ProgramStmt correctly in
ProgramTree. When building it we were dereferencing an empty optional.
And in ResolveSpecificationParts we were dereferencing a null pointer.

Original-commit: flang-compiler/f18@1bda90d0051d7b67bf76f9aaa1f2963976a15d4a
Reviewed-on: https://github.com/flang-compiler/f18/pull/827

flang/lib/semantics/program-tree.cc
flang/lib/semantics/resolve-names.cc

index 9a55f5f..e31f282 100644 (file)
@@ -62,8 +62,9 @@ ProgramTree ProgramTree::Build(const parser::MainProgram &x) {
       std::get<std::optional<parser::Statement<parser::ProgramStmt>>>(x.t)};
   const auto &end{std::get<parser::Statement<parser::EndProgramStmt>>(x.t)};
   static parser::Name emptyName;
-  const auto &name{stmt ? stmt->statement.v : emptyName};
-  return BuildSubprogramTree(name, x).set_stmt(*stmt).set_endStmt(end);
+  auto result{stmt ? BuildSubprogramTree(stmt->statement.v, x).set_stmt(*stmt)
+                   : BuildSubprogramTree(emptyName, x)};
+  return result.set_endStmt(end);
 }
 
 ProgramTree ProgramTree::Build(const parser::FunctionSubprogram &x) {
index 8d3e9c9..6685ff0 100644 (file)
@@ -5921,7 +5921,13 @@ void ResolveNamesVisitor::ResolveSpecificationParts(ProgramTree &node) {
   Scope &scope{currScope()};
   node.set_scope(scope);
   AddSubpNames(node);
-  std::visit([&](const auto *x) { Walk(*x); }, node.stmt());
+  std::visit(
+      [&](const auto *x) {
+        if (x) {
+          Walk(*x);
+        }
+      },
+      node.stmt());
   Walk(node.spec());
   // If this is a function, convert result to an object. This is to prevent the
   // result to be converted later to a function symbol if it is called inside