[Bug libfortran/33079] Optional empty strings do not appear to be 'PRESENT'
-- fxcoudert at gcc dot gnu dot org changed: What|Removed |Added Status|UNCONFIRMED |NEW Ever Confirmed|0 |1 Keywords||wrong-code Known to fail||4.3.0 Last reconfirmed|-00-00 00:00:00 |2007-08-15 13:05:53 date|| http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33079
[Bug libfortran/33079] Optional empty strings do not appear to be 'PRESENT'
--- 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
[Bug libfortran/33079] Optional empty strings do not appear to be 'PRESENT'
--- Comment #2 from schwab at suse dot de 2007-08-15 14:18 --- Why do you need to special case len == 0? The other strings aren't NUL terminated either. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33079
[Bug libfortran/33079] Optional empty strings do not appear to be 'PRESENT'
--- Comment #3 from fxcoudert at gcc dot gnu dot org 2007-08-15 14:22 --- (In reply to comment #2) > Why do you need to special case len == 0? The other strings aren't NUL > terminated either. The zero-termination is just a detail to avoid keeping memory uninitialized. The thing is that *dest should not be NULL, and malloc(0) will return NULL. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33079
[Bug libfortran/33079] Optional empty strings do not appear to be 'PRESENT'
-- fxcoudert at gcc dot gnu dot org changed: What|Removed |Added URL||http://gcc.gnu.org/ml/gcc- ||patches/2007- ||08/msg01012.html Target Milestone|--- |4.3.0 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33079
[Bug libfortran/33079] Optional empty strings do not appear to be 'PRESENT'
--- Comment #4 from patchapp at dberlin dot org 2007-08-16 13:25 --- Subject: Bug number PR33079 A patch for this bug has been added to the patch tracker. The mailing list url for the patch is http://gcc.gnu.org/ml/gcc-patches/2007-08/msg01012.html -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33079
[Bug libfortran/33079] Optional empty strings do not appear to be 'PRESENT'
--- Comment #5 from fxcoudert at gcc dot gnu dot org 2007-08-17 13:09 --- Subject: Bug 33079 Author: fxcoudert Date: Fri Aug 17 13:09:23 2007 New Revision: 127584 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=127584 Log: PR fortran/33079 * intrinsics/string_intrinsics.c (string_trim, string_minmax): Fix the zero-length result case. * gfortran.dg/zero_length_2.f90: New test. Added: trunk/gcc/testsuite/gfortran.dg/zero_length_2.f90 Modified: trunk/gcc/testsuite/ChangeLog trunk/libgfortran/ChangeLog trunk/libgfortran/intrinsics/string_intrinsics.c -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33079
[Bug libfortran/33079] Optional empty strings do not appear to be 'PRESENT'
--- Comment #6 from fxcoudert at gcc dot gnu dot org 2007-08-17 13:10 --- Fixed. -- fxcoudert at gcc dot gnu dot org changed: What|Removed |Added Status|ASSIGNED|RESOLVED Resolution||FIXED http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33079