diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 3a352f8bbd31cbe2faeb242d50967e7904dc7a5c..4b4eeefb675c3ddd4f3f0d517a35bd1ed8fb8e41 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,10 @@
+2020-04-29  Richard Biener  <rguenther@suse.de>
+	    Li Zekun  <lizekun1@huawei.com>
+
+	PR lto/94822
+	* tree.c (component_ref_size): Guard against error_mark_node
+	DECL_INITIAL as it happens with LTO.
+
 2020-04-29  Richard Sandiford  <richard.sandiford@arm.com>
 
 	* config/aarch64/aarch64.c (aarch64_function_arg_alignment): Add a
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 701af0b1e72e734ab3fcebf09070c77ee2c1280a..3810ea3375e2089c89f8722fd75bdccc9794881a 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,11 @@
+2020-04-29  Richard Biener  <rguenther@suse.de>
+	    Li Zekun  <lizekun1@huawei.com>
+
+	PR lto/94822
+	* gcc.dg/lto/pr94822_0.c: New testcase.
+	* gcc.dg/lto/pr94822_1.c: Alternate file.
+	* gcc.dg/lto/pr94822.h: Likewise.
+
 2020-04-29  Richard Sandiford  <richard.sandiford@arm.com>
 
 	* g++.target/aarch64/no_unique_address_1.C: New test.
diff --git a/gcc/testsuite/gcc.dg/lto/pr94822.h b/gcc/testsuite/gcc.dg/lto/pr94822.h
new file mode 100644
index 0000000000000000000000000000000000000000..d9e6c3da6459b9b3d971200e857ce90193696421
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/lto/pr94822.h
@@ -0,0 +1,4 @@
+typedef struct {
+  int i;
+  int ints[];
+} struct_t;
diff --git a/gcc/testsuite/gcc.dg/lto/pr94822_0.c b/gcc/testsuite/gcc.dg/lto/pr94822_0.c
new file mode 100644
index 0000000000000000000000000000000000000000..698c0928a81c3195aa7f2e71189d0ad79d1867b4
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/lto/pr94822_0.c
@@ -0,0 +1,10 @@
+/* { dg-lto-do link } */
+
+#include "pr94822.h"
+
+extern struct_t my_struct;
+
+int main() {
+ return my_struct.ints[1];
+}
+
diff --git a/gcc/testsuite/gcc.dg/lto/pr94822_1.c b/gcc/testsuite/gcc.dg/lto/pr94822_1.c
new file mode 100644
index 0000000000000000000000000000000000000000..a7ace71680f274fc073a2a3ab5055566a42227fa
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/lto/pr94822_1.c
@@ -0,0 +1,6 @@
+#include "pr94822.h"
+
+struct_t my_struct = {
+ 20,
+ { 1, 2 }
+};
diff --git a/gcc/tree.c b/gcc/tree.c
index e28b29580caf94a81d9580c4928dc990ba7ea646..e451401822c8eed91976b62a47386459534e4eae 100644
--- a/gcc/tree.c
+++ b/gcc/tree.c
@@ -13723,24 +13723,25 @@ component_ref_size (tree ref, bool *interior_zero_length /* = NULL */)
     /* MEMBER is a true flexible array member.  Compute its size from
        the initializer of the BASE object if it has one.  */
     if (tree init = DECL_P (base) ? DECL_INITIAL (base) : NULL_TREE)
-      {
-	init = get_initializer_for (init, member);
-	if (init)
-	  {
-	    memsize = TYPE_SIZE_UNIT (TREE_TYPE (init));
-	    if (tree refsize = TYPE_SIZE_UNIT (reftype))
-	      {
-		/* Use the larger of the initializer size and the tail
-		   padding in the enclosing struct.  */
-		poly_int64 rsz = tree_to_poly_int64 (refsize);
-		rsz -= baseoff;
-		if (known_lt (tree_to_poly_int64 (memsize), rsz))
-		  memsize = wide_int_to_tree (TREE_TYPE (memsize), rsz);
-	      }
+      if (init != error_mark_node)
+	{
+	  init = get_initializer_for (init, member);
+	  if (init)
+	    {
+	      memsize = TYPE_SIZE_UNIT (TREE_TYPE (init));
+	      if (tree refsize = TYPE_SIZE_UNIT (reftype))
+		{
+		  /* Use the larger of the initializer size and the tail
+		     padding in the enclosing struct.  */
+		  poly_int64 rsz = tree_to_poly_int64 (refsize);
+		  rsz -= baseoff;
+		  if (known_lt (tree_to_poly_int64 (memsize), rsz))
+		    memsize = wide_int_to_tree (TREE_TYPE (memsize), rsz);
+		}
 
-	    baseoff = 0;
-	  }
-      }
+	      baseoff = 0;
+	    }
+	}
 
   if (!memsize)
     {