changeset 166:9b5f1e6aa35a

arogue7, xrogue: fix uninitialized variables when restoring. The save and restore code assumed sizeof(long) == 4, which is not the case on x64. Reading only 4 bytes from the savefile left the others uninitialized, which led to problems like billions of experience points or gold pieces.
author John "Elwin" Edwards
date Fri, 26 Jun 2015 11:42:02 -0400
parents 2d94c32a709e
children a0a57cf42810
files arogue7/state.c xrogue/state.c
diffstat 2 files changed, 30 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/arogue7/state.c	Fri Jun 26 11:32:37 2015 -0400
+++ b/arogue7/state.c	Fri Jun 26 11:42:02 2015 -0400
@@ -549,7 +549,10 @@
         buf = bytes;
     }
     
-    *i = *((long *) buf);
+    if (sizeof(long) == 8)
+        *i = *((int *) buf);
+    else
+        *i = *((long *) buf);
 
     return(READSTAT);
 }
@@ -641,7 +644,10 @@
         buf = bytes;
     }
     
-    *i = *((unsigned long *) buf);
+    if ( (sizeof(long) == 8) && (sizeof(int) == 4) )
+      *i = *((unsigned int *) buf);
+    else
+      *i = *((unsigned long *) buf);
 
     return(READSTAT);
 }
--- a/xrogue/state.c	Fri Jun 26 11:32:37 2015 -0400
+++ b/xrogue/state.c	Fri Jun 26 11:42:02 2015 -0400
@@ -201,9 +201,16 @@
 
 rs_write_ulong(FILE *savef, unsigned long c)
 {
+    unsigned int c2;
     char bytes[4];
     char *buf = (char *)&c;
     
+    if ( (sizeof(long) == 8) && (sizeof(int) == 4) )
+    {
+        c2 = c;
+        buf = (char *) &c2;
+    }
+
     if (big_endian)
     {
         bytes[3] = buf[0];
@@ -220,9 +227,16 @@
 
 rs_write_long(FILE *savef, long c)
 {
+    int c2;
     char bytes[4];
     char *buf = (char *)&c;
     
+    if ( (sizeof(long) == 8) && (sizeof(int) == 4) )
+    {
+        c2 = c;
+        buf = (char *) &c2;
+    }
+
     if (big_endian)
     {
         bytes[3] = buf[0];
@@ -312,7 +326,10 @@
         buf = bytes;
     }
     
-    *i = *((unsigned long *) buf);
+    if ( (sizeof(long) == 8) && (sizeof(int) == 4) )
+      *i = *((unsigned int *) buf);
+    else
+      *i = *((unsigned long *) buf);
     return(READSTAT);
 }
 
@@ -333,7 +350,10 @@
         buf = bytes;
     }
     
-    *i = *((long *) buf);
+    if ( (sizeof(long) == 8) && (sizeof(int) == 4) )
+      *i = *((int *) buf);
+    else
+      *i = *((long *) buf);
     return(READSTAT);
 }