From 100e0cc90d2bd6a3bb4b176d62751ae7ce15f1c7 Mon Sep 17 00:00:00 2001
From: Philippe WAUTELET <philippe.wautelet@aero.obs-mip.fr>
Date: Wed, 23 May 2018 16:19:35 +0200
Subject: [PATCH] Philippe 23/05/2018: IO: bug correction for allocation of a
 character string

---
 src/LIB/SURCOUCHE/src/fmwrit_ll.f90 | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)

diff --git a/src/LIB/SURCOUCHE/src/fmwrit_ll.f90 b/src/LIB/SURCOUCHE/src/fmwrit_ll.f90
index 113318835..56299ed08 100644
--- a/src/LIB/SURCOUCHE/src/fmwrit_ll.f90
+++ b/src/LIB/SURCOUCHE/src/fmwrit_ll.f90
@@ -310,7 +310,7 @@ CONTAINS
     CHARACTER(LEN=*),OPTIONAL,INTENT(IN)  :: HDAD_NAME
     !
     CHARACTER(LEN=:),ALLOCATABLE :: YDAD_NAME
-    INTEGER                      :: ILEN
+    INTEGER                      :: ILEN,ILEN2
     !
     CALL PRINT_MSG(NVERB_DEBUG,'IO','IO_WRITE_HEADER','called for file '//TRIM(TPFILE%CNAME))
     !
@@ -331,12 +331,23 @@ CONTAINS
     CALL IO_WRITE_FIELD(TPFILE,'MY_NAME',     TPFILE%CNAME)
     !
     IF ( ASSOCIATED(TPFILE%TDADFILE) ) THEN
-      ALLOCATE(CHARACTER(LEN=MAX(NFILENAMELGTMAXLFI,LEN_TRIM(TPFILE%TDADFILE%CNAME))) :: YDAD_NAME)
-      YDAD_NAME(:) = TPFILE%TDADFILE%CNAME
+      ILEN  = LEN_TRIM(TPFILE%TDADFILE%CNAME)
+      ILEN2 = MAX(NFILENAMELGTMAXLFI,ILEN)
+      ALLOCATE(CHARACTER(LEN=ILEN2) :: YDAD_NAME)
+      IF(ILEN>0) THEN
+        YDAD_NAME(1:ILEN) = TPFILE%TDADFILE%CNAME(1:ILEN)
+      ELSE
+        YDAD_NAME(:) = ' '
+      END IF
     ELSE IF (PRESENT(HDAD_NAME)) THEN
-      ILEN = LEN_TRIM(HDAD_NAME)
-      ALLOCATE(CHARACTER(LEN=MAX(NFILENAMELGTMAXLFI,ILEN)) :: YDAD_NAME)
-      YDAD_NAME(:) = HDAD_NAME
+      ILEN  = LEN_TRIM(HDAD_NAME)
+      ILEN2 = MAX(NFILENAMELGTMAXLFI,ILEN)
+      ALLOCATE(CHARACTER(LEN=ILEN2) :: YDAD_NAME)
+      IF(ILEN>0) THEN
+        YDAD_NAME(1:ILEN) = HDAD_NAME(1:ILEN)
+      ELSE
+        YDAD_NAME(:) = ' '
+      END IF
     ELSE
       CALL PRINT_MSG(NVERB_DEBUG,'IO','IO_WRITE_HEADER',TRIM(TPFILE%CNAME)// &
                      ': TPFILE%TDADFILE not associated and HDAD_NAME not provided')
-- 
GitLab