public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libfortran/33079]  New: Optional empty strings do not appear to be 'PRESENT'
@ 2007-08-15 13:02 P dot Schaffnit at access dot rwth-aachen dot de
  2007-08-15 13:06 ` [Bug libfortran/33079] " fxcoudert at gcc dot gnu dot org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: P dot Schaffnit at access dot rwth-aachen dot de @ 2007-08-15 13:02 UTC (permalink / raw)
  To: gcc-bugs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 972 bytes --]

Hi!


See
http://gcc.gnu.org/ml/fortran/2007-08/msg00336.html
and
http://gcc.gnu.org/ml/fortran/2007-08/msg00338.html

GFortran seems to handle in a slightly surprising way optional empty strings...

Cheers!

Philippe

PS: a testcase from François-Xavier Coudert

  CHARACTER(LEN=1) :: s
  s = " "
  CALL bar (s)
  CALL bar (trim(s))
CONTAINS
  SUBROUTINE bar (s)
    CHARACTER(LEN=*), OPTIONAL :: s
    IF (PRESENT (s)) THEN
      WRITE(*,"(A)") "Argument: |" // s // "|"
    ELSE
      WRITE(*,"(A)") "No argument"
    END IF
  END SUBROUTINE bar
END


-- 
           Summary: Optional empty strings do not appear to be 'PRESENT'
           Product: gcc
           Version: 4.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libfortran
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: P dot Schaffnit at access dot rwth-aachen dot de


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33079


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug libfortran/33079] Optional empty strings do not appear to be 'PRESENT'
  2007-08-15 13:02 [Bug libfortran/33079] New: Optional empty strings do not appear to be 'PRESENT' P dot Schaffnit at access dot rwth-aachen dot de
@ 2007-08-15 13:06 ` fxcoudert at gcc dot gnu dot org
  2007-08-15 14:06 ` fxcoudert at gcc dot gnu dot org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: fxcoudert at gcc dot gnu dot org @ 2007-08-15 13:06 UTC (permalink / raw)
  To: gcc-bugs



-- 

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|0000-00-00 00:00:00         |2007-08-15 13:05:53
               date|                            |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33079


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug libfortran/33079] Optional empty strings do not appear to be 'PRESENT'
  2007-08-15 13:02 [Bug libfortran/33079] New: Optional empty strings do not appear to be 'PRESENT' P dot Schaffnit at access dot rwth-aachen dot de
  2007-08-15 13:06 ` [Bug libfortran/33079] " fxcoudert at gcc dot gnu dot org
@ 2007-08-15 14:06 ` fxcoudert at gcc dot gnu dot org
  2007-08-15 14:18 ` schwab at suse dot de
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: fxcoudert at gcc dot gnu dot org @ 2007-08-15 14:06 UTC (permalink / raw)
  To: gcc-bugs



------- 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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug libfortran/33079] Optional empty strings do not appear to be 'PRESENT'
  2007-08-15 13:02 [Bug libfortran/33079] New: Optional empty strings do not appear to be 'PRESENT' P dot Schaffnit at access dot rwth-aachen dot de
  2007-08-15 13:06 ` [Bug libfortran/33079] " fxcoudert at gcc dot gnu dot org
  2007-08-15 14:06 ` fxcoudert at gcc dot gnu dot org
@ 2007-08-15 14:18 ` schwab at suse dot de
  2007-08-15 14:22 ` fxcoudert at gcc dot gnu dot org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: schwab at suse dot de @ 2007-08-15 14:18 UTC (permalink / raw)
  To: gcc-bugs



------- 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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug libfortran/33079] Optional empty strings do not appear to be 'PRESENT'
  2007-08-15 13:02 [Bug libfortran/33079] New: Optional empty strings do not appear to be 'PRESENT' P dot Schaffnit at access dot rwth-aachen dot de
                   ` (2 preceding siblings ...)
  2007-08-15 14:18 ` schwab at suse dot de
