This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[gfortran,patch] Correctly NULL-terminate character constant strings


:ADDPATCH fortran:

Hi all,

Attached patch corrects a few functions which did not correctly
NULL-terminate value.character.string for CHARACTER constants.
Although it appears that this NULL-termination was not required when
the front-end was first written, a few places now rely on it, and I
don't think it's too bad to accomodate them. I've tried to review and
fix not only the problem in the original PR, but all other such
places.

OK for mainline, 4.2 and 4.1?

FX

Attachment: pr29067.ChangeLog
Description: Binary data

Index: decl.c
===================================================================
--- decl.c	(revision 118134)
+++ decl.c	(working copy)
@@ -753,10 +753,11 @@
   slen = expr->value.character.length;
   if (len != slen)
     {
-      s = gfc_getmem (len);
+      s = gfc_getmem (len + 1);
       memcpy (s, expr->value.character.string, MIN (len, slen));
       if (len > slen)
 	memset (&s[slen], ' ', len - slen);
+      s[len] = '\0';
       gfc_free (expr->value.character.string);
       expr->value.character.string = s;
       expr->value.character.length = len;
Index: data.c
===================================================================
--- data.c	(revision 118134)
+++ data.c	(working copy)
@@ -155,7 +155,8 @@
       init->expr_type = EXPR_CONSTANT;
       init->ts = *ts;
       
-      dest = gfc_getmem (len);
+      dest = gfc_getmem (len + 1);
+      dest[len] = '\0';
       init->value.character.length = len;
       init->value.character.string = dest;
       /* Blank the string if we're only setting a substring.  */
Index: expr.c
===================================================================
--- expr.c	(revision 118134)
+++ expr.c	(working copy)
@@ -1438,7 +1438,7 @@
 	  gfc_extract_int (p->ref->u.ss.end, &end);
 	  s = gfc_getmem (end - start + 1);
 	  memcpy (s, p->value.character.string + start, end - start);
-	  s[end] = '\0';  /* TODO: C-style string for debugging.  */
+	  s[end-start+1] = '\0';  /* TODO: C-style string for debugging.  */
 	  gfc_free (p->value.character.string);
 	  p->value.character.string = s;
 	  p->value.character.length = end - start;
Index: primary.c
===================================================================
--- primary.c	(revision 118134)
+++ primary.c	(working copy)
@@ -281,6 +281,7 @@
 		gfc_default_character_kind, &gfc_current_locus);
 	  e->value.character.string = gfc_getmem (num+1);
 	  memcpy (e->value.character.string, buffer, num);
+	  e->value.character.string[num] = '\0';
 	  e->value.character.length = num;
 	  *result = e;
 	  return MATCH_YES;

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]