OSDN Git Service

PR fortran/33079
authorfxcoudert <fxcoudert@138bc75d-0d04-0410-961f-82ee72b054a4>
Fri, 17 Aug 2007 13:09:23 +0000 (13:09 +0000)
committerfxcoudert <fxcoudert@138bc75d-0d04-0410-961f-82ee72b054a4>
Fri, 17 Aug 2007 13:09:23 +0000 (13:09 +0000)
* intrinsics/string_intrinsics.c (string_trim, string_minmax): Fix
the zero-length result case.

* gfortran.dg/zero_length_2.f90: New test.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@127584 138bc75d-0d04-0410-961f-82ee72b054a4

gcc/testsuite/ChangeLog
gcc/testsuite/gfortran.dg/zero_length_2.f90 [new file with mode: 0644]
libgfortran/ChangeLog
libgfortran/intrinsics/string_intrinsics.c

index a320c2e..7ababfa 100644 (file)
@@ -1,3 +1,8 @@
+2007-08-17  Francois-Xavier Coudert  <fxcoudert@gcc.gnu.org>
+
+       PR fortran/33079
+       * gfortran.dg/zero_length_2.f90: New test.
+
 2007-08-17  Tobias Burnus  <burnus@net-b.de>
 
        * gfortran.dg/kind_tests_2.f03: Add cleanup-modules.
diff --git a/gcc/testsuite/gfortran.dg/zero_length_2.f90 b/gcc/testsuite/gfortran.dg/zero_length_2.f90
new file mode 100644 (file)
index 0000000..31b99f5
--- /dev/null
@@ -0,0 +1,16 @@
+! { dg-do run }
+  character(len=1) :: s
+  character(len=0) :: s0 ! { dg-warning "CHARACTER variable has zero length" }
+  s = " "
+  s0 = ""
+  call bar ("")
+  call bar (s)
+  call bar (s0)
+  call bar (trim(s))
+  call bar (min(s0,s0))
+contains
+  subroutine bar (s)
+    character(len=*), optional :: s
+    if (.not. present (S)) call abort
+  end subroutine bar
+end
index 2305b3e..de3d574 100644 (file)
@@ -1,3 +1,9 @@
+2007-08-17  Francois-Xavier Coudert  <fxcoudert@gcc.gnu.org>
+
+       PR fortran/33079
+       * intrinsics/string_intrinsics.c (string_trim, string_minmax): Fix
+       the zero-length result case.
+
 2007-08-15  Francois-Xavier Coudert  <fxcoudert@gcc.gnu.org>
 
        PR fortran/33077
index be02811..c663daa 100644 (file)
@@ -77,6 +77,11 @@ export_proto(string_trim);
 extern void string_minmax (GFC_INTEGER_4 *, void **, int, int, ...);
 export_proto(string_minmax);
 
+
+/* Use for functions which can return a zero-length string.  */
+static char zero_length_string = '\0';
+
+
 /* Strings of unequal length are extended with pad characters.  */
 
 int
@@ -167,16 +172,16 @@ string_trim (GFC_INTEGER_4 * len, void ** dest, GFC_INTEGER_4 slen,
     }
   *len = i + 1;
 
-  if (*len > 0)
+  if (*len == 0)
+    *dest = &zero_length_string;
+  else
     {
       /* Allocate space for result string.  */
       *dest = internal_malloc_size (*len);
 
-      /* copy string if necessary.  */
+      /* Copy string if necessary.  */
       memmove (*dest, src, *len);
     }
-  else
-    *dest = NULL;
 }
 
 
@@ -403,14 +408,13 @@ string_minmax (GFC_INTEGER_4 *rlen, void **dest, int op, int nargs, ...)
     }
   va_end (ap);
 
-  if (*rlen > 0)
+  if (*rlen == 0)
+    *dest = &zero_length_string;
+  else
     {
       char * tmp = internal_malloc_size (*rlen);
       memcpy (tmp, res, reslen);
       memset (&tmp[reslen], ' ', *rlen - reslen);
       *dest = tmp;
     }
-  else
-    *dest = NULL;
 }
-