@ 2007-08-15 14:22 ` fxcoudert at gcc dot gnu dot org
  2007-08-16 13:22 ` fxcoudert at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: fxcoudert at gcc dot gnu dot org @ 2007-08-15 14:22 UTC (permalink / raw)
  To: gcc-bugs



------- 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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug libfortran/33079] Optional empty strings do not appear to be 'PRESENT'
  2007-08-15 13:02 [Bug libfortran/33079] New: Optional empty strings do not appear to be 'PRESENT' P dot Schaffnit at access dot rwth-aachen dot de
                   ` (3 preceding siblings ...)
  2007-08-15 14:22 ` fxcoudert at gcc dot gnu dot org
@ 2007-08-16 13:22 ` fxcoudert at gcc dot gnu dot org
  2007-08-16 13:25 ` patchapp at dberlin dot org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: fxcoudert at gcc dot gnu dot org @ 2007-08-16 13:22 UTC (permalink / raw)
  To: gcc-bugs



-- 

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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug libfortran/33079] Optional empty strings do not appear to be 'PRESENT'
  2007-08-15 13:02 [Bug libfortran/33079] New: Optional empty strings do not appear to be 'PRESENT' P dot Schaffnit at access dot rwth-aachen dot de
                   ` (4 preceding siblings ...)
  2007-08-16 13:22 ` fxcoudert at gcc dot gnu dot org
@ 2007-08-16 13:25 ` patchapp at dberlin dot org
  2007-08-17 13:10 ` fxcoudert at gcc dot gnu dot org
  2007-08-17 13:11 ` fxcoudert at gcc dot gnu dot org
  7 siblings, 0 replies; 9+ messages in thread
From: patchapp at dberlin dot org @ 2007-08-16 13:25 UTC (permalink / raw)
  To: gcc-bugs



------- 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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug libfortran/33079] Optional empty strings do not appear to be 'PRESENT'
  2007-08-15 13:02 [Bug libfortran/33079] New: Optional empty strings do not appear to be 'PRESENT' P dot Schaffnit at access dot rwth-aachen dot de
                   ` (5 preceding siblings ...)
  2007-08-16 13:25 ` patchapp at dberlin dot org
@ 2007-08-17 13:10 ` fxcoudert at gcc dot gnu dot org
  2007-08-17 13:11 ` fxcoudert at gcc dot gnu dot org
  7 siblings, 0 replies; 9+ messages in thread
From: fxcoudert at gcc dot gnu dot org @ 2007-08-17 13:10 UTC (permalink / raw)
  To: gcc-bugs



------- 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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug libfortran/33079] Optional empty strings do not appear to be 'PRESENT'
  2007-08-15 13:02 [Bug libfortran/33079] New: Optional empty strings do not appear to be 'PRESENT' P dot Schaffnit at access dot rwth-aachen dot de
                   ` (6 preceding siblings ...)
  2007-08-17 13:10 ` fxcoudert at gcc dot gnu dot org
@ 2007-08-17 13:11 ` fxcoudert at gcc dot gnu dot org
  7 siblings, 0 replies; 9+ messages in thread
From: fxcoudert at gcc dot gnu dot org @ 2007-08-17 13:11 UTC (permalink / raw)
  To: gcc-bugs



------- 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


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2007-08-17 13:11 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-08-15 13:02 [Bug libfortran/33079] New: Optional empty strings do not appear to be 'PRESENT' P dot Schaffnit at access dot rwth-aachen dot de
2007-08-15 13:06 ` [Bug libfortran/33079] " fxcoudert at gcc dot gnu dot org
2007-08-15 14:06 ` fxcoudert at gcc dot gnu dot org
2007-08-15 14:18 ` schwab at suse dot de
2007-08-15 14:22 ` fxcoudert at gcc dot gnu dot org
2007-08-16 13:22 ` fxcoudert at gcc dot gnu dot org
2007-08-16 13:25 ` patchapp at dberlin dot org
2007-08-17 13:10 ` fxcoudert at gcc dot gnu dot org
2007-08-17 13:11 ` fxcoudert at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).