This is the mail archive of the
fortran@gcc.gnu.org
mailing list for the GNU Fortran project.
[libgfortran,patch] Zero-length strings messed up in library (PR33079)
- From: "François-Xavier Coudert" <fxcoudert at gmail dot com>
- To: GFortran <fortran at gcc dot gnu dot org>
- Cc: gcc-patches <gcc-patches at gcc dot gnu dot org>
- Date: Thu, 16 Aug 2007 14:21:07 +0100
- Subject: [libgfortran,patch] Zero-length strings messed up in library (PR33079)
- Dkim-signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:mime-version:content-type:content-transfer-encoding:content-disposition; b=kR0oIRy0CKEpuBGHM6j0SwrCLL7X+GIs2H5OdHAxLHtUE+uqNT6ODYR9HEldHOcPrmE7we16fMAHL6zyfVJZHZ+hyIqY06GMoklJxTERk4WoUVh/EXpFRMWNjB0iiEphJtgnDryLV6ry80lDtIZ/GC/c0S8lSIE8lyQCKlt5p+A=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:cc:mime-version:content-type:content-transfer-encoding:content-disposition; b=hbCwuugx/g3PGkWZgMpBNTb7qeG9hT+RAI/fu3HGbvawBwPIaOov16wKvTZSmeUG4oOZkbpOc4GQHxyx2pkoxZWbG7HhEfQ+/yA1I2LD/8M67s71hKJia1CMX3CmLQ07w/+Fjz+AmX0QsLFKm5xrQ2pnsb3ViC7aJ+DH7xGav1Y=
Hi all,
When TRIM, MIN and MAX result is a zero-length string, the library
functions actually make it a NULL pointer instead "" (ie a pointer to
'\0'). The MIN and MAX case error is mine, because I copied the code
from TRIM when I wrote string_minmax(), while the error in
string_trim() has probably been there for a long time. This patch
fixes both. I could not see any other library routine which suffered
from this problem.
Regtested on x86_64-linux, comes with a testcase. OK for mainline?
FX
:ADDPATCH libgfortran:
2007-08-16 Francois-Xavier Coudert <fxcoudert@gcc.gnu.org>
PR fortran/33079
* intrinsics/string_intrinsics.c (string_trim, string_minmax):
Fix the zero-length result case.
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;
}
-
! { dg-do run }
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