From 43a6f9c7bb02e24dfb565babcb52ddf3d6a0fe52 Mon Sep 17 00:00:00 2001
From: Ben Pfaff <blp@gnu.org>
Date: Thu, 12 Feb 2004 04:40:55 +0000
Subject: [PATCH] Fixed crash from FLIP when a numeric variable is specified on
 NEWNAMES and a large value is used, and a couple of other minor bugs in FLIP.

---
 src/ChangeLog | 12 ++++++++++++
 src/flip.c    | 37 +++++++++++++++++++++++++------------
 2 files changed, 37 insertions(+), 12 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index 53cf28a2..556199ef 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,15 @@
+Wed Feb 11 20:33:18 2004  Ben Pfaff  <blp@gnu.org>
+
+	* flip.c: Fixed crash from FLIP when a numeric variable is
+	  specified on NEWNAMES and a large value is used, and a couple of
+	  other minor bugs besides.
+	  (struct varname) Make name a 9-character fixed-size array
+	  instead of a 1-character variable size array.
+	  (make_new_var) Allow digits in variable names.
+	  (flip_stream_write) Limit numeric values to 8 characters and
+	  format system missing and very large and small values more
+	  appropriately.
+
 Thu Feb  5 13:19:06 WAST 2004 John Darrington <john@darrington.wattle.id.au>
 
 	* fixed bug which caused a crash under certain invalid input.
diff --git a/src/flip.c b/src/flip.c
index d2eb197c..de02119e 100644
--- a/src/flip.c
+++ b/src/flip.c
@@ -22,6 +22,7 @@
 #include <ctype.h>
 #include <errno.h>
 #include <float.h>
+#include <limits.h>
 #include <stdlib.h>
 #include "alloc.h"
 #include "command.h"
@@ -43,7 +44,7 @@ static struct variable *newnames;
 struct varname
   {
     struct varname *next;
-    char name[1];
+    char name[9];
   };
 
 /* New variable names. */
@@ -126,7 +127,8 @@ make_new_var (char name[])
       {
 	*cp = toupper ((unsigned char) *cp);
 	if (!isalpha (*cp) && *cp != '@' && *cp != '#'
-	    && (cp == name || (*cp != '.' && *cp != '$' && *cp != '_')))
+	    && (cp == name || (*cp != '.' && *cp != '$' && *cp != '_'
+                               && !isdigit (*cp))))
 	  {
 	    if (cp == name)
 	      *cp = 'V';	/* _ not valid in first position. */
@@ -307,20 +309,31 @@ flip_stream_write (void)
 
   if (newnames)
     {
-      struct varname *v;
-      char name[INT_DIGITS + 2];
-
-      if (newnames->type == NUMERIC)
-	sprintf (name, "V%d", (int) temp_case->data[newnames->fv].f);
+      struct varname *v = xmalloc (sizeof (struct varname));
+      if (newnames->type == NUMERIC) 
+        {
+          double f = temp_case->data[newnames->fv].f;
+
+          if (f == SYSMIS)
+            strcpy (v->name, "VSYSMIS");
+          else if (f < INT_MIN)
+            strcpy (v->name, "VNEGINF");
+          else if (f > INT_MAX)
+            strcpy (v->name, "VPOSINF");
+          else 
+            {
+              char name[INT_DIGITS + 2];
+              sprintf (name, "V%d", (int) f);
+              strncpy (v->name, name, 8);
+              name[8] = 0; 
+            }
+        }
       else
 	{
 	  int width = min (newnames->width, 8);
-	  memcpy (name, temp_case->data[newnames->fv].s, width);
-	  name[width] = 0;
+	  memcpy (v->name, temp_case->data[newnames->fv].s, width);
+	  v->name[width] = 0;
 	}
-
-      v = xmalloc (sizeof (struct varname) + strlen (name) - 1);
-      strcpy (v->name, name);
       
       if (new_names_tail == NULL)
 	new_names_head = v;
-- 
2.30.2