diff xrogue/state.c @ 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 1af259ac4ed2
children 6e6fb0955095
line wrap: on
line diff
--- 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);
 }