From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 23066 invoked by alias); 15 Aug 2007 14:06:17 -0000 Received: (qmail 21803 invoked by uid 48); 15 Aug 2007 14:05:32 -0000 Date: Wed, 15 Aug 2007 14:06:00 -0000 Message-ID: <20070815140532.21802.qmail@sourceware.org> X-Bugzilla-Reason: CC References: Subject: [Bug libfortran/33079] Optional empty strings do not appear to be 'PRESENT' In-Reply-To: Reply-To: gcc-bugzilla@gcc.gnu.org To: gcc-bugs@gcc.gnu.org From: "fxcoudert at gcc dot gnu dot org" Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2007-08/txt/msg01011.txt.bz2 ------- 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