From 419a920e1db5771199e5c3a7fb9a7761b7e4bcf5 Mon Sep 17 00:00:00 2001 From: "John \"Elwin\" Edwards" Date: Fri, 26 Jun 2015 11:42:02 -0400 Subject: [PATCH] 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. --- arogue7/state.c | 10 ++++++++-- xrogue/state.c | 24 ++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/arogue7/state.c b/arogue7/state.c index 4656b89..8a8d6ee 100644 --- a/arogue7/state.c +++ b/arogue7/state.c @@ -549,7 +549,10 @@ rs_read_long(int inf, long *i) buf = bytes; } - *i = *((long *) buf); + if (sizeof(long) == 8) + *i = *((int *) buf); + else + *i = *((long *) buf); return(READSTAT); } @@ -641,7 +644,10 @@ rs_read_ulong(int inf, unsigned long *i) buf = bytes; } - *i = *((unsigned long *) buf); + if ( (sizeof(long) == 8) && (sizeof(int) == 4) ) + *i = *((unsigned int *) buf); + else + *i = *((unsigned long *) buf); return(READSTAT); } diff --git a/xrogue/state.c b/xrogue/state.c index 83a17b1..169c7e7 100644 --- a/xrogue/state.c +++ b/xrogue/state.c @@ -201,9 +201,16 @@ rs_write_int(FILE *savef, int c) 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_ulong(FILE *savef, unsigned long c) 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 @@ rs_read_ulong(int inf, unsigned long *i) 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 @@ rs_read_long(int inf, long *i) buf = bytes; } - *i = *((long *) buf); + if ( (sizeof(long) == 8) && (sizeof(int) == 4) ) + *i = *((int *) buf); + else + *i = *((long *) buf); return(READSTAT); }