This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug libfortran/33079] Optional empty strings do not appear to be 'PRESENT'
- From: "fxcoudert at gcc dot gnu dot org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 15 Aug 2007 14:05:32 -0000
- Subject: [Bug libfortran/33079] Optional empty strings do not appear to be 'PRESENT'
- References: <bug-33079-10601@http.gcc.gnu.org/bugzilla/>
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
------- Comment #1 from fxcoudert at gcc dot gnu dot org 2007-08-15 14:05 -------
The problem is shared by the TRIM and MIN/MAX. It is demonstrated by:
character(len=1) :: s
character(len=0) :: s0
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
The problem is that a zero-length character string isn't NULL, it's "" (ie a
pointer to a '\0'). The following patch fixes it:
Index: intrinsics/string_intrinsics.c
===================================================================
--- intrinsics/string_intrinsics.c (revision 127490)
+++ intrinsics/string_intrinsics.c (working copy)
@@ -167,16 +167,21 @@ string_trim (GFC_INTEGER_4 * len, void *
}
*len = i + 1;
- if (*len > 0)
+ if (*len == 0)
+ {
+ /* A zero-length Fortran string is "". */
+ char * tmp = internal_malloc_size (1);
+ tmp[0] = '\0';
+ *dest = tmp;
+ }
+ 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,18 @@ string_minmax (GFC_INTEGER_4 *rlen, void
}
va_end (ap);
- if (*rlen > 0)
+ if (*rlen == 0)
+ {
+ /* A zero-length Fortran string is "". */
+ char * tmp = internal_malloc_size (1);
+ tmp[0] = '\0';
+ *dest = tmp;
+ }
+ else
{
char * tmp = internal_malloc_size (*rlen);
memcpy (tmp, res, reslen);
memset (&tmp[reslen], ' ', *rlen - reslen);
*dest = tmp;
}
- else
- *dest = NULL;
}
-
--
fxcoudert at gcc dot gnu dot org changed:
What |Removed |Added
----------------------------------------------------------------------------
AssignedTo|unassigned at gcc dot gnu |fxcoudert at gcc dot gnu dot
|dot org |org
Status|NEW |ASSIGNED
Keywords| |patch
Last reconfirmed|2007-08-15 13:05:53 |2007-08-15 14:05:29
date| |
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33079