diff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc
index 1e0d698a9497b6aec54d24279ba0c3423ee45e78..2181990aa04ac09403ba6b5a11dac74d9c6a3419 100644
--- a/gcc/fortran/trans-array.cc
+++ b/gcc/fortran/trans-array.cc
@@ -11531,6 +11531,9 @@ gfc_trans_deferred_array (gfc_symbol * sym, gfc_wrapped_block * block)
   if (sym->ts.type == BT_CHARACTER
       && !INTEGER_CST_P (sym->ts.u.cl->backend_decl))
     {
+      if (sym->ts.deferred && !sym->ts.u.cl->length)
+	gfc_add_modify (&init, sym->ts.u.cl->backend_decl,
+			build_zero_cst (TREE_TYPE (sym->ts.u.cl->backend_decl)));
       gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
       gfc_trans_vla_type_sizes (sym, &init);
 
diff --git a/gcc/testsuite/gfortran.dg/allocatable_length.f90 b/gcc/testsuite/gfortran.dg/allocatable_length.f90
new file mode 100644
index 0000000000000000000000000000000000000000..e8b638fac880f6167fe254770e608b78334ccce3
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/allocatable_length.f90
@@ -0,0 +1,9 @@
+! { dg-do compile }
+! { dg-options "-Werror -Wall" }
+module foo
+   contains
+      subroutine bar 
+         character(len=:), allocatable :: s(:)
+         call bah(s)
+      end subroutine bar
+end module foo